Java Code Examples for org.eclipse.jdt.core.dom.rewrite.ASTRewrite#rewriteAST()

The following examples show how to use org.eclipse.jdt.core.dom.rewrite.ASTRewrite#rewriteAST() . 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: DeleteChangeCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static TextChange addTextEditFromRewrite(TextChangeManager manager, ICompilationUnit cu, ASTRewrite rewrite) throws CoreException {
	try {
		ITextFileBuffer buffer= RefactoringFileBuffers.acquire(cu);
		TextEdit resultingEdits= rewrite.rewriteAST(buffer.getDocument(), cu.getJavaProject().getOptions(true));
		TextChange textChange= manager.get(cu);
		if (textChange instanceof TextFileChange) {
			TextFileChange tfc= (TextFileChange) textChange;
			tfc.setSaveMode(TextFileChange.KEEP_SAVE_STATE);
		}
		String message= RefactoringCoreMessages.DeleteChangeCreator_1;
		TextChangeCompatibility.addTextEdit(textChange, message, resultingEdits);
		return textChange;
	} finally {
		RefactoringFileBuffers.release(cu);
	}
}
 
Example 2
Source File: ASTRewriteCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException {
	super.addEdits(document, editRoot);
	ASTRewrite rewrite= getRewrite();
	if (rewrite != null) {
		try {
			TextEdit edit= rewrite.rewriteAST();
			editRoot.addChild(edit);
		} catch (IllegalArgumentException e) {
			throw new CoreException(StatusFactory.newErrorStatus("Invalid AST rewriter", e));
		}
	}
	if (fImportRewrite != null) {
		editRoot.addChild(fImportRewrite.rewriteImports(new NullProgressMonitor()));
	}
}
 
