Search in sources :

Example 11 with ASTNode

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

the class UnresolvedElementsSubProcessor method getArrayAccessProposals.

public static void getArrayAccessProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    CompilationUnit root = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(root);
    if (!(selectedNode instanceof MethodInvocation)) {
        return;
    }
    MethodInvocation decl = (MethodInvocation) selectedNode;
    SimpleName nameNode = decl.getName();
    String methodName = nameNode.getIdentifier();
    IBinding[] bindings = (new ScopeAnalyzer(root)).getDeclarationsInScope(nameNode, ScopeAnalyzer.METHODS);
    for (int i = 0; i < bindings.length; i++) {
        String currName = bindings[i].getName();
        if (NameMatcher.isSimilarName(methodName, currName)) {
            String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_arraychangetomethod_description, BasicElementLabels.getJavaElementName(currName));
            proposals.add(new RenameNodeCorrectionProposal(label, context.getCompilationUnit(), nameNode.getStartPosition(), nameNode.getLength(), currName, IProposalRelevance.ARRAY_CHANGE_TO_METHOD));
        }
    }
    // always suggest 'length'
    //$NON-NLS-1$
    String lengthId = "length";
    String label = CorrectionMessages.UnresolvedElementsSubProcessor_arraychangetolength_description;
    int offset = nameNode.getStartPosition();
    int length = decl.getStartPosition() + decl.getLength() - offset;
    proposals.add(new RenameNodeCorrectionProposal(label, context.getCompilationUnit(), offset, length, lengthId, IProposalRelevance.ARRAY_CHANGE_TO_LENGTH));
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RenameNodeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.RenameNodeCorrectionProposal) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ScopeAnalyzer(org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Example 12 with ASTNode

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

the class UnresolvedElementsSubProcessor method addEnhancedForWithoutTypeProposals.

private static void addEnhancedForWithoutTypeProposals(ICompilationUnit cu, ASTNode selectedNode, Collection<ICommandAccess> proposals) {
    if (selectedNode instanceof SimpleName && (selectedNode.getLocationInParent() == SimpleType.NAME_PROPERTY || selectedNode.getLocationInParent() == NameQualifiedType.NAME_PROPERTY)) {
        ASTNode type = selectedNode.getParent();
        if (type.getLocationInParent() == SingleVariableDeclaration.TYPE_PROPERTY) {
            SingleVariableDeclaration svd = (SingleVariableDeclaration) type.getParent();
            if (svd.getLocationInParent() == EnhancedForStatement.PARAMETER_PROPERTY) {
                if (svd.getName().getLength() == 0) {
                    SimpleName simpleName = (SimpleName) selectedNode;
                    String name = simpleName.getIdentifier();
                    int relevance = StubUtility.hasLocalVariableName(cu.getJavaProject(), name) ? 10 : 7;
                    String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_create_loop_variable_description, BasicElementLabels.getJavaElementName(name));
                    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
                    proposals.add(new NewVariableCorrectionProposal(label, cu, NewVariableCorrectionProposal.LOCAL, simpleName, null, relevance, image));
                }
            }
        }
    }
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NewVariableCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposal) Image(org.eclipse.swt.graphics.Image)

Example 13 with ASTNode

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

the class UnresolvedElementsSubProcessor method addNewVariableProposals.

private static void addNewVariableProposals(ICompilationUnit cu, Name node, SimpleName simpleName, Collection<ICommandAccess> proposals) {
    String name = simpleName.getIdentifier();
    BodyDeclaration bodyDeclaration = ASTResolving.findParentBodyDeclaration(node, true);
    int type = bodyDeclaration.getNodeType();
    if (type == ASTNode.METHOD_DECLARATION) {
        int relevance = StubUtility.hasParameterName(cu.getJavaProject(), name) ? IProposalRelevance.CREATE_PARAMETER_PREFIX_OR_SUFFIX_MATCH : IProposalRelevance.CREATE_PARAMETER;
        String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createparameter_description, BasicElementLabels.getJavaElementName(name));
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
        proposals.add(new NewVariableCorrectionProposal(label, cu, NewVariableCorrectionProposal.PARAM, simpleName, null, relevance, image));
    }
    if (type == ASTNode.INITIALIZER || type == ASTNode.METHOD_DECLARATION && !ASTResolving.isInsideConstructorInvocation((MethodDeclaration) bodyDeclaration, node)) {
        int relevance = StubUtility.hasLocalVariableName(cu.getJavaProject(), name) ? IProposalRelevance.CREATE_LOCAL_PREFIX_OR_SUFFIX_MATCH : IProposalRelevance.CREATE_LOCAL;
        String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createlocal_description, BasicElementLabels.getJavaElementName(name));
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
        proposals.add(new NewVariableCorrectionProposal(label, cu, NewVariableCorrectionProposal.LOCAL, simpleName, null, relevance, image));
    }
    if (node.getParent().getNodeType() == ASTNode.ASSIGNMENT) {
        Assignment assignment = (Assignment) node.getParent();
        if (assignment.getLeftHandSide() == node && assignment.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
            ASTNode statement = assignment.getParent();
            ASTRewrite rewrite = ASTRewrite.create(statement.getAST());
            if (ASTNodes.isControlStatementBody(assignment.getParent().getLocationInParent())) {
                rewrite.replace(statement, rewrite.getAST().newBlock(), null);
            } else {
                rewrite.remove(statement, null);
            }
            String label = CorrectionMessages.UnresolvedElementsSubProcessor_removestatement_description;
            //JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_TOOL_DELETE);
            ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_ASSIGNMENT, image);
            proposals.add(proposal);
        }
    }
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) NewVariableCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) Image(org.eclipse.swt.graphics.Image)

