Java Code Examples for org.eclipse.jdt.core.dom.ASTNode#ASSIGNMENT
The following examples show how to use
org.eclipse.jdt.core.dom.ASTNode#ASSIGNMENT .
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: RefactorProposalUtility.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
public static CUCorrectionProposal getAssignVariableProposal(CodeActionParams params, IInvocationContext context, boolean problemsAtLocation, Map formatterOptions, boolean returnAsCommand, IProblemLocationCore[] locations) throws CoreException { ASTNode node = context.getCoveringNode(); Statement statement = ASTResolving.findParentStatement(node); if (!(statement instanceof ExpressionStatement)) { return null; } ExpressionStatement expressionStatement = (ExpressionStatement) statement; Expression expression = expressionStatement.getExpression(); if (expression.getNodeType() == ASTNode.ASSIGNMENT) { return null; } ITypeBinding typeBinding = expression.resolveTypeBinding(); typeBinding = Bindings.normalizeTypeBinding(typeBinding); if (typeBinding == null) { return null; } if (containsMatchingProblem(locations, IProblem.UnusedObjectAllocation)) { return null; } final ICompilationUnit cu = context.getCompilationUnit(); int relevance; if (context.getSelectionLength() == 0) { relevance = IProposalRelevance.EXTRACT_LOCAL_ZERO_SELECTION; } else if (problemsAtLocation) { relevance = IProposalRelevance.EXTRACT_LOCAL_ERROR; } else { relevance = IProposalRelevance.EXTRACT_LOCAL; } if (returnAsCommand) { return new AssignToVariableAssistCommandProposal(cu, JavaCodeActionKind.REFACTOR_ASSIGN_VARIABLE, AssignToVariableAssistProposal.LOCAL, expressionStatement, typeBinding, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(ASSIGN_VARIABLE_COMMAND, params)); } else { return new AssignToVariableAssistProposal(cu, JavaCodeActionKind.REFACTOR_ASSIGN_VARIABLE, AssignToVariableAssistProposal.LOCAL, expressionStatement, typeBinding, relevance); } }
Example 2
Source File: RefactorProposalUtility.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
public static CUCorrectionProposal getAssignFieldProposal(CodeActionParams params, IInvocationContext context, boolean problemsAtLocation, Map formatterOptions, boolean returnAsCommand, IProblemLocationCore[] locations) throws CoreException { ASTNode node = context.getCoveringNode(); Statement statement = ASTResolving.findParentStatement(node); if (!(statement instanceof ExpressionStatement)) { return null; } ExpressionStatement expressionStatement = (ExpressionStatement) statement; Expression expression = expressionStatement.getExpression(); if (expression.getNodeType() == ASTNode.ASSIGNMENT) { return null; } ITypeBinding typeBinding = expression.resolveTypeBinding(); typeBinding = Bindings.normalizeTypeBinding(typeBinding); if (typeBinding == null) { return null; } if (containsMatchingProblem(locations, IProblem.UnusedObjectAllocation)) { return null; } final ICompilationUnit cu = context.getCompilationUnit(); ASTNode type = ASTResolving.findParentType(expression); if (type != null) { int relevance; if (context.getSelectionLength() == 0) { relevance = IProposalRelevance.EXTRACT_LOCAL_ZERO_SELECTION; } else if (problemsAtLocation) { relevance = IProposalRelevance.EXTRACT_LOCAL_ERROR; } else { relevance = IProposalRelevance.EXTRACT_LOCAL; } if (returnAsCommand) { return new AssignToVariableAssistCommandProposal(cu, JavaCodeActionKind.REFACTOR_ASSIGN_FIELD, AssignToVariableAssistProposal.FIELD, expressionStatement, typeBinding, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(ASSIGN_FIELD_COMMAND, params)); } else { return new AssignToVariableAssistProposal(cu, JavaCodeActionKind.REFACTOR_ASSIGN_FIELD, AssignToVariableAssistProposal.FIELD, expressionStatement, typeBinding, relevance); } } return null; }
Example 3
Source File: CastCorrectionProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private static boolean needsInnerParantheses(ASTNode nodeToCast) { int nodeType= nodeToCast.getNodeType(); // nodes have weaker precedence than cast return nodeType == ASTNode.INFIX_EXPRESSION || nodeType == ASTNode.CONDITIONAL_EXPRESSION || nodeType == ASTNode.ASSIGNMENT || nodeType == ASTNode.INSTANCEOF_EXPRESSION; }
Example 4
Source File: NecessaryParenthesesChecker.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static boolean expressionTypeNeedsParentheses(Expression expression) { int type= expression.getNodeType(); return type == ASTNode.INFIX_EXPRESSION || type == ASTNode.CONDITIONAL_EXPRESSION || type == ASTNode.PREFIX_EXPRESSION || type == ASTNode.POSTFIX_EXPRESSION || type == ASTNode.CAST_EXPRESSION || type == ASTNode.INSTANCEOF_EXPRESSION || type == ASTNode.ARRAY_CREATION || type == ASTNode.ASSIGNMENT; }
Example 5
Source File: UnusedCodeFix.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static boolean hasSideEffect(SimpleName reference) { ASTNode parent= reference.getParent(); while (parent instanceof QualifiedName) { parent= parent.getParent(); } if (parent instanceof FieldAccess) { parent= parent.getParent(); } ASTNode node= null; int nameParentType= parent.getNodeType(); if (nameParentType == ASTNode.ASSIGNMENT) { Assignment assignment= (Assignment) parent; node= assignment.getRightHandSide(); } else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) { SingleVariableDeclaration decl= (SingleVariableDeclaration)parent; node= decl.getInitializer(); if (node == null) return false; } else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) { node= parent; } else { return false; } ArrayList<Expression> sideEffects= new ArrayList<Expression>(); node.accept(new SideEffectFinder(sideEffects)); return sideEffects.size() > 0; }
Example 6
Source File: ASTResolving.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public static boolean isWriteAccess(Name selectedNode) { ASTNode curr= selectedNode; ASTNode parent= curr.getParent(); while (parent != null) { switch (parent.getNodeType()) { case ASTNode.QUALIFIED_NAME: if (((QualifiedName) parent).getQualifier() == curr) { return false; } break; case ASTNode.FIELD_ACCESS: if (((FieldAccess) parent).getExpression() == curr) { return false; } break; case ASTNode.SUPER_FIELD_ACCESS: break; case ASTNode.ASSIGNMENT: return ((Assignment) parent).getLeftHandSide() == curr; case ASTNode.VARIABLE_DECLARATION_FRAGMENT: case ASTNode.SINGLE_VARIABLE_DECLARATION: return ((VariableDeclaration) parent).getName() == curr; case ASTNode.POSTFIX_EXPRESSION: return true; case ASTNode.PREFIX_EXPRESSION: PrefixExpression.Operator op= ((PrefixExpression) parent).getOperator(); return op == PrefixExpression.Operator.DECREMENT || op == PrefixExpression.Operator.INCREMENT; default: return false; } curr= parent; parent= curr.getParent(); } return false; }
Example 7
Source File: QuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public static boolean getAssignToVariableProposals(IInvocationContext context, ASTNode node, IProblemLocation[] locations, Collection<ICommandAccess> resultingCollections) { Statement statement= ASTResolving.findParentStatement(node); if (!(statement instanceof ExpressionStatement)) { return false; } ExpressionStatement expressionStatement= (ExpressionStatement) statement; Expression expression= expressionStatement.getExpression(); if (expression.getNodeType() == ASTNode.ASSIGNMENT) { return false; // too confusing and not helpful } ITypeBinding typeBinding= expression.resolveTypeBinding(); typeBinding= Bindings.normalizeTypeBinding(typeBinding); if (typeBinding == null) { return false; } if (resultingCollections == null) { return true; } // don't add if already added as quick fix if (containsMatchingProblem(locations, IProblem.UnusedObjectAllocation)) return false; ICompilationUnit cu= context.getCompilationUnit(); AssignToVariableAssistProposal localProposal= new AssignToVariableAssistProposal(cu, AssignToVariableAssistProposal.LOCAL, expressionStatement, typeBinding, IProposalRelevance.ASSIGN_TO_LOCAL); localProposal.setCommandId(ASSIGN_TO_LOCAL_ID); resultingCollections.add(localProposal); ASTNode type= ASTResolving.findParentType(expression); if (type != null) { AssignToVariableAssistProposal fieldProposal= new AssignToVariableAssistProposal(cu, AssignToVariableAssistProposal.FIELD, expressionStatement, typeBinding, IProposalRelevance.ASSIGN_TO_FIELD); fieldProposal.setCommandId(ASSIGN_TO_FIELD_ID); resultingCollections.add(fieldProposal); } return true; }
Example 8
Source File: CastCorrectionProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static boolean needsInnerParantheses(ASTNode nodeToCast) { int nodeType= nodeToCast.getNodeType(); // nodes have weaker precedence than cast return nodeType == ASTNode.INFIX_EXPRESSION || nodeType == ASTNode.CONDITIONAL_EXPRESSION || nodeType == ASTNode.ASSIGNMENT || nodeType == ASTNode.INSTANCEOF_EXPRESSION; }
Example 9
Source File: CompilationUnitBuilder.java From j2cl with Apache License 2.0 | 4 votes |
private Expression convert(org.eclipse.jdt.core.dom.Expression expression) { switch (expression.getNodeType()) { case ASTNode.ARRAY_ACCESS: return convert((org.eclipse.jdt.core.dom.ArrayAccess) expression); case ASTNode.ARRAY_CREATION: return convert((org.eclipse.jdt.core.dom.ArrayCreation) expression); case ASTNode.ARRAY_INITIALIZER: return convert((org.eclipse.jdt.core.dom.ArrayInitializer) expression); case ASTNode.ASSIGNMENT: return convert((org.eclipse.jdt.core.dom.Assignment) expression); case ASTNode.BOOLEAN_LITERAL: return convert((org.eclipse.jdt.core.dom.BooleanLiteral) expression); case ASTNode.CAST_EXPRESSION: return convert((org.eclipse.jdt.core.dom.CastExpression) expression); case ASTNode.CHARACTER_LITERAL: return convert((org.eclipse.jdt.core.dom.CharacterLiteral) expression); case ASTNode.CLASS_INSTANCE_CREATION: return convert((org.eclipse.jdt.core.dom.ClassInstanceCreation) expression); case ASTNode.CONDITIONAL_EXPRESSION: return convert((org.eclipse.jdt.core.dom.ConditionalExpression) expression); case ASTNode.EXPRESSION_METHOD_REFERENCE: return convert((org.eclipse.jdt.core.dom.ExpressionMethodReference) expression); case ASTNode.CREATION_REFERENCE: return convert((org.eclipse.jdt.core.dom.CreationReference) expression); case ASTNode.TYPE_METHOD_REFERENCE: return convert((org.eclipse.jdt.core.dom.TypeMethodReference) expression); case ASTNode.SUPER_METHOD_REFERENCE: return convert((org.eclipse.jdt.core.dom.SuperMethodReference) expression); case ASTNode.FIELD_ACCESS: return convert((org.eclipse.jdt.core.dom.FieldAccess) expression); case ASTNode.INFIX_EXPRESSION: return convert((org.eclipse.jdt.core.dom.InfixExpression) expression); case ASTNode.INSTANCEOF_EXPRESSION: return convert((org.eclipse.jdt.core.dom.InstanceofExpression) expression); case ASTNode.LAMBDA_EXPRESSION: return convert((org.eclipse.jdt.core.dom.LambdaExpression) expression); case ASTNode.METHOD_INVOCATION: return convert((org.eclipse.jdt.core.dom.MethodInvocation) expression); case ASTNode.NULL_LITERAL: return NullLiteral.get(); case ASTNode.NUMBER_LITERAL: return convert((org.eclipse.jdt.core.dom.NumberLiteral) expression); case ASTNode.PARENTHESIZED_EXPRESSION: return convert((org.eclipse.jdt.core.dom.ParenthesizedExpression) expression); case ASTNode.POSTFIX_EXPRESSION: return convert((org.eclipse.jdt.core.dom.PostfixExpression) expression); case ASTNode.PREFIX_EXPRESSION: return convert((org.eclipse.jdt.core.dom.PrefixExpression) expression); case ASTNode.QUALIFIED_NAME: return convert((org.eclipse.jdt.core.dom.QualifiedName) expression); case ASTNode.SIMPLE_NAME: return convert((org.eclipse.jdt.core.dom.SimpleName) expression); case ASTNode.STRING_LITERAL: return convert((org.eclipse.jdt.core.dom.StringLiteral) expression); case ASTNode.SUPER_FIELD_ACCESS: return convert((org.eclipse.jdt.core.dom.SuperFieldAccess) expression); case ASTNode.SUPER_METHOD_INVOCATION: return convert((org.eclipse.jdt.core.dom.SuperMethodInvocation) expression); case ASTNode.THIS_EXPRESSION: return convert((org.eclipse.jdt.core.dom.ThisExpression) expression); case ASTNode.TYPE_LITERAL: return convert((org.eclipse.jdt.core.dom.TypeLiteral) expression); case ASTNode.VARIABLE_DECLARATION_EXPRESSION: return convert((org.eclipse.jdt.core.dom.VariableDeclarationExpression) expression); default: throw internalCompilerError( "Unexpected type for Expression: %s", expression.getClass().getName()); } }
Example 10
Source File: JavaASTVisitor.java From SnowGraph with Apache License 2.0 | 4 votes |
private void parseExpression(MethodInfo methodInfo, Expression expression) { if (expression == null) { return; }//System.out.println(expression.toString()+" "+Annotation.nodeClassForType(expression.getNodeType())); if (expression.getNodeType() == ASTNode.ARRAY_INITIALIZER) { List<Expression> expressions = ((ArrayInitializer) expression).expressions(); for (Expression expression2 : expressions) { parseExpression(methodInfo, expression2); } } if (expression.getNodeType() == ASTNode.CAST_EXPRESSION) { parseExpression(methodInfo, ((CastExpression) expression).getExpression()); } if (expression.getNodeType() == ASTNode.CONDITIONAL_EXPRESSION) { parseExpression(methodInfo, ((ConditionalExpression) expression).getExpression()); parseExpression(methodInfo, ((ConditionalExpression) expression).getElseExpression()); parseExpression(methodInfo, ((ConditionalExpression) expression).getThenExpression()); } if (expression.getNodeType() == ASTNode.INFIX_EXPRESSION) { parseExpression(methodInfo, ((InfixExpression) expression).getLeftOperand()); parseExpression(methodInfo, ((InfixExpression) expression).getRightOperand()); } if (expression.getNodeType() == ASTNode.INSTANCEOF_EXPRESSION) { parseExpression(methodInfo, ((InstanceofExpression) expression).getLeftOperand()); } if (expression.getNodeType() == ASTNode.PARENTHESIZED_EXPRESSION) { parseExpression(methodInfo, ((ParenthesizedExpression) expression).getExpression()); } if (expression.getNodeType() == ASTNode.POSTFIX_EXPRESSION) { parseExpression(methodInfo, ((PostfixExpression) expression).getOperand()); } if (expression.getNodeType() == ASTNode.PREFIX_EXPRESSION) { parseExpression(methodInfo, ((PrefixExpression) expression).getOperand()); } if (expression.getNodeType() == ASTNode.THIS_EXPRESSION) { parseExpression(methodInfo, ((ThisExpression) expression).getQualifier()); } if (expression.getNodeType() == ASTNode.METHOD_INVOCATION) { List<Expression> arguments = ((MethodInvocation) expression).arguments(); IMethodBinding methodBinding = ((MethodInvocation) expression).resolveMethodBinding(); if (methodBinding != null) methodInfo.methodCalls.add(methodBinding); for (Expression exp : arguments) parseExpression(methodInfo, exp); parseExpression(methodInfo, ((MethodInvocation) expression).getExpression()); } if (expression.getNodeType() == ASTNode.ASSIGNMENT) { parseExpression(methodInfo, ((Assignment) expression).getLeftHandSide()); parseExpression(methodInfo, ((Assignment) expression).getRightHandSide()); } if (expression.getNodeType() == ASTNode.QUALIFIED_NAME) { if (((QualifiedName) expression).getQualifier().resolveTypeBinding() != null) { String name = ((QualifiedName) expression).getQualifier().resolveTypeBinding().getQualifiedName() + "." + ((QualifiedName) expression).getName().getIdentifier(); methodInfo.fieldUsesSet.add(name); } parseExpression(methodInfo, ((QualifiedName) expression).getQualifier()); } }
Example 11
Source File: GetterSetterUtil.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Converts an assignment, postfix expression or prefix expression into an assignable equivalent expression using the getter. * * @param node the assignment/prefix/postfix node * @param astRewrite the astRewrite to use * @param getterExpression the expression to insert for read accesses or <code>null</code> if such an expression does not exist * @param variableType the type of the variable that the result will be assigned to * @param is50OrHigher <code>true</code> if a 5.0 or higher environment can be used * @return an expression that can be assigned to the type variableType with node being replaced by a equivalent expression using the getter */ public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, Expression getterExpression, ITypeBinding variableType, boolean is50OrHigher) { InfixExpression.Operator op= null; AST ast= astRewrite.getAST(); if (isNotInBlock(node)) return null; if (node.getNodeType() == ASTNode.ASSIGNMENT) { Assignment assignment= ((Assignment) node); Expression rightHandSide= assignment.getRightHandSide(); Expression copiedRightOp= (Expression) astRewrite.createCopyTarget(rightHandSide); if (assignment.getOperator() == Operator.ASSIGN) { ITypeBinding rightHandSideType= rightHandSide.resolveTypeBinding(); copiedRightOp= createNarrowCastIfNessecary(copiedRightOp, rightHandSideType, ast, variableType, is50OrHigher); return copiedRightOp; } if (getterExpression != null) { InfixExpression infix= ast.newInfixExpression(); infix.setLeftOperand(getterExpression); infix.setOperator(ASTNodes.convertToInfixOperator(assignment.getOperator())); ITypeBinding infixType= infix.resolveTypeBinding(); if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, infix, variableType)) { ParenthesizedExpression p= ast.newParenthesizedExpression(); p.setExpression(copiedRightOp); copiedRightOp= p; } infix.setRightOperand(copiedRightOp); return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher); } } else if (node.getNodeType() == ASTNode.POSTFIX_EXPRESSION) { PostfixExpression po= (PostfixExpression) node; if (po.getOperator() == PostfixExpression.Operator.INCREMENT) op= InfixExpression.Operator.PLUS; if (po.getOperator() == PostfixExpression.Operator.DECREMENT) op= InfixExpression.Operator.MINUS; } else if (node.getNodeType() == ASTNode.PREFIX_EXPRESSION) { PrefixExpression pe= (PrefixExpression) node; if (pe.getOperator() == PrefixExpression.Operator.INCREMENT) op= InfixExpression.Operator.PLUS; if (pe.getOperator() == PrefixExpression.Operator.DECREMENT) op= InfixExpression.Operator.MINUS; } if (op != null && getterExpression != null) { return createInfixInvocationFromPostPrefixExpression(op, getterExpression, ast, variableType, is50OrHigher); } return null; }
Example 12
Source File: UnusedCodeFix.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Remove the field or variable declaration including the initializer. * @param rewrite the AST rewriter to use * @param reference a reference to the variable to remove * @param group the text edit group to use */ private void removeVariableReferences(ASTRewrite rewrite, SimpleName reference, TextEditGroup group) { ASTNode parent= reference.getParent(); while (parent instanceof QualifiedName) { parent= parent.getParent(); } if (parent instanceof FieldAccess) { parent= parent.getParent(); } int nameParentType= parent.getNodeType(); if (nameParentType == ASTNode.ASSIGNMENT) { Assignment assignment= (Assignment) parent; Expression rightHand= assignment.getRightHandSide(); ASTNode assignParent= assignment.getParent(); if (assignParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT && rightHand.getNodeType() != ASTNode.ASSIGNMENT) { removeVariableWithInitializer(rewrite, rightHand, assignParent, group); } else { rewrite.replace(assignment, rewrite.createCopyTarget(rightHand), group); } } else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) { rewrite.remove(parent, group); } else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) { VariableDeclarationFragment frag= (VariableDeclarationFragment) parent; ASTNode varDecl= frag.getParent(); List<VariableDeclarationFragment> fragments; if (varDecl instanceof VariableDeclarationExpression) { fragments= ((VariableDeclarationExpression) varDecl).fragments(); } else if (varDecl instanceof FieldDeclaration) { fragments= ((FieldDeclaration) varDecl).fragments(); } else { fragments= ((VariableDeclarationStatement) varDecl).fragments(); } Expression initializer = frag.getInitializer(); ArrayList<Expression> sideEffects= new ArrayList<Expression>(); if (initializer != null) { initializer.accept(new SideEffectFinder(sideEffects)); } boolean sideEffectInitializer= sideEffects.size() > 0; if (fragments.size() == fUnusedNames.length) { if (fForceRemove) { rewrite.remove(varDecl, group); return; } if (parent.getParent() instanceof FieldDeclaration) { rewrite.remove(varDecl, group); return; } if (sideEffectInitializer) { Statement[] wrapped= new Statement[sideEffects.size()]; for (int i= 0; i < wrapped.length; i++) { Expression sideEffect= sideEffects.get(i); Expression movedInit= (Expression) rewrite.createMoveTarget(sideEffect); wrapped[i]= rewrite.getAST().newExpressionStatement(movedInit); } StatementRewrite statementRewrite= new StatementRewrite(rewrite, new ASTNode[] { varDecl }); statementRewrite.replace(wrapped, group); } else { rewrite.remove(varDecl, group); } } else { if (fForceRemove) { rewrite.remove(frag, group); return; } //multiple declarations in one line ASTNode declaration = parent.getParent(); if (declaration instanceof FieldDeclaration) { rewrite.remove(frag, group); return; } if (declaration instanceof VariableDeclarationStatement) { splitUpDeclarations(rewrite, group, frag, (VariableDeclarationStatement) declaration, sideEffects); rewrite.remove(frag, group); return; } if (declaration instanceof VariableDeclarationExpression) { //keep constructors and method invocations if (!sideEffectInitializer){ rewrite.remove(frag, group); } } } } else if (nameParentType == ASTNode.POSTFIX_EXPRESSION || nameParentType == ASTNode.PREFIX_EXPRESSION) { Expression expression= (Expression)parent; ASTNode expressionParent= expression.getParent(); if (expressionParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT) { removeStatement(rewrite, expressionParent, group); } else { rewrite.remove(expression, group); } } }
Example 13
Source File: RemoveDeclarationCorrectionProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Remove the field or variable declaration including the initializer. * @param rewrite the ast rewrite * @param reference the reference */ private void removeVariableReferences(ASTRewrite rewrite, SimpleName reference) { ASTNode parent= reference.getParent(); while (parent instanceof QualifiedName) { parent= parent.getParent(); } if (parent instanceof FieldAccess) { parent= parent.getParent(); } int nameParentType= parent.getNodeType(); if (nameParentType == ASTNode.ASSIGNMENT) { Assignment assignment= (Assignment) parent; Expression rightHand= assignment.getRightHandSide(); ASTNode assignParent= assignment.getParent(); if (assignParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT && rightHand.getNodeType() != ASTNode.ASSIGNMENT) { removeVariableWithInitializer(rewrite, rightHand, assignParent); } else { rewrite.replace(assignment, rewrite.createCopyTarget(rightHand), null); } } else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) { rewrite.remove(parent, null); } else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) { VariableDeclarationFragment frag= (VariableDeclarationFragment) parent; ASTNode varDecl= frag.getParent(); List<VariableDeclarationFragment> fragments; if (varDecl instanceof VariableDeclarationExpression) { fragments= ((VariableDeclarationExpression) varDecl).fragments(); } else if (varDecl instanceof FieldDeclaration) { fragments= ((FieldDeclaration) varDecl).fragments(); } else { fragments= ((VariableDeclarationStatement) varDecl).fragments(); } if (fragments.size() == 1) { rewrite.remove(varDecl, null); } else { rewrite.remove(frag, null); // don't try to preserve } } }