Example 3
Source File: MoveMethodRefactoring.java    From JDeodorant with MIT License 6 votes vote down vote up
private void removeAdditionalMethodsFromSourceClass() {
	Set<MethodDeclaration> methodsToBeMoved = new LinkedHashSet<MethodDeclaration>(additionalMethodsToBeMoved.values());
	for(MethodDeclaration methodDeclaration : methodsToBeMoved) {
		ASTRewrite sourceRewriter = ASTRewrite.create(sourceCompilationUnit.getAST());
		ListRewrite sourceClassBodyRewrite = sourceRewriter.getListRewrite(sourceTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
		sourceClassBodyRewrite.remove(methodDeclaration, null);
		try {
			TextEdit sourceEdit = sourceRewriter.rewriteAST();
			sourceMultiTextEdit.addChild(sourceEdit);
			sourceCompilationUnitChange.addTextEditGroup(new TextEditGroup("Remove additional moved method", new TextEdit[] {sourceEdit}));
		}
		catch(JavaModelException javaModelException) {
			javaModelException.printStackTrace();
		}
	}
}
 
Example 4
Source File: JavaStatementPostfixContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Adds a new field to the {@link AST} using the given type and variable name. The method
 * returns a {@link TextEdit} which can then be applied using the {@link #applyTextEdit(TextEdit)} method.
 * 
 * @param type
 * @param varName
 * @return a {@link TextEdit} which represents the changes which would be made, or <code>null</code> if the field
 * can not be created.
 */
public TextEdit addField(String type, String varName, boolean publicField, boolean staticField, boolean finalField, String value) {
	
	if (isReadOnly())
		return null;

	if (!domInitialized)
		initDomAST();
	
	boolean isStatic = isBodyStatic();
	int modifiers = (!publicField) ? Modifier.PRIVATE : Modifier.PUBLIC;
	if (isStatic || staticField) {
		modifiers |= Modifier.STATIC;
	}
	if (finalField) {
		modifiers |= Modifier.FINAL;
	}
	
	ASTRewrite rewrite= ASTRewrite.create(parentDeclaration.getAST());
	
	VariableDeclarationFragment newDeclFrag = addFieldDeclaration(rewrite, parentDeclaration, modifiers, varName, type, value);

	TextEdit te = rewrite.rewriteAST(getDocument(), null);
	return te;
}
 
Example 5
Source File: CreateElementInCUOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected void generateNewCompilationUnitAST(ICompilationUnit cu) throws JavaModelException {
	this.cuAST = parse(cu);

	AST ast = this.cuAST.getAST();
	ASTRewrite rewriter = ASTRewrite.create(ast);
	ASTNode child = generateElementAST(rewriter, cu);
	if (child != null) {
		ASTNode parent = ((JavaElement) getParentElement()).findNode(this.cuAST);
		if (parent == null)
			parent = this.cuAST;
		insertASTNode(rewriter, parent, child);
		TextEdit edits = rewriter.rewriteAST();
		applyTextEdit(cu, edits);
	}
	worked(1);
}
 
Example 6
Source File: ReplaceTypeCodeWithStateStrategy.java    From JDeodorant with MIT License 6 votes vote down vote up
private void initializeReturnedVariableDeclaration() {
	ASTRewrite sourceRewriter = ASTRewrite.create(sourceTypeDeclaration.getAST());
	AST contextAST = sourceTypeDeclaration.getAST();
	if(returnedVariable != null) {
		IVariableBinding returnedVariableBinding = returnedVariable.resolveBinding();
		if(returnedVariable instanceof VariableDeclarationFragment && !returnedVariableBinding.isField()) {
			VariableDeclarationFragment variableDeclarationFragment = (VariableDeclarationFragment)returnedVariable;
			if(variableDeclarationFragment.getInitializer() == null) {
				Expression defaultValue = generateDefaultValue(sourceRewriter, contextAST, returnedVariableBinding.getType());
				sourceRewriter.set(variableDeclarationFragment, VariableDeclarationFragment.INITIALIZER_PROPERTY, defaultValue, null);
			}
		}
	}
	try {
		TextEdit sourceEdit = sourceRewriter.rewriteAST();
		ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
		CompilationUnitChange change = compilationUnitChanges.get(sourceICompilationUnit);
		change.getEdit().addChild(sourceEdit);
		change.addTextEditGroup(new TextEditGroup("Initialize returned variable", new TextEdit[] {sourceEdit}));
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
}
 
Example 7
Source File: SortElementsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Calculates the required text edits to sort the <code>unit</code>
 * @param group
 * @return the edit or null if no sorting is required
 */
public TextEdit calculateEdit(org.eclipse.jdt.core.dom.CompilationUnit unit, TextEditGroup group) throws JavaModelException {
	if (this.elementsToProcess.length != 1)
		throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.NO_ELEMENTS_TO_PROCESS));

	if (!(this.elementsToProcess[0] instanceof ICompilationUnit))
		throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, this.elementsToProcess[0]));

	try {
		beginTask(Messages.operation_sortelements, getMainAmountOfWork());

		ICompilationUnit cu= (ICompilationUnit)this.elementsToProcess[0];
		String content= cu.getBuffer().getContents();
		ASTRewrite rewrite= sortCompilationUnit(unit, group);
		if (rewrite == null) {
			return null;
		}

		Document document= new Document(content);
		return rewrite.rewriteAST(document, cu.getJavaProject().getOptions(true));
	} finally {
		done();
	}
}
 
Example 8
Source File: DeleteChangeCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static TextChange addTextEditFromRewrite(TextChangeManager manager, ICompilationUnit cu, ASTRewrite rewrite) throws CoreException {
	try {
		ITextFileBuffer buffer= RefactoringFileBuffers.acquire(cu);
		TextEdit resultingEdits= rewrite.rewriteAST(buffer.getDocument(), cu.getJavaProject().getOptions(true));
		TextChange textChange= manager.get(cu);
		if (textChange instanceof TextFileChange) {
			TextFileChange tfc= (TextFileChange) textChange;
			tfc.setSaveMode(TextFileChange.KEEP_SAVE_STATE);
		}
		String message= RefactoringCoreMessages.DeleteChangeCreator_1;
		TextChangeCompatibility.addTextEdit(textChange, message, resultingEdits);
		return textChange;
	} finally {
		RefactoringFileBuffers.release(cu);
	}
}
 
