Search in sources :

Example 6 with FieldAccess

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

the class NullAnnotationsCorrectionProcessor method findProblemFieldName.

private static SimpleName findProblemFieldName(ASTNode selectedNode, int problemID) {
    // with expected type declared @NonNull we first need to find the SimpleName inside:
    if (selectedNode instanceof FieldAccess)
        selectedNode = ((FieldAccess) selectedNode).getName();
    else if (selectedNode instanceof QualifiedName)
        selectedNode = ((QualifiedName) selectedNode).getName();
    if (selectedNode instanceof SimpleName) {
        SimpleName name = (SimpleName) selectedNode;
        if (problemID == IProblem.NullableFieldReference)
            return name;
        // not field dereference, but compatibility issue - is value a field reference?
        IBinding binding = name.resolveBinding();
        if ((binding instanceof IVariableBinding) && ((IVariableBinding) binding).isField())
            return name;
    }
    return null;
}
Also used : QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 7 with FieldAccess

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

the class ConvertIterableLoopOperation method convert.

@Override
protected Statement convert(CompilationUnitRewrite cuRewrite, final TextEditGroup group, final LinkedProposalModel positionGroups) throws CoreException {
    final AST ast = cuRewrite.getAST();
    final ASTRewrite astRewrite = cuRewrite.getASTRewrite();
    final ImportRewrite importRewrite = cuRewrite.getImportRewrite();
    final ImportRemover remover = cuRewrite.getImportRemover();
    fEnhancedForLoop = ast.newEnhancedForStatement();
    String[] names = getVariableNameProposals();
    String name;
    if (fElementVariable != null) {
        name = fElementVariable.getName();
    } else {
        name = names[0];
    }
    final LinkedProposalPositionGroup pg = positionGroups.getPositionGroup(name, true);
    if (fElementVariable != null)
        pg.addProposal(name, null, 10);
    for (int i = 0; i < names.length; i++) {
        pg.addProposal(names[i], null, 10);
    }
    final Statement body = getForStatement().getBody();
    if (body != null) {
        final ListRewrite list;
        if (body instanceof Block) {
            list = astRewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);
            for (final Iterator<Expression> iterator = fOccurrences.iterator(); iterator.hasNext(); ) {
                final Statement parent = (Statement) ASTNodes.getParent(iterator.next(), Statement.class);
                if (parent != null && list.getRewrittenList().contains(parent)) {
                    list.remove(parent, null);
                    remover.registerRemovedNode(parent);
                }
            }
        } else {
            list = null;
        }
        final String text = name;
        body.accept(new ASTVisitor() {

            private boolean replace(final Expression expression) {
                final SimpleName node = ast.newSimpleName(text);
                astRewrite.replace(expression, node, group);
                remover.registerRemovedNode(expression);
                pg.addPosition(astRewrite.track(node), false);
                return false;
            }

            @Override
            public final boolean visit(final MethodInvocation node) {
                final IMethodBinding binding = node.resolveMethodBinding();
                if (binding != null && (binding.getName().equals("next") || binding.getName().equals("nextElement"))) {
                    //$NON-NLS-1$ //$NON-NLS-2$
                    final Expression expression = node.getExpression();
                    if (expression instanceof Name) {
                        final IBinding result = ((Name) expression).resolveBinding();
                        if (result != null && result.equals(fIteratorVariable))
                            return replace(node);
                    } else if (expression instanceof FieldAccess) {
                        final IBinding result = ((FieldAccess) expression).resolveFieldBinding();
                        if (result != null && result.equals(fIteratorVariable))
                            return replace(node);
                    }
                }
                return super.visit(node);
            }

            @Override
            public final boolean visit(final SimpleName node) {
                if (fElementVariable != null) {
                    final IBinding binding = node.resolveBinding();
                    if (binding != null && binding.equals(fElementVariable)) {
                        final Statement parent = (Statement) ASTNodes.getParent(node, Statement.class);
                        if (parent != null && (list == null || list.getRewrittenList().contains(parent)))
                            pg.addPosition(astRewrite.track(node), false);
                    }
                }
                return false;
            }
        });
        fEnhancedForLoop.setBody(getBody(cuRewrite, group, positionGroups));
    }
    final SingleVariableDeclaration declaration = ast.newSingleVariableDeclaration();
    final SimpleName simple = ast.newSimpleName(name);
    pg.addPosition(astRewrite.track(simple), true);
    declaration.setName(simple);
    final ITypeBinding elementType = getElementType(fIteratorVariable.getType());
    declaration.setType(importType(elementType, getForStatement(), importRewrite, getRoot()));
    if (fMakeFinal) {
        ModifierRewrite.create(astRewrite, declaration).setModifiers(Modifier.FINAL, 0, group);
    }
    remover.registerAddedImport(elementType.getQualifiedName());
    fEnhancedForLoop.setParameter(declaration);
    fEnhancedForLoop.setExpression(getExpression(astRewrite));
    for (Iterator<Expression> iterator = getForStatement().initializers().iterator(); iterator.hasNext(); ) {
        ASTNode node = iterator.next();
        if (node instanceof VariableDeclarationExpression) {
            VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression) node;
            remover.registerRemovedNode(variableDeclarationExpression.getType());
        } else {
            remover.registerRemovedNode(node);
        }
    }
    for (Iterator<Expression> iterator = getForStatement().updaters().iterator(); iterator.hasNext(); ) {
        ASTNode node = iterator.next();
        remover.registerRemovedNode(node);
    }
    return fEnhancedForLoop;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ImportRemover(org.eclipse.jdt.internal.corext.refactoring.structure.ImportRemover) AST(org.eclipse.jdt.core.dom.AST) Statement(org.eclipse.jdt.core.dom.Statement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Block(org.eclipse.jdt.core.dom.Block) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 8 with FieldAccess

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

the class CodeStyleFix method createToStaticAccessOperations.

private static ToStaticAccessOperation[] createToStaticAccessOperations(CompilationUnit astRoot, HashMap<ASTNode, Block> createdBlocks, IProblemLocation problem, boolean conservative) {
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null) {
        return null;
    }
    Expression qualifier = null;
    IBinding accessBinding = null;
    if (selectedNode instanceof SimpleName) {
        selectedNode = selectedNode.getParent();
    }
    if (selectedNode instanceof QualifiedName) {
        QualifiedName name = (QualifiedName) selectedNode;
        qualifier = name.getQualifier();
        accessBinding = name.resolveBinding();
    } else if (selectedNode instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) selectedNode;
        qualifier = methodInvocation.getExpression();
        accessBinding = methodInvocation.getName().resolveBinding();
    } else if (selectedNode instanceof FieldAccess) {
        FieldAccess fieldAccess = (FieldAccess) selectedNode;
        qualifier = fieldAccess.getExpression();
        accessBinding = fieldAccess.getName().resolveBinding();
    }
    if (accessBinding != null && qualifier != null) {
        if (conservative && ASTResolving.findParentStatement(qualifier) == null)
            return null;
        ToStaticAccessOperation declaring = null;
        ITypeBinding declaringTypeBinding = getDeclaringTypeBinding(accessBinding);
        if (declaringTypeBinding != null) {
            // use generic to avoid any type arguments
            declaringTypeBinding = declaringTypeBinding.getTypeDeclaration();
            declaring = new ToStaticAccessOperation(declaringTypeBinding, qualifier, createdBlocks);
        }
        ToStaticAccessOperation instance = null;
        ITypeBinding instanceTypeBinding = Bindings.normalizeTypeBinding(qualifier.resolveTypeBinding());
        if (instanceTypeBinding != null) {
            // use generic to avoid any type arguments
            instanceTypeBinding = instanceTypeBinding.getTypeDeclaration();
            if (instanceTypeBinding.getTypeDeclaration() != declaringTypeBinding) {
                instance = new ToStaticAccessOperation(instanceTypeBinding, qualifier, createdBlocks);
            }
        }
        if (declaring != null && instance != null) {
            return new ToStaticAccessOperation[] { declaring, instance };
        } else {
            return new ToStaticAccessOperation[] { declaring };
        }
    }
    return null;
}
Also used : ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) IBinding(org.eclipse.jdt.core.dom.IBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess)

