Java Code Examples for org.eclipse.jdt.core.dom.rewrite.ListRewrite#insertAfter()

The following examples show how to use org.eclipse.jdt.core.dom.rewrite.ListRewrite#insertAfter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReplaceRewrite.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
protected void handleManyMany(ASTNode[] replacements, TextEditGroup description) {
	ListRewrite container= fRewrite.getListRewrite(fToReplace[0].getParent(), (ChildListPropertyDescriptor)fDescriptor);
	if (fToReplace.length == replacements.length) {
		for (int i= 0; i < fToReplace.length; i++) {
			container.replace(fToReplace[i], replacements[i], description);
		}
	} else if (fToReplace.length < replacements.length) {
		for (int i= 0; i < fToReplace.length; i++) {
			container.replace(fToReplace[i], replacements[i], description);
		}
		for (int i= fToReplace.length; i < replacements.length; i++) {
			container.insertAfter(replacements[i], replacements[i - 1], description);
		}
	} else if (fToReplace.length > replacements.length) {
		int delta= fToReplace.length - replacements.length;
		for(int i= 0; i < delta; i++) {
			container.remove(fToReplace[i], description);
		}
		for (int i= delta, r= 0; i < fToReplace.length; i++, r++) {
			container.replace(fToReplace[i], replacements[r], description);
		}
	}
}
 
Example 2
Source File: StatementRewrite.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void handleOneMany(ASTNode[] replacements, TextEditGroup description) {
	AST ast= fToReplace[0].getAST();
	// to replace == 1, but more than one replacement. Have to check if we
	// need to insert a block to not change structure
	if (ASTNodes.isControlStatementBody(fDescriptor)) {
		Block block= ast.newBlock();
		ListRewrite statements= fRewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
		for (int i= 0; i < replacements.length; i++) {
			statements.insertLast(replacements[i], description);
		}
		fRewrite.replace(fToReplace[0], block, description);
	} else {
		ListRewrite container= fRewrite.getListRewrite(fToReplace[0].getParent(), (ChildListPropertyDescriptor)fDescriptor);
		container.replace(fToReplace[0], replacements[0], description);
		for (int i= 1; i < replacements.length; i++) {
			container.insertAfter(replacements[i], replacements[i - 1], description);
		}
	}
}
 
