Search in sources :

Example 56 with VariableDeclarationFragment

use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project flux by eclipse.

the class QuickAssistProcessor method getSplitVariableProposals.

private static boolean getSplitVariableProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    VariableDeclarationFragment fragment;
    if (node instanceof VariableDeclarationFragment) {
        fragment = (VariableDeclarationFragment) node;
    } else if (node.getLocationInParent() == VariableDeclarationFragment.NAME_PROPERTY) {
        fragment = (VariableDeclarationFragment) node.getParent();
    } else {
        return false;
    }
    if (fragment.getInitializer() == null) {
        return false;
    }
    Statement statement;
    ASTNode fragParent = fragment.getParent();
    if (fragParent instanceof VariableDeclarationStatement) {
        statement = (VariableDeclarationStatement) fragParent;
    } else if (fragParent instanceof VariableDeclarationExpression) {
        if (fragParent.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
            return false;
        }
        statement = (Statement) fragParent.getParent();
    } else {
        return false;
    }
    // statement is ForStatement or VariableDeclarationStatement
    ASTNode statementParent = statement.getParent();
    StructuralPropertyDescriptor property = statement.getLocationInParent();
    if (!property.isChildListProperty()) {
        return false;
    }
    List<? extends ASTNode> list = ASTNodes.getChildListProperty(statementParent, (ChildListPropertyDescriptor) property);
    if (resultingCollections == null) {
        return true;
    }
    AST ast = statement.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    String label = CorrectionMessages.QuickAssistProcessor_splitdeclaration_description;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SPLIT_VARIABLE_DECLARATION);
    boolean commandConflict = false;
    for (Iterator<ICommandAccess> iterator = resultingCollections.iterator(); iterator.hasNext(); ) {
        Object completionProposal = iterator.next();
        if (completionProposal instanceof ChangeCorrectionProposal) {
            if (SPLIT_JOIN_VARIABLE_DECLARATION_ID.equals(((ChangeCorrectionProposal) completionProposal).getCommandId())) {
                commandConflict = true;
            }
        }
    }
    if (!commandConflict) {
        proposal.setCommandId(SPLIT_JOIN_VARIABLE_DECLARATION_ID);
    }
    Statement newStatement;
    int insertIndex = list.indexOf(statement);
    Expression placeholder = (Expression) rewrite.createMoveTarget(fragment.getInitializer());
    ITypeBinding binding = fragment.getInitializer().resolveTypeBinding();
    if (placeholder instanceof ArrayInitializer && binding != null && binding.isArray()) {
        ArrayCreation creation = ast.newArrayCreation();
        creation.setInitializer((ArrayInitializer) placeholder);
        final ITypeBinding componentType = binding.getElementType();
        Type type = null;
        if (componentType.isPrimitive())
            type = ast.newPrimitiveType(PrimitiveType.toCode(componentType.getName()));
        else
            type = ast.newSimpleType(ast.newSimpleName(componentType.getName()));
        creation.setType(ast.newArrayType(type, binding.getDimensions()));
        placeholder = creation;
    }
    Assignment assignment = ast.newAssignment();
    assignment.setRightHandSide(placeholder);
    assignment.setLeftHandSide(ast.newSimpleName(fragment.getName().getIdentifier()));
    if (statement instanceof VariableDeclarationStatement) {
        newStatement = ast.newExpressionStatement(assignment);
        // add after declaration
        insertIndex += 1;
    } else {
        rewrite.replace(fragment.getParent(), assignment, null);
        VariableDeclarationFragment newFrag = ast.newVariableDeclarationFragment();
        newFrag.setName(ast.newSimpleName(fragment.getName().getIdentifier()));
        newFrag.extraDimensions().addAll(DimensionRewrite.copyDimensions(fragment.extraDimensions(), rewrite));
        VariableDeclarationExpression oldVarDecl = (VariableDeclarationExpression) fragParent;
        VariableDeclarationStatement newVarDec = ast.newVariableDeclarationStatement(newFrag);
        newVarDec.setType((Type) rewrite.createCopyTarget(oldVarDecl.getType()));
        newVarDec.modifiers().addAll(ASTNodeFactory.newModifiers(ast, oldVarDecl.getModifiers()));
        newStatement = newVarDec;
    }
    ListRewrite listRewriter = rewrite.getListRewrite(statementParent, (ChildListPropertyDescriptor) property);
    listRewriter.insertAt(newStatement, insertIndex, null);
    resultingCollections.add(proposal);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ICommandAccess(org.eclipse.jdt.ui.text.java.correction.ICommandAccess) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) Assignment(org.eclipse.jdt.core.dom.Assignment) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IType(org.eclipse.jdt.core.IType) UnionType(org.eclipse.jdt.core.dom.UnionType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 57 with VariableDeclarationFragment

use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project flux by eclipse.

the class QuickAssistProcessor method getJoinVariableProposals.

private static boolean getJoinVariableProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    ASTNode parent = node.getParent();
    VariableDeclarationFragment fragment = null;
    boolean onFirstAccess = false;
    if (node instanceof SimpleName && node.getLocationInParent() == Assignment.LEFT_HAND_SIDE_PROPERTY) {
        onFirstAccess = true;
        SimpleName name = (SimpleName) node;
        IBinding binding = name.resolveBinding();
        if (!(binding instanceof IVariableBinding)) {
            return false;
        }
        ASTNode declaring = context.getASTRoot().findDeclaringNode(binding);
        if (declaring instanceof VariableDeclarationFragment) {
            fragment = (VariableDeclarationFragment) declaring;
        } else {
            return false;
        }
    } else if (parent instanceof VariableDeclarationFragment) {
        fragment = (VariableDeclarationFragment) parent;
    } else {
        return false;
    }
    IVariableBinding binding = fragment.resolveBinding();
    Expression initializer = fragment.getInitializer();
    if ((initializer != null && initializer.getNodeType() != ASTNode.NULL_LITERAL) || binding == null || binding.isField()) {
        return false;
    }
    if (!(fragment.getParent() instanceof VariableDeclarationStatement)) {
        return false;
    }
    VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
    SimpleName[] names = LinkedNodeFinder.findByBinding(statement.getParent(), binding);
    if (names.length <= 1 || names[0] != fragment.getName()) {
        return false;
    }
    SimpleName firstAccess = names[1];
    if (onFirstAccess) {
        if (firstAccess != node) {
            return false;
        }
    } else {
        if (firstAccess.getLocationInParent() != Assignment.LEFT_HAND_SIDE_PROPERTY) {
            return false;
        }
    }
    Assignment assignment = (Assignment) firstAccess.getParent();
    if (assignment.getLocationInParent() != ExpressionStatement.EXPRESSION_PROPERTY) {
        return false;
    }
    ExpressionStatement assignParent = (ExpressionStatement) assignment.getParent();
    if (resultingCollections == null) {
        return true;
    }
    AST ast = statement.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    //		TightSourceRangeComputer sourceRangeComputer= new TightSourceRangeComputer();
    //		sourceRangeComputer.addTightSourceNode(assignParent);
    //		rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
    String label = CorrectionMessages.QuickAssistProcessor_joindeclaration_description;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.JOIN_VARIABLE_DECLARATION);
    proposal.setCommandId(SPLIT_JOIN_VARIABLE_DECLARATION_ID);
    Expression placeholder = (Expression) rewrite.createMoveTarget(assignment.getRightHandSide());
    rewrite.set(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY, placeholder, null);
    if (onFirstAccess) {
        // replace assignment with variable declaration
        rewrite.replace(assignParent, rewrite.createMoveTarget(statement), null);
    } else {
        // different scopes -> remove assignments, set variable initializer
        if (ASTNodes.isControlStatementBody(assignParent.getLocationInParent())) {
            Block block = ast.newBlock();
            rewrite.replace(assignParent, block, null);
        } else {
            rewrite.remove(assignParent, null);
        }
    }
    proposal.setEndPosition(rewrite.track(fragment.getName()));
    resultingCollections.add(proposal);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Assignment(org.eclipse.jdt.core.dom.Assignment) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block)

