Search in sources :

Example 51 with SimpleName

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

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

the class UnusedCodeFix method isSideEffectFree.

private static boolean isSideEffectFree(SimpleName simpleName, CompilationUnit completeRoot) {
    SimpleName nameNode = (SimpleName) NodeFinder.perform(completeRoot, simpleName.getStartPosition(), simpleName.getLength());
    SimpleName[] references = LinkedNodeFinder.findByBinding(completeRoot, nameNode.resolveBinding());
    for (int i = 0; i < references.length; i++) {
        if (hasSideEffect(references[i]))
            return false;
    }
    return true;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName)

Example 53 with SimpleName

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

the class TempOccurrenceAnalyzer method addOffsets.

private void addOffsets(int[] offsets, int start, Set<SimpleName> nodeSet) {
    int i = start;
    for (Iterator<SimpleName> iter = nodeSet.iterator(); iter.hasNext(); i++) {
        ASTNode node = iter.next();
        offsets[i] = node.getStartPosition();
    }
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 54 with SimpleName

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

the class ASTNodeSearchUtil method getAstNode.

public static ASTNode getAstNode(CompilationUnit cuNode, int start, int length) {
    SelectionAnalyzer analyzer = new SelectionAnalyzer(Selection.createFromStartLength(start, length), true);
    cuNode.accept(analyzer);
    //XXX workaround for jdt core feature 23527
    ASTNode node = analyzer.getFirstSelectedNode();
    if (node == null && analyzer.getLastCoveringNode() instanceof SuperConstructorInvocation)
        node = analyzer.getLastCoveringNode().getParent();
    else if (node == null && analyzer.getLastCoveringNode() instanceof ConstructorInvocation)
        node = analyzer.getLastCoveringNode().getParent();
    if (node == null)
        return null;
    ASTNode parentNode = node.getParent();
    if (parentNode instanceof MethodDeclaration) {
        MethodDeclaration md = (MethodDeclaration) parentNode;
        if (!(node instanceof SimpleName) && md.isConstructor() && md.getBody() != null && md.getBody().statements().size() > 0 && (md.getBody().statements().get(0) instanceof ConstructorInvocation || md.getBody().statements().get(0) instanceof SuperConstructorInvocation) && ((ASTNode) md.getBody().statements().get(0)).getLength() == length + 1)
            return (ASTNode) md.getBody().statements().get(0);
    }
    if (parentNode instanceof SuperConstructorInvocation) {
        if (parentNode.getLength() == length + 1)
            return parentNode;
    }
    if (parentNode instanceof ConstructorInvocation) {
        if (parentNode.getLength() == length + 1)
            return parentNode;
    }
    return node;
}
Also used : SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Example 55 with SimpleName

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

the class JavadocFinder method resolveBinding.

private static IBinding resolveBinding(ASTNode node) {
    if (node instanceof SimpleName) {
        SimpleName simpleName = (SimpleName) node;
        // workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
        ASTNode normalized = ASTNodes.getNormalizedNode(simpleName);
        if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
            ClassInstanceCreation cic = (ClassInstanceCreation) normalized.getParent();
            IMethodBinding constructorBinding = cic.resolveConstructorBinding();
            if (constructorBinding == null)
                return null;
            ITypeBinding declaringClass = constructorBinding.getDeclaringClass();
            if (!declaringClass.isAnonymous())
                return constructorBinding;
            ITypeBinding superTypeDeclaration = declaringClass.getSuperclass().getTypeDeclaration();
            return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
        }
        return simpleName.resolveBinding();
    } else if (node instanceof SuperConstructorInvocation) {
        return ((SuperConstructorInvocation) node).resolveConstructorBinding();
    } else if (node instanceof ConstructorInvocation) {
        return ((ConstructorInvocation) node).resolveConstructorBinding();
    } else {
        return null;
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

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