Example 14 with ASTNode

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

the class ReturnTypeSubProcessor method addMissingReturnStatementProposals.

public static void addMissingReturnStatementProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode == null) {
        return;
    }
    ReturnStatement existingStatement = (selectedNode instanceof ReturnStatement) ? (ReturnStatement) selectedNode : null;
    // Lambda Expression can be in a MethodDeclaration or a Field Declaration
    if (selectedNode instanceof LambdaExpression) {
        MissingReturnTypeInLambdaCorrectionProposal proposal = new MissingReturnTypeInLambdaCorrectionProposal(cu, (LambdaExpression) selectedNode, existingStatement, IProposalRelevance.MISSING_RETURN_TYPE);
        proposals.add(proposal);
    } else {
        BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
        if (decl instanceof MethodDeclaration) {
            MethodDeclaration methodDecl = (MethodDeclaration) decl;
            Block block = methodDecl.getBody();
            if (block == null) {
                return;
            }
            proposals.add(new MissingReturnTypeCorrectionProposal(cu, methodDecl, existingStatement, IProposalRelevance.MISSING_RETURN_TYPE));
            Type returnType = methodDecl.getReturnType2();
            if (returnType != null && !"void".equals(ASTNodes.asString(returnType))) {
                //$NON-NLS-1$
                AST ast = methodDecl.getAST();
                ASTRewrite rewrite = ASTRewrite.create(ast);
                rewrite.replace(returnType, ast.newPrimitiveType(PrimitiveType.VOID), null);
                Javadoc javadoc = methodDecl.getJavadoc();
                if (javadoc != null) {
                    TagElement tagElement = JavadocTagsSubProcessor.findTag(javadoc, TagElement.TAG_RETURN, null);
                    if (tagElement != null) {
                        rewrite.remove(tagElement, null);
                    }
                }
                String label = CorrectionMessages.ReturnTypeSubProcessor_changetovoid_description;
                Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
                ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.CHANGE_RETURN_TYPE_TO_VOID, image);
                proposals.add(proposal);
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) MissingReturnTypeInLambdaCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.MissingReturnTypeInLambdaCorrectionProposal) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) MissingReturnTypeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.MissingReturnTypeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 15 with ASTNode

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

the class ReturnTypeSubProcessor method addMethodWithConstrNameProposals.

public static void addMethodWithConstrNameProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode instanceof MethodDeclaration) {
        MethodDeclaration declaration = (MethodDeclaration) selectedNode;
        ASTRewrite rewrite = ASTRewrite.create(declaration.getAST());
        rewrite.set(declaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null);
        String label = CorrectionMessages.ReturnTypeSubProcessor_constrnamemethod_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.CHANGE_TO_CONSTRUCTOR, image);
        proposals.add(proposal);
    }
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Image(org.eclipse.swt.graphics.Image)

Aggregations

ASTNode (org.eclipse.jdt.core.dom.ASTNode)710 SimpleName (org.eclipse.jdt.core.dom.SimpleName)151 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)148 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)147 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)134 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)126 Expression (org.eclipse.jdt.core.dom.Expression)125 ArrayList (java.util.ArrayList)111 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)108 AST (org.eclipse.jdt.core.dom.AST)106 Type (org.eclipse.jdt.core.dom.Type)92 Block (org.eclipse.jdt.core.dom.Block)89 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)77 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)73 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)67 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)66 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)65 CastExpression (org.eclipse.jdt.core.dom.CastExpression)64 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)62 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)60