Example 58 with VariableDeclarationFragment

use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project flux by eclipse.

the class AdvancedQuickAssistProcessor method getInverseLocalVariableProposals.

private static boolean getInverseLocalVariableProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    final AST ast = covering.getAST();
    // cursor should be placed on variable name
    if (!(covering instanceof SimpleName)) {
        return false;
    }
    SimpleName coveringName = (SimpleName) covering;
    if (!coveringName.isDeclaration()) {
        return false;
    }
    // prepare bindings
    final IBinding variableBinding = coveringName.resolveBinding();
    if (!(variableBinding instanceof IVariableBinding)) {
        return false;
    }
    IVariableBinding binding = (IVariableBinding) variableBinding;
    if (binding.isField()) {
        return false;
    }
    // we operate only on boolean variable
    if (!isBoolean(coveringName)) {
        return false;
    }
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    // find linked nodes
    final MethodDeclaration method = ASTResolving.findParentMethodDeclaration(covering);
    SimpleName[] linkedNodes = LinkedNodeFinder.findByBinding(method, variableBinding);
    //
    final ASTRewrite rewrite = ASTRewrite.create(ast);
    // create proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_inverseBooleanVariable;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    //$NON-NLS-1$
    final String KEY_NAME = "name";
    final LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERSE_BOOLEAN_VARIABLE);
    // prepare new variable identifier
    final String oldIdentifier = coveringName.getIdentifier();
    //$NON-NLS-1$
    final String notString = Messages.format(CorrectionMessages.AdvancedQuickAssistProcessor_negatedVariableName, "");
    final String newIdentifier;
    if (oldIdentifier.startsWith(notString)) {
        int notLength = notString.length();
        if (oldIdentifier.length() > notLength) {
            newIdentifier = Character.toLowerCase(oldIdentifier.charAt(notLength)) + oldIdentifier.substring(notLength + 1);
        } else {
            newIdentifier = oldIdentifier;
        }
    } else {
        newIdentifier = Messages.format(CorrectionMessages.AdvancedQuickAssistProcessor_negatedVariableName, Character.toUpperCase(oldIdentifier.charAt(0)) + oldIdentifier.substring(1));
    }
    //
    proposal.addLinkedPositionProposal(KEY_NAME, newIdentifier, null);
    proposal.addLinkedPositionProposal(KEY_NAME, oldIdentifier, null);
    // iterate over linked nodes and replace variable references with negated reference
    final HashSet<SimpleName> renamedNames = new HashSet<SimpleName>();
    for (int i = 0; i < linkedNodes.length; i++) {
        SimpleName name = linkedNodes[i];
        if (renamedNames.contains(name)) {
            continue;
        }
        // prepare new name with new identifier
        SimpleName newName = ast.newSimpleName(newIdentifier);
        proposal.addLinkedPosition(rewrite.track(newName), name == coveringName, KEY_NAME);
        //
        StructuralPropertyDescriptor location = name.getLocationInParent();
        if (location == SingleVariableDeclaration.NAME_PROPERTY) {
            // set new name
            rewrite.replace(name, newName, null);
        } else if (location == Assignment.LEFT_HAND_SIDE_PROPERTY) {
            Assignment assignment = (Assignment) name.getParent();
            Expression expression = assignment.getRightHandSide();
            int exStart = expression.getStartPosition();
            int exEnd = exStart + expression.getLength();
            // collect all names that are used in assignments
            HashSet<SimpleName> overlapNames = new HashSet<SimpleName>();
            for (int j = 0; j < linkedNodes.length; j++) {
                SimpleName name2 = linkedNodes[j];
                if (name2 == null) {
                    continue;
                }
                int name2Start = name2.getStartPosition();
                if (exStart <= name2Start && name2Start < exEnd) {
                    overlapNames.add(name2);
                }
            }
            // prepare inverted expression
            SimpleNameRenameProvider provider = new SimpleNameRenameProvider() {

                public SimpleName getRenamed(SimpleName simpleName) {
                    if (simpleName.resolveBinding() == variableBinding) {
                        renamedNames.add(simpleName);
                        return ast.newSimpleName(newIdentifier);
                    }
                    return null;
                }
            };
            Expression inversedExpression = getInversedExpression(rewrite, expression, provider);
            // if any name was not renamed during expression inverting, we can not already rename it, so fail to create assist
            for (Iterator<SimpleName> iter = overlapNames.iterator(); iter.hasNext(); ) {
                Object o = iter.next();
                if (!renamedNames.contains(o)) {
                    return false;
                }
            }
            // check operator and replace if needed
            Assignment.Operator operator = assignment.getOperator();
            if (operator == Assignment.Operator.BIT_AND_ASSIGN) {
                Assignment newAssignment = ast.newAssignment();
                newAssignment.setLeftHandSide(newName);
                newAssignment.setRightHandSide(inversedExpression);
                newAssignment.setOperator(Assignment.Operator.BIT_OR_ASSIGN);
                rewrite.replace(assignment, newAssignment, null);
            } else if (operator == Assignment.Operator.BIT_OR_ASSIGN) {
                Assignment newAssignment = ast.newAssignment();
                newAssignment.setLeftHandSide(newName);
                newAssignment.setRightHandSide(inversedExpression);
                newAssignment.setOperator(Assignment.Operator.BIT_AND_ASSIGN);
                rewrite.replace(assignment, newAssignment, null);
            } else {
                rewrite.replace(expression, inversedExpression, null);
                // set new name
                rewrite.replace(name, newName, null);
            }
        } else if (location == VariableDeclarationFragment.NAME_PROPERTY) {
            // replace initializer for variable
            VariableDeclarationFragment vdf = (VariableDeclarationFragment) name.getParent();
            Expression expression = vdf.getInitializer();
            if (expression != null) {
                rewrite.replace(expression, getInversedExpression(rewrite, expression), null);
            }
            // set new name
            rewrite.replace(name, newName, null);
        } else if (name.getParent() instanceof PrefixExpression && ((PrefixExpression) name.getParent()).getOperator() == PrefixExpression.Operator.NOT) {
            rewrite.replace(name.getParent(), newName, null);
        } else {
            PrefixExpression expression = ast.newPrefixExpression();
            expression.setOperator(PrefixExpression.Operator.NOT);
            expression.setOperand(newName);
            rewrite.replace(name, expression, null);
        }
    }
    // add correction proposal
    resultingCollections.add(proposal);
    return true;
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Assignment(org.eclipse.jdt.core.dom.Assignment) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) Iterator(java.util.Iterator) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) HashSet(java.util.HashSet) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 59 with VariableDeclarationFragment