Example 9
Source File: CopyResourceElementsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Updates the content of <code>cu</code>, modifying the type name and/or package
 * declaration as necessary.
 *
 * @return an AST rewrite or null if no rewrite needed
 */
private TextEdit updateContent(ICompilationUnit cu, PackageFragment dest, String newName) throws JavaModelException {
	String[] currPackageName = ((PackageFragment) cu.getParent()).names;
	String[] destPackageName = dest.names;
	if (Util.equalArraysOrNull(currPackageName, destPackageName) && newName == null) {
		return null; //nothing to change
	} else {
		// ensure cu is consistent (noop if already consistent)
		cu.makeConsistent(this.progressMonitor);
		this.parser.setSource(cu);
		CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor);
		AST ast = astCU.getAST();
		ASTRewrite rewrite = ASTRewrite.create(ast);
		updateTypeName(cu, astCU, cu.getElementName(), newName, rewrite);
		updatePackageStatement(astCU, destPackageName, rewrite, cu);
		return rewrite.rewriteAST();
	}
}
 
Example 10
Source File: ASTRewriteCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException {
	super.addEdits(document, editRoot);
	ASTRewrite rewrite= getRewrite();
	if (rewrite != null) {
		try {
			TextEdit edit= rewrite.rewriteAST();
			editRoot.addChild(edit);
		} catch (IllegalArgumentException e) {
			throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
		}
	}
	if (fImportRewrite != null) {
		editRoot.addChild(fImportRewrite.rewriteImports(new NullProgressMonitor()));
	}
}
 
Example 11
Source File: PasteAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
	ASTParser p= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
	p.setSource(getDestinationCu());
	CompilationUnit cuNode= (CompilationUnit) p.createAST(pm);
	ASTRewrite rewrite= ASTRewrite.create(cuNode.getAST());
	TypedSource source= null;
	for (int i= fSources.length - 1; i >= 0; i--) {
		source= fSources[i];
		final ASTNode destination= getDestinationNodeForSourceElement(fDestination, source.getType(), cuNode);
		if (destination != null) {
			if (destination instanceof CompilationUnit)
				insertToCu(rewrite, createNewNodeToInsertToCu(source, rewrite), (CompilationUnit) destination);
			else if (destination instanceof AbstractTypeDeclaration)
				insertToType(rewrite, createNewNodeToInsertToType(source, rewrite), (AbstractTypeDeclaration) destination);
		}
	}
	final CompilationUnitChange result= new CompilationUnitChange(ReorgMessages.PasteAction_change_name, getDestinationCu());
	try {
		ITextFileBuffer buffer= RefactoringFileBuffers.acquire(getDestinationCu());
		TextEdit rootEdit= rewrite.rewriteAST(buffer.getDocument(), fDestination.getJavaProject().getOptions(true));
		if (getDestinationCu().isWorkingCopy())
			result.setSaveMode(TextFileChange.LEAVE_DIRTY);
		TextChangeCompatibility.addTextEdit(result, ReorgMessages.PasteAction_edit_name, rootEdit);
	} finally {
		RefactoringFileBuffers.release(getDestinationCu());
	}
	return result;
}
 