Example 3
Source File: DelegateCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Performs the actual rewriting and adds an edit to the ASTRewrite set with
 * {@link #setSourceRewrite(CompilationUnitRewrite)}.
 *
 * @throws JavaModelException
 */
public void createEdit() throws JavaModelException {
	try {
		IDocument document= new Document(fDelegateRewrite.getCu().getBuffer().getContents());
		TextEdit edit= fDelegateRewrite.getASTRewrite().rewriteAST(document, fDelegateRewrite.getCu().getJavaProject().getOptions(true));
		edit.apply(document, TextEdit.UPDATE_REGIONS);

		String newSource= Strings.trimIndentation(document.get(fTrackedPosition.getStartPosition(), fTrackedPosition.getLength()),
				fPreferences.tabWidth, fPreferences.indentWidth, false);

		ASTNode placeholder= fOriginalRewrite.getASTRewrite().createStringPlaceholder(newSource, fDeclaration.getNodeType());

		CategorizedTextEditGroup groupDescription= fOriginalRewrite.createCategorizedGroupDescription(getTextEditGroupLabel(), CATEGORY_DELEGATE);
		ListRewrite bodyDeclarationsListRewrite= fOriginalRewrite.getASTRewrite().getListRewrite(fDeclaration.getParent(), getTypeBodyDeclarationsProperty());
		if (fCopy)
			if (fInsertBefore)
				bodyDeclarationsListRewrite.insertBefore(placeholder, fDeclaration, groupDescription);
			else
				bodyDeclarationsListRewrite.insertAfter(placeholder, fDeclaration, groupDescription);
		else
			bodyDeclarationsListRewrite.replace(fDeclaration, placeholder, groupDescription);

	} catch (BadLocationException e) {
		JavaPlugin.log(e);
	}
}
 
Example 4
Source File: ReplaceRewrite.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected void handleManyMany(ASTNode[] replacements, TextEditGroup description) {
	ListRewrite container= fRewrite.getListRewrite(fToReplace[0].getParent(), (ChildListPropertyDescriptor)fDescriptor);
	if (fToReplace.length == replacements.length) {
		for (int i= 0; i < fToReplace.length; i++) {
			container.replace(fToReplace[i], replacements[i], description);
		}
	} else if (fToReplace.length < replacements.length) {
		for (int i= 0; i < fToReplace.length; i++) {
			container.replace(fToReplace[i], replacements[i], description);
		}
		for (int i= fToReplace.length; i < replacements.length; i++) {
			container.insertAfter(replacements[i], replacements[i - 1], description);
		}
	} else if (fToReplace.length > replacements.length) {
		int delta= fToReplace.length - replacements.length;
		for(int i= 0; i < delta; i++) {
			container.remove(fToReplace[i], description);
		}
		for (int i= delta, r= 0; i < fToReplace.length; i++, r++) {
			container.replace(fToReplace[i], replacements[r], description);
		}
	}
}
 
Example 5
Source File: StatementRewrite.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void handleOneMany(ASTNode[] replacements, TextEditGroup description) {
	AST ast= fToReplace[0].getAST();
	// to replace == 1, but more than one replacement. Have to check if we
	// need to insert a block to not change structure
	if (ASTNodes.isControlStatementBody(fDescriptor)) {
		Block block= ast.newBlock();
		ListRewrite statements= fRewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
		for (int i= 0; i < replacements.length; i++) {
			statements.insertLast(replacements[i], description);
		}
		fRewrite.replace(fToReplace[0], block, description);
	} else {
		ListRewrite container= fRewrite.getListRewrite(fToReplace[0].getParent(), (ChildListPropertyDescriptor)fDescriptor);
		container.replace(fToReplace[0], replacements[0], description);
		for (int i= 1; i < replacements.length; i++) {
			container.insertAfter(replacements[i], replacements[i - 1], description);
		}
	}
}
 
Example 6
Source File: ConstructorInsertionFragment.java    From SparkBuilderGenerator with MIT License 5 votes vote down vote up
public void insertMethodToFirstPlace(TypeDeclaration originalType, ListRewrite listRewrite, MethodDeclaration constructor) {
    FieldDeclaration[] fields = originalType.getFields();
    if (fields == null || fields.length == 0) {
        listRewrite.insertFirst(constructor, null);
    } else {
        listRewrite.insertAfter(constructor, fields[fields.length - 1], null);
    }
}
 
Example 7
Source File: DelegateCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Performs the actual rewriting and adds an edit to the ASTRewrite set with
 * {@link #setSourceRewrite(CompilationUnitRewrite)}.
 *
 * @throws JavaModelException
 */
public void createEdit() throws JavaModelException {
	try {
		IDocument document= new Document(fDelegateRewrite.getCu().getBuffer().getContents());
		TextEdit edit= fDelegateRewrite.getASTRewrite().rewriteAST(document, fDelegateRewrite.getCu().getJavaProject().getOptions(true));
		edit.apply(document, TextEdit.UPDATE_REGIONS);

		int tabWidth = CodeFormatterUtil.getTabWidth(fOriginalRewrite.getCu().getJavaProject());
		int identWidth = CodeFormatterUtil.getIndentWidth(fOriginalRewrite.getCu().getJavaProject());

		String newSource= Strings.trimIndentation(document.get(fTrackedPosition.getStartPosition(), fTrackedPosition.getLength()),
				tabWidth, identWidth, false);

		ASTNode placeholder= fOriginalRewrite.getASTRewrite().createStringPlaceholder(newSource, fDeclaration.getNodeType());

		CategorizedTextEditGroup groupDescription= fOriginalRewrite.createCategorizedGroupDescription(getTextEditGroupLabel(), CATEGORY_DELEGATE);
		ListRewrite bodyDeclarationsListRewrite= fOriginalRewrite.getASTRewrite().getListRewrite(fDeclaration.getParent(), getTypeBodyDeclarationsProperty());
		if (fCopy) {
			if (fInsertBefore) {
				bodyDeclarationsListRewrite.insertBefore(placeholder, fDeclaration, groupDescription);
			} else {
				bodyDeclarationsListRewrite.insertAfter(placeholder, fDeclaration, groupDescription);
			}
		} else {
			bodyDeclarationsListRewrite.replace(fDeclaration, placeholder, groupDescription);
		}

	} catch (BadLocationException e) {
		//JavaPlugin.log(e);
	}
}
 
Example 8
Source File: JavadocTagsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static void insertTag(ListRewrite rewriter, TagElement newElement, Set<String> sameKindLeadingNames, TextEditGroup groupDescription) {
	List<? extends ASTNode> tags= rewriter.getRewrittenList();

	String insertedTagName= newElement.getTagName();

	ASTNode after= null;
	int tagRanking= getTagRanking(insertedTagName);
	for (int i= tags.size() - 1; i >= 0; i--) {
		TagElement curr= (TagElement) tags.get(i);
		String tagName= curr.getTagName();
		if (tagName == null || tagRanking > getTagRanking(tagName)) {
			after= curr;
			break;
		}
		if (sameKindLeadingNames != null && isSameTag(insertedTagName, tagName)) {
			String arg= getArgument(curr);
			if (arg != null && sameKindLeadingNames.contains(arg)) {
				after= curr;
				break;
			}
		}
	}
	if (after != null) {
		rewriter.insertAfter(newElement, after, groupDescription);
	} else {
		rewriter.insertFirst(newElement, groupDescription);
	}
}
 
Example 9
Source File: UnusedCodeFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void splitUpDeclarations(ASTRewrite rewrite, TextEditGroup group, VariableDeclarationFragment frag, VariableDeclarationStatement originalStatement, List<Expression> sideEffects) {
	if (sideEffects.size() > 0) {
		ListRewrite statementRewrite= rewrite.getListRewrite(originalStatement.getParent(), (ChildListPropertyDescriptor) originalStatement.getLocationInParent());
		
		Statement previousStatement= originalStatement;
		for (int i= 0; i < sideEffects.size(); i++) {
			Expression sideEffect= sideEffects.get(i);
			Expression movedInit= (Expression) rewrite.createMoveTarget(sideEffect);
			ExpressionStatement wrapped= rewrite.getAST().newExpressionStatement(movedInit);
			statementRewrite.insertAfter(wrapped, previousStatement, group);
			previousStatement= wrapped;
		}

		VariableDeclarationStatement newDeclaration= null;
		List<VariableDeclarationFragment> fragments= originalStatement.fragments();
		int fragIndex= fragments.indexOf(frag);
		ListIterator<VariableDeclarationFragment> fragmentIterator= fragments.listIterator(fragIndex+1);
		while (fragmentIterator.hasNext()) {
			VariableDeclarationFragment currentFragment= fragmentIterator.next();
			VariableDeclarationFragment movedFragment= (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
			if (newDeclaration == null) {
				newDeclaration= rewrite.getAST().newVariableDeclarationStatement(movedFragment);
				Type copiedType= (Type) rewrite.createCopyTarget(originalStatement.getType());
				newDeclaration.setType(copiedType);
			} else {
				newDeclaration.fragments().add(movedFragment);
			}
		}
		if (newDeclaration != null){
			statementRewrite.insertAfter(newDeclaration, previousStatement, group);
			if (originalStatement.fragments().size() == newDeclaration.fragments().size() + 1){
				rewrite.remove(originalStatement, group);
			}
		}
	}
}
 
Example 10
Source File: JavadocTagsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static void insertTag(ListRewrite rewriter, TagElement newElement, Set<String> sameKindLeadingNames, TextEditGroup groupDescription) {
	List<? extends ASTNode> tags= rewriter.getRewrittenList();

	String insertedTagName= newElement.getTagName();

	ASTNode after= null;
	int tagRanking= getTagRanking(insertedTagName);
	for (int i= tags.size() - 1; i >= 0; i--) {
		TagElement curr= (TagElement) tags.get(i);
		String tagName= curr.getTagName();
		if (tagName == null || tagRanking > getTagRanking(tagName)) {
			after= curr;
			break;
		}
		if (sameKindLeadingNames != null && isSameTag(insertedTagName, tagName)) {
			String arg= getArgument(curr);
			if (arg != null && sameKindLeadingNames.contains(arg)) {
				after= curr;
				break;
			}
		}
	}
	if (after != null) {
		rewriter.insertAfter(newElement, after, groupDescription);
	} else {
		rewriter.insertFirst(newElement, groupDescription);
	}
}
 
Example 11
Source File: CreateElementInCUOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Inserts the given child into the given AST,
 * based on the position settings of this operation.
 *
 * @see #createAfter(IJavaElement)
 * @see #createBefore(IJavaElement)
 */
protected void insertASTNode(ASTRewrite rewriter, ASTNode parent, ASTNode child) throws JavaModelException {
	StructuralPropertyDescriptor propertyDescriptor = getChildPropertyDescriptor(parent);
	if (propertyDescriptor instanceof ChildListPropertyDescriptor) {
		ChildListPropertyDescriptor childListPropertyDescriptor = (ChildListPropertyDescriptor) propertyDescriptor;
 		ListRewrite rewrite = rewriter.getListRewrite(parent, childListPropertyDescriptor);
 		switch (this.insertionPolicy) {
 			case INSERT_BEFORE:
 				ASTNode element = ((JavaElement) this.anchorElement).findNode(this.cuAST);
 				if (childListPropertyDescriptor.getElementType().isAssignableFrom(element.getClass()))
	 				rewrite.insertBefore(child, element, null);
 				else
 					// case of an empty import list: the anchor element is the top level type and cannot be used in insertBefore as it is not the same type
 					rewrite.insertLast(child, null);
 				break;
 			case INSERT_AFTER:
 				element = ((JavaElement) this.anchorElement).findNode(this.cuAST);
 				if (childListPropertyDescriptor.getElementType().isAssignableFrom(element.getClass()))
	 				rewrite.insertAfter(child, element, null);
 				else
 					// case of an empty import list: the anchor element is the top level type and cannot be used in insertAfter as it is not the same type
 					rewrite.insertLast(child, null);
 				break;
 			case INSERT_LAST:
 				rewrite.insertLast(child, null);
 				break;
 		}
	} else {
		rewriter.set(parent, propertyDescriptor, child, null);
	}
	}
 
Example 12
Source File: ExtractConstantRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private void createConstantDeclaration() throws CoreException {
	Type type = getConstantType();

	IExpressionFragment fragment = getSelectedExpression();
	Expression initializer = getSelectedExpression().createCopyTarget(fCuRewrite.getASTRewrite(), true);

	AST ast = fCuRewrite.getAST();
	VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
	variableDeclarationFragment.setName(ast.newSimpleName(fConstantName));
	variableDeclarationFragment.setInitializer(initializer);

	FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(variableDeclarationFragment);
	fieldDeclaration.setType(type);
	Modifier.ModifierKeyword accessModifier = Modifier.ModifierKeyword.toKeyword(fVisibility);
	if (accessModifier != null) {
		fieldDeclaration.modifiers().add(ast.newModifier(accessModifier));
	}
	fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
	fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));

	//		boolean createComments = JavaPreferencesSettings.getCodeGenerationSettings(fCu.getJavaProject()).createComments;
	//		if (createComments) {
	String comment = CodeGeneration.getFieldComment(fCu, getConstantTypeName(), fConstantName, StubUtility.getLineDelimiterUsed(fCu));
	if (comment != null && comment.length() > 0 && !"/**\n *\n */".equals(comment)) {
		Javadoc doc = (Javadoc) fCuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC);
		fieldDeclaration.setJavadoc(doc);
	}
	//		}

	AbstractTypeDeclaration parent = getContainingTypeDeclarationNode();
	ListRewrite listRewrite = fCuRewrite.getASTRewrite().getListRewrite(parent, parent.getBodyDeclarationsProperty());
	TextEditGroup msg = fCuRewrite.createGroupDescription(RefactoringCoreMessages.ExtractConstantRefactoring_declare_constant);
	if (insertFirst()) {
		listRewrite.insertFirst(fieldDeclaration, msg);
	} else {
		listRewrite.insertAfter(fieldDeclaration, getNodeToInsertConstantDeclarationAfter(), msg);
	}

	if (fLinkedProposalModel != null) {
		ASTRewrite rewrite = fCuRewrite.getASTRewrite();
		LinkedProposalPositionGroupCore nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
		nameGroup.addPosition(rewrite.track(variableDeclarationFragment.getName()), true);

		String[] nameSuggestions = guessConstantNames();
		if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fConstantName)) {
			nameGroup.addProposal(fConstantName, nameSuggestions.length + 1);
		}
		for (int i = 0; i < nameSuggestions.length; i++) {
			nameGroup.addProposal(nameSuggestions[i], nameSuggestions.length - i);
		}

		LinkedProposalPositionGroupCore typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
		typeGroup.addPosition(rewrite.track(type), true);

		ITypeBinding typeBinding = guessBindingForReference(fragment.getAssociatedExpression());
		if (typeBinding != null) {
			ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding);
			for (int i = 0; i < relaxingTypes.length; i++) {
				typeGroup.addProposal(relaxingTypes[i], fCuRewrite.getCu(), relaxingTypes.length - i);
			}
		}
		boolean isInterface = parent.resolveBinding() != null && parent.resolveBinding().isInterface();
		ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, rewrite, fieldDeclaration.modifiers(), isInterface);
	}
}
 
