Search in sources :

Example 21 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit 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 22 with ICompilationUnit

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

Example 23 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class ReturnTypeSubProcessor method addMissingReturnTypeProposals.

public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null) {
        return;
    }
    BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
    if (decl instanceof MethodDeclaration) {
        MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
        ReturnStatementCollector eval = new ReturnStatementCollector();
        decl.accept(eval);
        AST ast = astRoot.getAST();
        ITypeBinding typeBinding = eval.getTypeBinding(decl.getAST());
        typeBinding = Bindings.normalizeTypeBinding(typeBinding);
        if (typeBinding == null) {
            //$NON-NLS-1$
            typeBinding = ast.resolveWellKnownType("void");
        }
        if (typeBinding.isWildcardType()) {
            typeBinding = ASTResolving.normalizeWildcardType(typeBinding, true, ast);
        }
        ASTRewrite rewrite = ASTRewrite.create(ast);
        String label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProvider.getBindingLabel(typeBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE, image);
        ImportRewrite imports = proposal.createImportRewrite(astRoot);
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
        Type type = imports.addImport(typeBinding, ast, importRewriteContext);
        rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
        rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);
        Javadoc javadoc = methodDeclaration.getJavadoc();
        if (javadoc != null && typeBinding != null) {
            TagElement newTag = ast.newTagElement();
            newTag.setTagName(TagElement.TAG_RETURN);
            TextElement commentStart = ast.newTextElement();
            newTag.fragments().add(commentStart);
            JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
            //$NON-NLS-1$
            proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
        }
        //$NON-NLS-1$
        String key = "return_type";
        proposal.addLinkedPosition(rewrite.track(type), true, key);
        if (typeBinding != null) {
            ITypeBinding[] bindings = ASTResolving.getRelaxingTypes(ast, typeBinding);
            for (int i = 0; i < bindings.length; i++) {
                proposal.addLinkedPositionProposal(key, bindings[i]);
            }
        }
        proposals.add(proposal);
        // change to constructor
        ASTNode parentType = ASTResolving.findParentType(decl);
        if (parentType instanceof AbstractTypeDeclaration) {
            boolean isInterface = parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
            if (!isInterface) {
                String constructorName = ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
                ASTNode nameNode = methodDeclaration.getName();
                label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
                proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, IProposalRelevance.CHANGE_TO_CONSTRUCTOR));
            }
        }
    }
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Image(org.eclipse.swt.graphics.Image) TextElement(org.eclipse.jdt.core.dom.TextElement) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) ReplaceCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal)

Example 24 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class SimilarElementsRequestor method findSimilarElement.

public static SimilarElement[] findSimilarElement(ICompilationUnit cu, Name name, int kind) throws JavaModelException {
    int pos = name.getStartPosition();
    int nArguments = -1;
    String identifier = ASTNodes.getSimpleNameIdentifier(name);
    String returnType = null;
    ICompilationUnit preparedCU = null;
    try {
        if (name.isQualifiedName()) {
            pos = ((QualifiedName) name).getName().getStartPosition();
        } else {
            // first letter must be included, other
            pos = name.getStartPosition() + 1;
        }
        Javadoc javadoc = (Javadoc) ASTNodes.getParent(name, ASTNode.JAVADOC);
        if (javadoc != null) {
            preparedCU = createPreparedCU(cu, javadoc, name.getStartPosition());
            cu = preparedCU;
        }
        SimilarElementsRequestor requestor = new SimilarElementsRequestor(identifier, kind, nArguments, returnType);
        requestor.setIgnored(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION, true);
        requestor.setIgnored(CompletionProposal.KEYWORD, true);
        requestor.setIgnored(CompletionProposal.LABEL_REF, true);
        requestor.setIgnored(CompletionProposal.METHOD_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.PACKAGE_REF, true);
        requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.METHOD_REF, true);
        requestor.setIgnored(CompletionProposal.CONSTRUCTOR_INVOCATION, true);
        requestor.setIgnored(CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER, true);
        requestor.setIgnored(CompletionProposal.FIELD_REF, true);
        requestor.setIgnored(CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER, true);
        requestor.setIgnored(CompletionProposal.LOCAL_VARIABLE_REF, true);
        requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.POTENTIAL_METHOD_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.METHOD_NAME_REFERENCE, true);
        return requestor.process(cu, pos);
    } finally {
        if (preparedCU != null) {
            preparedCU.discardWorkingCopy();
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Javadoc(org.eclipse.jdt.core.dom.Javadoc)

Example 25 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class SimilarElementsRequestor method getStaticImportFavorites.

public static String[] getStaticImportFavorites(ICompilationUnit cu, final String elementName, boolean isMethod, String[] favorites) throws JavaModelException {
    StringBuffer dummyCU = new StringBuffer();
    String packName = cu.getParent().getElementName();
    IType type = cu.findPrimaryType();
    if (type == null)
        return new String[0];
    if (packName.length() > 0) {
        //$NON-NLS-1$
        dummyCU.append("package ").append(packName).append(';');
    }
    // static initializer  //$NON-NLS-1$//$NON-NLS-2$
    dummyCU.append("public class ").append(type.getElementName()).append("{\n static {\n").append(elementName);
    int offset = dummyCU.length();
    //$NON-NLS-1$
    dummyCU.append("\n}\n }");
    ICompilationUnit newCU = null;
    try {
        newCU = cu.getWorkingCopy(null);
        newCU.getBuffer().setContents(dummyCU.toString());
        final HashSet<String> result = new HashSet<String>();
        CompletionRequestor requestor = new CompletionRequestor(true) {

            @Override
            public void accept(CompletionProposal proposal) {
                if (elementName.equals(new String(proposal.getName()))) {
                    CompletionProposal[] requiredProposals = proposal.getRequiredProposals();
                    for (int i = 0; i < requiredProposals.length; i++) {
                        CompletionProposal curr = requiredProposals[i];
                        if (curr.getKind() == CompletionProposal.METHOD_IMPORT || curr.getKind() == CompletionProposal.FIELD_IMPORT) {
                            result.add(JavaModelUtil.concatenateName(Signature.toCharArray(curr.getDeclarationSignature()), curr.getName()));
                        }
                    }
                }
            }
        };
        if (isMethod) {
            requestor.setIgnored(CompletionProposal.METHOD_REF, false);
            requestor.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);
        } else {
            requestor.setIgnored(CompletionProposal.FIELD_REF, false);
            requestor.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);
        }
        requestor.setFavoriteReferences(favorites);
        newCU.codeComplete(offset, requestor);
        return result.toArray(new String[result.size()]);
    } finally {
        if (newCU != null) {
            newCU.discardWorkingCopy();
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionProposal(org.eclipse.jdt.core.CompletionProposal) CompletionRequestor(org.eclipse.jdt.core.CompletionRequestor) IType(org.eclipse.jdt.core.IType) HashSet(java.util.HashSet)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1975 Test (org.junit.Test)1482 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1455 ArrayList (java.util.ArrayList)837 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)548 List (java.util.List)476 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)385 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)284 Hashtable (java.util.Hashtable)143 IType (org.eclipse.jdt.core.IType)135 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)103 ASTNode (org.eclipse.jdt.core.dom.ASTNode)97 JavaModelException (org.eclipse.jdt.core.JavaModelException)82 IJavaElement (org.eclipse.jdt.core.IJavaElement)81 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)70 IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)70 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)65 IJavaProject (org.eclipse.jdt.core.IJavaProject)64 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)62 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)56