Search in sources :

Example 31 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 32 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 33 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 34 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)

Example 35 with IBinding

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

the class ConvertAnonymousToNestedRefactoring method accessesAnonymousFields.

private boolean accessesAnonymousFields() {
    List<IVariableBinding> anonymousInnerFieldTypes = getAllEnclosingAnonymousTypesField();
    List<IBinding> accessedField = getAllAccessedFields();
    final Iterator<IVariableBinding> it = anonymousInnerFieldTypes.iterator();
    while (it.hasNext()) {
        final IVariableBinding variableBinding = it.next();
        final Iterator<IBinding> it2 = accessedField.iterator();
        while (it2.hasNext()) {
            IVariableBinding variableBinding2 = (IVariableBinding) it2.next();
            if (Bindings.equals(variableBinding, variableBinding2)) {
                return true;
            }
        }
    }
    return false;
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Aggregations

IBinding (org.eclipse.jdt.core.dom.IBinding)103 SimpleName (org.eclipse.jdt.core.dom.SimpleName)59 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)48 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)41 ASTNode (org.eclipse.jdt.core.dom.ASTNode)40 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)25 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)25 Name (org.eclipse.jdt.core.dom.Name)20 Expression (org.eclipse.jdt.core.dom.Expression)19 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 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)11 AST (org.eclipse.jdt.core.dom.AST)10 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)10