use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.

the class VariableDeclarationFix method createAddFinalOperation.

private static ModifierChangeOperation createAddFinalOperation(SimpleName name, ASTNode decl) {
    if (decl == null)
        return null;
    IBinding binding = name.resolveBinding();
    if (!canAddFinal(binding, decl))
        return null;
    if (decl instanceof SingleVariableDeclaration) {
        return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
    } else if (decl instanceof VariableDeclarationExpression) {
        return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
    } else if (decl instanceof VariableDeclarationFragment) {
        VariableDeclarationFragment frag = (VariableDeclarationFragment) decl;
        decl = decl.getParent();
        if (decl instanceof FieldDeclaration || decl instanceof VariableDeclarationStatement) {
            List<VariableDeclarationFragment> list = new ArrayList<VariableDeclarationFragment>();
            list.add(frag);
            return new ModifierChangeOperation(decl, list, Modifier.FINAL, Modifier.NONE);
        } else if (decl instanceof VariableDeclarationExpression) {
            return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
        }
    }
    return null;
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) IBinding(org.eclipse.jdt.core.dom.IBinding) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ArrayList(java.util.ArrayList) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 60 with VariableDeclarationFragment

use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.

the class ExtractTempRefactoring method canReplace.

private static boolean canReplace(IASTFragment fragment) {
    ASTNode node = fragment.getAssociatedNode();
    ASTNode parent = node.getParent();
    if (parent instanceof VariableDeclarationFragment) {
        VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
        if (node.equals(vdf.getName()))
            return false;
    }
    if (isMethodParameter(node))
        return false;
    if (isThrowableInCatchBlock(node))
        return false;
    if (parent instanceof ExpressionStatement)
        return false;
    if (isLeftValue(node))
        return false;
    if (isReferringToLocalVariableFromFor((Expression) node))
        return false;
    if (isUsedInForInitializerOrUpdater((Expression) node))
        return false;
    if (parent instanceof SwitchCase)
        return false;
    return true;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Aggregations

VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)101 ASTNode (org.eclipse.jdt.core.dom.ASTNode)44 AST (org.eclipse.jdt.core.dom.AST)38 Expression (org.eclipse.jdt.core.dom.Expression)33 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)33 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)30 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)28 Assignment (org.eclipse.jdt.core.dom.Assignment)25 SimpleName (org.eclipse.jdt.core.dom.SimpleName)25 Type (org.eclipse.jdt.core.dom.Type)25 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)22 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)21 Block (org.eclipse.jdt.core.dom.Block)17 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)16 ArrayList (java.util.ArrayList)15 CastExpression (org.eclipse.jdt.core.dom.CastExpression)15 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)15 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)15 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)15 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)14