Search in sources :

Example 26 with IBinding

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

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

use of org.eclipse.jdt.core.dom.IBinding 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)

Example 29 with IBinding

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

the class UnusedCodeFix method createCleanUp.

public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, IProblemLocation[] problems, boolean removeUnusedPrivateMethods, boolean removeUnusedPrivateConstructors, boolean removeUnusedPrivateFields, boolean removeUnusedPrivateTypes, boolean removeUnusedLocalVariables, boolean removeUnusedImports, boolean removeUnusedCast) {
    List<CompilationUnitRewriteOperation> result = new ArrayList<CompilationUnitRewriteOperation>();
    Hashtable<ASTNode, List<SimpleName>> variableDeclarations = new Hashtable<ASTNode, List<SimpleName>>();
    LinkedHashSet<CastExpression> unnecessaryCasts = new LinkedHashSet<CastExpression>();
    for (int i = 0; i < problems.length; i++) {
        IProblemLocation problem = problems[i];
        int id = problem.getProblemId();
        if (removeUnusedImports && (id == IProblem.UnusedImport || id == IProblem.DuplicateImport || id == IProblem.ConflictingImport || id == IProblem.CannotImportPackage || id == IProblem.ImportNotFound)) {
            ImportDeclaration node = UnusedCodeFix.getImportDeclaration(problem, compilationUnit);
            if (node != null) {
                result.add(new RemoveImportOperation(node));
            }
        }
        if ((removeUnusedPrivateMethods && id == IProblem.UnusedPrivateMethod) || (removeUnusedPrivateConstructors && id == IProblem.UnusedPrivateConstructor) || (removeUnusedPrivateTypes && id == IProblem.UnusedPrivateType)) {
            SimpleName name = getUnusedName(compilationUnit, problem);
            if (name != null) {
                IBinding binding = name.resolveBinding();
                if (binding != null) {
                    result.add(new RemoveUnusedMemberOperation(new SimpleName[] { name }, false));
                }
            }
        }
        if ((removeUnusedLocalVariables && id == IProblem.LocalVariableIsNeverUsed) || (removeUnusedPrivateFields && id == IProblem.UnusedPrivateField)) {
            SimpleName name = getUnusedName(compilationUnit, problem);
            if (name != null) {
                IBinding binding = name.resolveBinding();
                if (binding instanceof IVariableBinding && !isFormalParameterInEnhancedForStatement(name) && (!((IVariableBinding) binding).isField() || isSideEffectFree(name, compilationUnit))) {
                    VariableDeclarationFragment parent = (VariableDeclarationFragment) ASTNodes.getParent(name, VariableDeclarationFragment.class);
                    if (parent != null) {
                        ASTNode varDecl = parent.getParent();
                        if (!variableDeclarations.containsKey(varDecl)) {
                            variableDeclarations.put(varDecl, new ArrayList<SimpleName>());
                        }
                        variableDeclarations.get(varDecl).add(name);
                    } else {
                        result.add(new RemoveUnusedMemberOperation(new SimpleName[] { name }, false));
                    }
                }
            }
        }
        if (removeUnusedCast && id == IProblem.UnnecessaryCast) {
            ASTNode selectedNode = problem.getCoveringNode(compilationUnit);
            ASTNode curr = selectedNode;
            while (curr instanceof ParenthesizedExpression) {
                curr = ((ParenthesizedExpression) curr).getExpression();
            }
            if (curr instanceof CastExpression) {
                unnecessaryCasts.add((CastExpression) curr);
            }
        }
    }
    for (Iterator<ASTNode> iter = variableDeclarations.keySet().iterator(); iter.hasNext(); ) {
        ASTNode node = iter.next();
        List<SimpleName> names = variableDeclarations.get(node);
        result.add(new RemoveUnusedMemberOperation(names.toArray(new SimpleName[names.size()]), false));
    }
    if (unnecessaryCasts.size() > 0)
        result.add(new RemoveAllCastOperation(unnecessaryCasts));
    if (result.size() == 0)
        return null;
    return new UnusedCodeFix(FixMessages.UnusedCodeFix_change_name, compilationUnit, result.toArray(new CompilationUnitRewriteOperation[result.size()]));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) List(java.util.List) ArrayList(java.util.ArrayList) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) Hashtable(java.util.Hashtable) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 30 with IBinding

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

the class Checks method isEnumCase.

public static boolean isEnumCase(ASTNode node) {
    if (node instanceof SwitchCase) {
        final SwitchCase caze = (SwitchCase) node;
        final Expression expression = caze.getExpression();
        if (expression instanceof Name) {
            final Name name = (Name) expression;
            final IBinding binding = name.resolveBinding();
            if (binding instanceof IVariableBinding) {
                IVariableBinding variableBinding = (IVariableBinding) binding;
                return variableBinding.isEnumConstant();
            }
        }
    }
    return false;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) Expression(org.eclipse.jdt.core.dom.Expression) IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Name(org.eclipse.jdt.core.dom.Name)

Aggregations

IBinding (org.eclipse.jdt.core.dom.IBinding)100 SimpleName (org.eclipse.jdt.core.dom.SimpleName)57 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)45 ASTNode (org.eclipse.jdt.core.dom.ASTNode)40 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)40 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)25 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)24 Name (org.eclipse.jdt.core.dom.Name)20 Expression (org.eclipse.jdt.core.dom.Expression)18 ArrayList (java.util.ArrayList)16 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)14 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)13 CastExpression (org.eclipse.jdt.core.dom.CastExpression)12 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)11 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)11 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)11 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)11 AST (org.eclipse.jdt.core.dom.AST)10 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)10 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)10