Example 13
Source File: ExtractClassRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private FieldDeclaration performFieldRewrite(IType type, ParameterObjectFactory pof, RefactoringStatus status) throws CoreException {
	fBaseCURewrite= new CompilationUnitRewrite(type.getCompilationUnit());
	SimpleName name= (SimpleName) NodeFinder.perform(fBaseCURewrite.getRoot(), type.getNameRange());
	TypeDeclaration typeNode= (TypeDeclaration) ASTNodes.getParent(name, ASTNode.TYPE_DECLARATION);
	ASTRewrite rewrite= fBaseCURewrite.getASTRewrite();
	int modifier= Modifier.PRIVATE;
	TextEditGroup removeFieldGroup= fBaseCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractClassRefactoring_group_remove_field);
	FieldDeclaration lastField= null;
	initializeDeclaration(typeNode);
	for (Iterator<FieldInfo> iter= fVariables.values().iterator(); iter.hasNext();) {
		FieldInfo pi= iter.next();
		if (isCreateField(pi)) {
			VariableDeclarationFragment vdf= pi.declaration;
			FieldDeclaration parent= (FieldDeclaration) vdf.getParent();
			if (lastField == null)
				lastField= parent;
			else if (lastField.getStartPosition() < parent.getStartPosition())
				lastField= parent;

			ListRewrite listRewrite= rewrite.getListRewrite(parent, FieldDeclaration.FRAGMENTS_PROPERTY);
			removeNode(vdf, removeFieldGroup, fBaseCURewrite);
			if (listRewrite.getRewrittenList().size() == 0) {
				removeNode(parent, removeFieldGroup, fBaseCURewrite);
			}

			if (fDescriptor.isCreateTopLevel()) {
				IVariableBinding binding= vdf.resolveBinding();
				ITypeRoot typeRoot= fBaseCURewrite.getCu();
				if (binding == null || binding.getType() == null){
					status.addFatalError(Messages.format(RefactoringCoreMessages.ExtractClassRefactoring_fatal_error_cannot_resolve_binding, BasicElementLabels.getJavaElementName(pi.name)), JavaStatusContext.create(typeRoot, vdf));
				} else {
					ITypeBinding typeBinding= binding.getType();
					if (Modifier.isPrivate(typeBinding.getModifiers())){
						status.addError(Messages.format(RefactoringCoreMessages.ExtractClassRefactoring_error_referencing_private_class, BasicElementLabels.getJavaElementName(typeBinding.getName())), JavaStatusContext.create(typeRoot, vdf));
					} else if (Modifier.isProtected(typeBinding.getModifiers())){
						ITypeBinding declaringClass= typeBinding.getDeclaringClass();
						if (declaringClass != null) {
							IPackageBinding package1= declaringClass.getPackage();
							if (package1 != null && !fDescriptor.getPackage().equals(package1.getName())){
								status.addError(Messages.format(RefactoringCoreMessages.ExtractClassRefactoring_error_referencing_protected_class, new String[] {BasicElementLabels.getJavaElementName(typeBinding.getName()), BasicElementLabels.getJavaElementName(fDescriptor.getPackage())}), JavaStatusContext.create(typeRoot, vdf));
							}
						}
					}
				}
			}
			Expression initializer= vdf.getInitializer();
			if (initializer != null)
				pi.initializer= initializer;
			int modifiers= parent.getModifiers();
			if (!MemberVisibilityAdjustor.hasLowerVisibility(modifiers, modifier)){
				modifier= modifiers;
			}
		}
	}
	FieldDeclaration fieldDeclaration= createParameterObjectField(pof, typeNode, modifier);
	ListRewrite bodyDeclList= rewrite.getListRewrite(typeNode, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
	if (lastField != null)
		bodyDeclList.insertAfter(fieldDeclaration, lastField, null);
	else
		bodyDeclList.insertFirst(fieldDeclaration, null);
	return fieldDeclaration;
}
 
