Search in sources :

Example 46 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName 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 47 with SimpleName

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

the class ConvertForLoopOperation method convertBody.

private void convertBody(Statement body, final IBinding indexBinding, final IBinding arrayBinding, final String parameterName, final ASTRewrite rewrite, final TextEditGroup editGroup, final LinkedProposalPositionGroup pg) {
    final AST ast = body.getAST();
    body.accept(new GenericVisitor() {

        @Override
        public boolean visit(ArrayAccess node) {
            IBinding binding = getBinding(node.getArray());
            if (arrayBinding.equals(binding)) {
                IBinding index = getBinding(node.getIndex());
                if (indexBinding.equals(index)) {
                    replaceAccess(node);
                }
            }
            return super.visit(node);
        }

        private void replaceAccess(ASTNode node) {
            if (fElementDeclaration != null && node.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
                VariableDeclarationFragment fragment = (VariableDeclarationFragment) node.getParent();
                IBinding targetBinding = fragment.getName().resolveBinding();
                if (targetBinding != null) {
                    VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
                    if (statement.fragments().size() == 1) {
                        rewrite.remove(statement, editGroup);
                    } else {
                        ListRewrite listRewrite = rewrite.getListRewrite(statement, VariableDeclarationStatement.FRAGMENTS_PROPERTY);
                        listRewrite.remove(fragment, editGroup);
                    }
                } else {
                    SimpleName name = ast.newSimpleName(parameterName);
                    rewrite.replace(node, name, editGroup);
                    pg.addPosition(rewrite.track(name), true);
                }
            } else {
                SimpleName name = ast.newSimpleName(parameterName);
                rewrite.replace(node, name, editGroup);
                pg.addPosition(rewrite.track(name), true);
            }
        }
    });
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) IBinding(org.eclipse.jdt.core.dom.IBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) GenericVisitor(org.eclipse.jdt.internal.corext.dom.GenericVisitor)

Example 48 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName 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 49 with SimpleName

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

the class NullAnnotationsFix method isComplainingAboutArgument.

/* recognizes any simple name referring to a parameter binding */
public static boolean isComplainingAboutArgument(ASTNode selectedNode) {
    if (!(selectedNode instanceof SimpleName))
        return false;
    SimpleName nameNode = (SimpleName) selectedNode;
    IBinding binding = nameNode.resolveBinding();
    if (binding.getKind() == IBinding.VARIABLE && ((IVariableBinding) binding).isParameter())
        return true;
    VariableDeclaration argDecl = (VariableDeclaration) ASTNodes.getParent(selectedNode, VariableDeclaration.class);
    if (argDecl != null)
        binding = argDecl.resolveBinding();
    if (binding.getKind() == IBinding.VARIABLE && ((IVariableBinding) binding).isParameter())
        return true;
    return false;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration)

Example 50 with SimpleName

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

the class UnusedCodeFix method createUnusedMemberFix.

public static UnusedCodeFix createUnusedMemberFix(CompilationUnit compilationUnit, IProblemLocation problem, boolean removeAllAssignements) {
    if (isUnusedMember(problem)) {
        SimpleName name = getUnusedName(compilationUnit, problem);
        if (name != null) {
            IBinding binding = name.resolveBinding();
            if (binding != null) {
                if (isFormalParameterInEnhancedForStatement(name))
                    return null;
                String label = getDisplayString(name, binding, removeAllAssignements);
                RemoveUnusedMemberOperation operation = new RemoveUnusedMemberOperation(new SimpleName[] { name }, removeAllAssignements);
                return new UnusedCodeFix(label, compilationUnit, new CompilationUnitRewriteOperation[] { operation }, getCleanUpOptions(binding, removeAllAssignements));
            }
        }
    }
    return null;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding)

Aggregations

SimpleName (org.eclipse.jdt.core.dom.SimpleName)291 ASTNode (org.eclipse.jdt.core.dom.ASTNode)122 IBinding (org.eclipse.jdt.core.dom.IBinding)70 Expression (org.eclipse.jdt.core.dom.Expression)67 AST (org.eclipse.jdt.core.dom.AST)63 ArrayList (java.util.ArrayList)60 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)57 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)55 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)53 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)47 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)46 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)44 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)43 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)41 Name (org.eclipse.jdt.core.dom.Name)40 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 Type (org.eclipse.jdt.core.dom.Type)35 Block (org.eclipse.jdt.core.dom.Block)34 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)33 Assignment (org.eclipse.jdt.core.dom.Assignment)32