Example 9 with FieldAccess

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

the class ConvertForLoopOperation method validateLengthQuery.

/*
	 * Must be one of:
	 * <ul>
	 * <li>[result].length</li>
	 * </ul>
	 */
private boolean validateLengthQuery(Expression lengthQuery) {
    if (lengthQuery instanceof QualifiedName) {
        QualifiedName qualifiedName = (QualifiedName) lengthQuery;
        SimpleName name = qualifiedName.getName();
        if (!LENGTH_QUERY.equals(name.getIdentifier()))
            return false;
        Name arrayAccess = qualifiedName.getQualifier();
        ITypeBinding accessType = arrayAccess.resolveTypeBinding();
        if (accessType == null)
            return false;
        if (!accessType.isArray())
            return false;
        IBinding arrayBinding = arrayAccess.resolveBinding();
        if (arrayBinding == null)
            return false;
        fArrayBinding = arrayBinding;
        fArrayAccess = arrayAccess;
        return true;
    } else if (lengthQuery instanceof FieldAccess) {
        FieldAccess fieldAccess = (FieldAccess) lengthQuery;
        SimpleName name = fieldAccess.getName();
        if (!LENGTH_QUERY.equals(name.getIdentifier()))
            return false;
        Expression arrayAccess = fieldAccess.getExpression();
        ITypeBinding accessType = arrayAccess.resolveTypeBinding();
        if (accessType == null)
            return false;
        if (!accessType.isArray())
            return false;
        IBinding arrayBinding = getBinding(arrayAccess);
        if (arrayBinding == null)
            return false;
        fArrayBinding = arrayBinding;
        fArrayAccess = arrayAccess;
        return true;
    }
    return false;
}
Also used : PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IBinding(org.eclipse.jdt.core.dom.IBinding) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName)

Example 10 with FieldAccess

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

the class UnusedCodeFix method hasSideEffect.

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;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) Expression(org.eclipse.jdt.core.dom.Expression) 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) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Aggregations

FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)30 SimpleName (org.eclipse.jdt.core.dom.SimpleName)20 Expression (org.eclipse.jdt.core.dom.Expression)18 ASTNode (org.eclipse.jdt.core.dom.ASTNode)17 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)14 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)12 IBinding (org.eclipse.jdt.core.dom.IBinding)11 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 Name (org.eclipse.jdt.core.dom.Name)10 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)8 AST (org.eclipse.jdt.core.dom.AST)7 Assignment (org.eclipse.jdt.core.dom.Assignment)7 Block (org.eclipse.jdt.core.dom.Block)7 SuperFieldAccess (org.eclipse.jdt.core.dom.SuperFieldAccess)7 CastExpression (org.eclipse.jdt.core.dom.CastExpression)6 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)6 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)6 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 ArrayList (java.util.ArrayList)5