Example 14
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void insertTag(TagElement tag, TagElement previousTag, ListRewrite tagsRewrite) {
	if (previousTag == null)
		tagsRewrite.insertFirst(tag, fDescription);
	else
		tagsRewrite.insertAfter(tag, previousTag, fDescription);
}
 
Example 15
Source File: ExtractConstantRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void createConstantDeclaration() throws CoreException {
	Type type= getConstantType();

	IExpressionFragment fragment= getSelectedExpression();
	Expression initializer= getSelectedExpression().createCopyTarget(fCuRewrite.getASTRewrite(), true);

	AST ast= fCuRewrite.getAST();
	VariableDeclarationFragment variableDeclarationFragment= ast.newVariableDeclarationFragment();
	variableDeclarationFragment.setName(ast.newSimpleName(fConstantName));
	variableDeclarationFragment.setInitializer(initializer);

	FieldDeclaration fieldDeclaration= ast.newFieldDeclaration(variableDeclarationFragment);
	fieldDeclaration.setType(type);
	Modifier.ModifierKeyword accessModifier= Modifier.ModifierKeyword.toKeyword(fVisibility);
	if (accessModifier != null)
		fieldDeclaration.modifiers().add(ast.newModifier(accessModifier));
	fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
	fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));

	boolean createComments= JavaPreferencesSettings.getCodeGenerationSettings(fCu.getJavaProject()).createComments;
	if (createComments) {
		String comment= CodeGeneration.getFieldComment(fCu, getConstantTypeName(), fConstantName, StubUtility.getLineDelimiterUsed(fCu));
		if (comment != null && comment.length() > 0) {
			Javadoc doc= (Javadoc) fCuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC);
			fieldDeclaration.setJavadoc(doc);
		}
	}

	AbstractTypeDeclaration parent= getContainingTypeDeclarationNode();
	ListRewrite listRewrite= fCuRewrite.getASTRewrite().getListRewrite(parent, parent.getBodyDeclarationsProperty());
	TextEditGroup msg= fCuRewrite.createGroupDescription(RefactoringCoreMessages.ExtractConstantRefactoring_declare_constant);
	if (insertFirst()) {
		listRewrite.insertFirst(fieldDeclaration, msg);
	} else {
		listRewrite.insertAfter(fieldDeclaration, getNodeToInsertConstantDeclarationAfter(), msg);
	}

	if (fLinkedProposalModel != null) {
		ASTRewrite rewrite= fCuRewrite.getASTRewrite();
		LinkedProposalPositionGroup nameGroup= fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
		nameGroup.addPosition(rewrite.track(variableDeclarationFragment.getName()), true);

		String[] nameSuggestions= guessConstantNames();
		if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fConstantName)) {
			nameGroup.addProposal(fConstantName, null, nameSuggestions.length + 1);
		}
		for (int i= 0; i < nameSuggestions.length; i++) {
			nameGroup.addProposal(nameSuggestions[i], null, nameSuggestions.length - i);
		}

		LinkedProposalPositionGroup typeGroup= fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
		typeGroup.addPosition(rewrite.track(type), true);

		ITypeBinding typeBinding= guessBindingForReference(fragment.getAssociatedExpression());
		if (typeBinding != null) {
			ITypeBinding[] relaxingTypes= ASTResolving.getNarrowingTypes(ast, typeBinding);
			for (int i= 0; i < relaxingTypes.length; i++) {
				typeGroup.addProposal(relaxingTypes[i], fCuRewrite.getCu(), relaxingTypes.length - i);
			}
		}
		boolean isInterface= parent.resolveBinding() != null && parent.resolveBinding().isInterface();
		ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, rewrite, fieldDeclaration.modifiers(), isInterface);
	}
}