Example 12
Source File: ReplaceTypeCodeWithStateStrategy.java    From JDeodorant with MIT License 5 votes vote down vote up
private void replacePrimitiveStateField() {
	ASTRewrite sourceRewriter = ASTRewrite.create(sourceTypeDeclaration.getAST());
	AST contextAST = sourceTypeDeclaration.getAST();
	ListRewrite contextBodyRewrite = sourceRewriter.getListRewrite(sourceTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
	FieldDeclaration[] fieldDeclarations = sourceTypeDeclaration.getFields();
	for(FieldDeclaration fieldDeclaration : fieldDeclarations) {
		List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
		for(VariableDeclarationFragment fragment : fragments) {
			if(fragment.equals(typeCheckElimination.getTypeField())) {
				if(fragments.size() == 1) {
					ListRewrite fragmentsRewriter = sourceRewriter.getListRewrite(fieldDeclaration, FieldDeclaration.FRAGMENTS_PROPERTY);
					fragmentsRewriter.remove(fragment, null);
					VariableDeclarationFragment typeFragment = createStateFieldVariableDeclarationFragment(sourceRewriter, contextAST);
					fragmentsRewriter.insertLast(typeFragment, null);
					sourceRewriter.set(fieldDeclaration, FieldDeclaration.TYPE_PROPERTY, contextAST.newSimpleName(abstractClassName), null);
				}
			}
		}
	}
	try {
		TextEdit sourceEdit = sourceRewriter.rewriteAST();
		ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
		CompilationUnitChange change = compilationUnitChanges.get(sourceICompilationUnit);
		change.getEdit().addChild(sourceEdit);
		change.addTextEditGroup(new TextEditGroup("Replace primitive type with State type", new TextEdit[] {sourceEdit}));
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
}
 
Example 13
Source File: DeleteElementsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void deleteElement(IJavaElement elementToRemove, ICompilationUnit cu) throws JavaModelException {
	// ensure cu is consistent (noop if already consistent)
	cu.makeConsistent(this.progressMonitor);
	this.parser.setSource(cu);
	CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor);
	ASTNode node = ((JavaElement) elementToRemove).findNode(astCU);
	if (node == null)
		Assert.isTrue(false, "Failed to locate " + elementToRemove.getElementName() + " in " + cu.getElementName()); //$NON-NLS-1$//$NON-NLS-2$
	AST ast = astCU.getAST();
	ASTRewrite rewriter = ASTRewrite.create(ast);
	rewriter.remove(node, null);
		TextEdit edits = rewriter.rewriteAST();
		applyTextEdit(cu, edits);
}
 
Example 14
Source File: CompilationUnitSourceSetter.java    From SparkBuilderGenerator with MIT License 5 votes vote down vote up
public void commitCodeChange(ICompilationUnit iCompilationUnit, ASTRewrite rewriter) {
    try {
        Document document = new Document(iCompilationUnit.getSource());
        TextEdit edits = rewriter.rewriteAST(document, null);
        edits.apply(document);
        iCompilationUnit.getBuffer().setContents(document.get());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 15
Source File: ReplaceTypeCodeWithStateStrategy.java    From JDeodorant with MIT License 5 votes vote down vote up
private void removePrimitiveStateField() {
	ASTRewrite sourceRewriter = ASTRewrite.create(sourceTypeDeclaration.getAST());
	AST contextAST = sourceTypeDeclaration.getAST();
	ListRewrite contextBodyRewrite = sourceRewriter.getListRewrite(sourceTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
	FieldDeclaration[] fieldDeclarations = sourceTypeDeclaration.getFields();
	for(FieldDeclaration fieldDeclaration : fieldDeclarations) {
		List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
		for(VariableDeclarationFragment fragment : fragments) {
			if(fragment.equals(typeCheckElimination.getTypeField())) {
				if(fragments.size() == 1) {
					contextBodyRewrite.remove(fragment.getParent(), null);
				}
				else {
					ListRewrite fragmentRewrite = sourceRewriter.getListRewrite(fragment.getParent(), FieldDeclaration.FRAGMENTS_PROPERTY);
					fragmentRewrite.remove(fragment, null);
				}
			}
		}
	}
	try {
		TextEdit sourceEdit = sourceRewriter.rewriteAST();
		ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
		CompilationUnitChange change = compilationUnitChanges.get(sourceICompilationUnit);
		change.getEdit().addChild(sourceEdit);
		change.addTextEditGroup(new TextEditGroup("Remove primitive field holding the current state", new TextEdit[] {sourceEdit}));
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
}
 
Example 16
Source File: BugResolution.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private IRegion rewriteCompilationUnit(ASTRewrite rewrite, IDocument doc, ICompilationUnit originalUnit)
        throws JavaModelException, BadLocationException {
    TextEdit edits = rewrite.rewriteAST(doc, originalUnit.getJavaProject().getOptions(true));
    edits.apply(doc);

    originalUnit.getBuffer().setContents(doc.get());
    originalUnit.commitWorkingCopy(false, monitor);
    return edits.getRegion();
}
 
Example 17
Source File: ChangeUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static void convertPackageUpdateEdit(ICompilationUnit cu, String newPkgName, WorkspaceEdit rootEdit) throws JavaModelException {
	CompilationUnit unit = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL).parse(cu, true);		
	ASTRewrite rewrite = ASTRewrite.create(unit.getAST());
	if (updatePackageStatement(unit, newPkgName, rewrite, cu)) {
		TextEdit textEdit = rewrite.rewriteAST();
		convertTextEdit(rootEdit, cu, textEdit);
	}
}
 
Example 18
Source File: NewCUProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private TextEdit constructEnclosingTypeEdit(ICompilationUnit icu) throws CoreException {
	ASTParser astParser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
	astParser.setSource(icu);
	CompilationUnit cu = (CompilationUnit) astParser.createAST(null);
	TypeDeclaration enclosingDecl = findEnclosingTypeDeclaration(cu, fTypeContainer.getElementName());
	AST ast = cu.getAST();
	ASTRewrite rewrite = ASTRewrite.create(ast);
	final AbstractTypeDeclaration newDeclaration;
	switch (fTypeKind) {
		case K_CLASS:
			newDeclaration = ast.newTypeDeclaration();
			((TypeDeclaration) newDeclaration).setInterface(false);
			break;
		case K_INTERFACE:
			newDeclaration = ast.newTypeDeclaration();
			((TypeDeclaration) newDeclaration).setInterface(true);
			break;
		case K_ENUM:
			newDeclaration = ast.newEnumDeclaration();
			break;
		case K_ANNOTATION:
			newDeclaration = ast.newAnnotationTypeDeclaration();
			break;
		default:
			return null;
	}
	newDeclaration.setJavadoc(null);
	newDeclaration.setName(ast.newSimpleName(ASTNodes.getSimpleNameIdentifier(fNode)));
	newDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
	if (isParameterizedType(fTypeKind, fNode)) {
		addTypeParameters((TypeDeclaration) newDeclaration);
	}

	ListRewrite lrw = rewrite.getListRewrite(enclosingDecl, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
	lrw.insertLast(newDeclaration, null);
	return rewrite.rewriteAST();
}
 
Example 19
Source File: SourceProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public TextEdit getDeleteEdit() {
	final ASTRewrite rewriter= ASTRewrite.create(fDeclaration.getAST());
	rewriter.remove(fDeclaration, null);
	return rewriter.rewriteAST(fDocument, fTypeRoot.getJavaProject().getOptions(true));
}
 
Example 20
Source File: PolymorphismRefactoring.java    From JDeodorant with MIT License 4 votes vote down vote up
protected void generateSettersForAssignedFields() {
	AST contextAST = sourceTypeDeclaration.getAST();
	Set<VariableDeclarationFragment> assignedFields = new LinkedHashSet<VariableDeclarationFragment>();
	assignedFields.addAll(typeCheckElimination.getAssignedFields());
	assignedFields.addAll(typeCheckElimination.getSuperAssignedFields());
	for(VariableDeclarationFragment fragment : assignedFields) {
		IMethodBinding setterMethodBinding = null;
		if(typeCheckElimination.getSuperAssignedFields().contains(fragment)) {
			for(IVariableBinding fieldBinding : typeCheckElimination.getSuperAssignedFieldBindings()) {
				if(fieldBinding.isEqualTo(fragment.resolveBinding())) {
					setterMethodBinding = typeCheckElimination.getSetterMethodBindingOfSuperAssignedField(fieldBinding);
					break;
				}
			}
		}
		else {
			setterMethodBinding = findSetterMethodInContext(fragment.resolveBinding());
		}
		if(setterMethodBinding == null) {
			FieldDeclaration fieldDeclaration = (FieldDeclaration)fragment.getParent();
			if(!fragment.equals(typeCheckElimination.getTypeField())) {
				ASTRewrite sourceRewriter = ASTRewrite.create(sourceTypeDeclaration.getAST());
				MethodDeclaration newMethodDeclaration = contextAST.newMethodDeclaration();
				sourceRewriter.set(newMethodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, contextAST.newPrimitiveType(PrimitiveType.VOID), null);
				ListRewrite methodDeclarationModifiersRewrite = sourceRewriter.getListRewrite(newMethodDeclaration, MethodDeclaration.MODIFIERS2_PROPERTY);
				methodDeclarationModifiersRewrite.insertLast(contextAST.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD), null);
				String methodName = fragment.getName().getIdentifier();
				methodName = "set" + methodName.substring(0,1).toUpperCase() + methodName.substring(1,methodName.length());
				sourceRewriter.set(newMethodDeclaration, MethodDeclaration.NAME_PROPERTY, contextAST.newSimpleName(methodName), null);
				ListRewrite methodDeclarationParametersRewrite = sourceRewriter.getListRewrite(newMethodDeclaration, MethodDeclaration.PARAMETERS_PROPERTY);
				SingleVariableDeclaration parameter = contextAST.newSingleVariableDeclaration();
				sourceRewriter.set(parameter, SingleVariableDeclaration.TYPE_PROPERTY, fieldDeclaration.getType(), null);
				sourceRewriter.set(parameter, SingleVariableDeclaration.NAME_PROPERTY, fragment.getName(), null);
				methodDeclarationParametersRewrite.insertLast(parameter, null);
				Block methodDeclarationBody = contextAST.newBlock();
				ListRewrite methodDeclarationBodyStatementsRewrite = sourceRewriter.getListRewrite(methodDeclarationBody, Block.STATEMENTS_PROPERTY);
				Assignment assignment = contextAST.newAssignment();
				sourceRewriter.set(assignment, Assignment.RIGHT_HAND_SIDE_PROPERTY, fragment.getName(), null);
				sourceRewriter.set(assignment, Assignment.OPERATOR_PROPERTY, Assignment.Operator.ASSIGN, null);
				FieldAccess fieldAccess = contextAST.newFieldAccess();
				sourceRewriter.set(fieldAccess, FieldAccess.EXPRESSION_PROPERTY, contextAST.newThisExpression(), null);
				sourceRewriter.set(fieldAccess, FieldAccess.NAME_PROPERTY, fragment.getName(), null);
				sourceRewriter.set(assignment, Assignment.LEFT_HAND_SIDE_PROPERTY, fieldAccess, null);
				ExpressionStatement expressionStatement = contextAST.newExpressionStatement(assignment);
				methodDeclarationBodyStatementsRewrite.insertLast(expressionStatement, null);
				sourceRewriter.set(newMethodDeclaration, MethodDeclaration.BODY_PROPERTY, methodDeclarationBody, null);
				ListRewrite contextBodyRewrite = sourceRewriter.getListRewrite(sourceTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
				contextBodyRewrite.insertLast(newMethodDeclaration, null);
				try {
					TextEdit sourceEdit = sourceRewriter.rewriteAST();
					ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
					CompilationUnitChange change = compilationUnitChanges.get(sourceICompilationUnit);
					change.getEdit().addChild(sourceEdit);
					change.addTextEditGroup(new TextEditGroup("Create setter method for assigned field", new TextEdit[] {sourceEdit}));
				} catch (JavaModelException e) {
					e.printStackTrace();
				}
			}
		}
	}
}