Search in sources :

Example 41 with SimpleName

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

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

the class Java50Fix method getRawReference.

private static SimpleType getRawReference(SimpleName name, CompilationUnit compilationUnit) {
    SimpleName[] names = LinkedNodeFinder.findByNode(compilationUnit, name);
    for (int j = 0; j < names.length; j++) {
        if (names[j].getParent() instanceof VariableDeclarationFragment) {
            VariableDeclarationFragment fragment = (VariableDeclarationFragment) names[j].getParent();
            if (fragment.getParent() instanceof VariableDeclarationStatement) {
                VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
                ASTNode result = (ASTNode) statement.getStructuralProperty(VariableDeclarationStatement.TYPE_PROPERTY);
                if (isRawTypeReference(result))
                    return (SimpleType) result;
            } else if (fragment.getParent() instanceof FieldDeclaration) {
                FieldDeclaration declaration = (FieldDeclaration) fragment.getParent();
                ASTNode result = (ASTNode) declaration.getStructuralProperty(FieldDeclaration.TYPE_PROPERTY);
                if (isRawTypeReference(result))
                    return (SimpleType) result;
            }
        } else if (names[j].getParent() instanceof SingleVariableDeclaration) {
            SingleVariableDeclaration declaration = (SingleVariableDeclaration) names[j].getParent();
            ASTNode result = (ASTNode) declaration.getStructuralProperty(SingleVariableDeclaration.TYPE_PROPERTY);
            if (isRawTypeReference(result))
                return (SimpleType) result;
        } else if (names[j].getParent() instanceof MethodDeclaration) {
            MethodDeclaration methodDecl = (MethodDeclaration) names[j].getParent();
            ASTNode result = (ASTNode) methodDecl.getStructuralProperty(MethodDeclaration.RETURN_TYPE2_PROPERTY);
            if (isRawTypeReference(result))
                return (SimpleType) result;
        }
    }
    return null;
}
Also used : SimpleType(org.eclipse.jdt.core.dom.SimpleType) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 43 with SimpleName

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

the class PotentialProgrammingProblemsFix method createMissingSerialVersionFixes.

public static IProposableFix[] createMissingSerialVersionFixes(CompilationUnit compilationUnit, IProblemLocation problem) {
    if (problem.getProblemId() != IProblem.MissingSerialVersion)
        return null;
    final ICompilationUnit unit = (ICompilationUnit) compilationUnit.getJavaElement();
    if (unit == null)
        return null;
    final SimpleName simpleName = getSelectedName(compilationUnit, problem);
    if (simpleName == null)
        return null;
    ASTNode declaringNode = getDeclarationNode(simpleName);
    if (declaringNode == null)
        return null;
    SerialVersionDefaultOperation defop = new SerialVersionDefaultOperation(unit, new ASTNode[] { declaringNode });
    IProposableFix fix1 = new PotentialProgrammingProblemsFix(FixMessages.Java50Fix_SerialVersion_default_description, compilationUnit, new CompilationUnitRewriteOperation[] { defop });
    return new IProposableFix[] { fix1 };
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 44 with SimpleName

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

the class PotentialProgrammingProblemsFix method createCleanUp.

public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, IProblemLocation[] problems, boolean addSerialVersionIds) {
    if (addSerialVersionIds) {
        final ICompilationUnit unit = (ICompilationUnit) compilationUnit.getJavaElement();
        if (unit == null)
            return null;
        List<ASTNode> declarationNodes = new ArrayList<ASTNode>();
        for (int i = 0; i < problems.length; i++) {
            if (problems[i].getProblemId() == IProblem.MissingSerialVersion) {
                final SimpleName simpleName = getSelectedName(compilationUnit, problems[i]);
                if (simpleName != null) {
                    ASTNode declarationNode = getDeclarationNode(simpleName);
                    if (declarationNode != null) {
                        declarationNodes.add(declarationNode);
                    }
                }
            }
        }
        if (declarationNodes.size() == 0)
            return null;
        for (Iterator<ASTNode> iter = declarationNodes.iterator(); iter.hasNext(); ) {
            ASTNode declarationNode = iter.next();
            ITypeBinding binding = getTypeBinding(declarationNode);
            if (fCurrentContext.getSerialVersionId(binding) != null) {
                SerialVersionHashBatchOperation op = new SerialVersionHashBatchOperation(unit, declarationNodes.toArray(new ASTNode[declarationNodes.size()]), fCurrentContext);
                return new PotentialProgrammingProblemsFix(FixMessages.PotentialProgrammingProblemsFix_add_id_change_name, compilationUnit, new CompilationUnitRewriteOperation[] { op });
            }
        }
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 45 with SimpleName

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

the class CodeStyleFix method getUnqualifiedFieldAccessResolveOperation.

private static AddThisQualifierOperation getUnqualifiedFieldAccessResolveOperation(CompilationUnit compilationUnit, IProblemLocation problem) {
    SimpleName name = getName(compilationUnit, problem);
    if (name == null)
        return null;
    IBinding binding = name.resolveBinding();
    if (binding == null || binding.getKind() != IBinding.VARIABLE)
        return null;
    ImportRewrite imports = StubUtility.createImportRewrite(compilationUnit, true);
    String replacement = getThisExpressionQualifier(((IVariableBinding) binding).getDeclaringClass(), imports, name);
    if (replacement == null)
        return null;
    if (replacement.length() == 0)
        replacement = null;
    return new AddThisQualifierOperation(replacement, name);
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) 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