Search in sources :

Example 26 with BodyDeclaration

use of org.eclipse.jdt.core.dom.BodyDeclaration in project eclipse.jdt.ls by eclipse.

the class JavadocTagsSubProcessor method getMissingJavadocCommentProposals.

public static void getMissingJavadocCommentProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) throws CoreException {
    ASTNode node = problem.getCoveringNode(context.getASTRoot());
    if (node == null) {
        return;
    }
    BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
    if (declaration == null) {
        return;
    }
    ICompilationUnit cu = context.getCompilationUnit();
    ITypeBinding binding = Bindings.getBindingOfParentType(declaration);
    if (binding == null) {
        return;
    }
    if (declaration instanceof MethodDeclaration) {
        MethodDeclaration methodDecl = (MethodDeclaration) declaration;
        IMethodBinding methodBinding = methodDecl.resolveBinding();
        IMethodBinding overridden = null;
        if (methodBinding != null) {
            overridden = Bindings.findOverriddenMethod(methodBinding, true);
        }
        String string = CodeGeneration.getMethodComment(cu, binding.getName(), methodDecl, overridden, String.valueOf('\n'));
        if (string != null) {
            String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_method_description;
            proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_METHOD, declaration.getStartPosition(), string));
        }
    } else if (declaration instanceof AbstractTypeDeclaration) {
        String typeQualifiedName = Bindings.getTypeQualifiedName(binding);
        String[] typeParamNames;
        if (declaration instanceof TypeDeclaration) {
            List<TypeParameter> typeParams = ((TypeDeclaration) declaration).typeParameters();
            typeParamNames = new String[typeParams.size()];
            for (int i = 0; i < typeParamNames.length; i++) {
                typeParamNames[i] = (typeParams.get(i)).getName().getIdentifier();
            }
        } else {
            typeParamNames = new String[0];
        }
        String string = CodeGeneration.getTypeComment(cu, typeQualifiedName, typeParamNames, String.valueOf('\n'));
        if (string != null) {
            String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_type_description;
            proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_TYPE, declaration.getStartPosition(), string));
        }
    } else if (declaration instanceof FieldDeclaration) {
        // $NON-NLS-1$
        String comment = "/**\n *\n */\n";
        List<VariableDeclarationFragment> fragments = ((FieldDeclaration) declaration).fragments();
        if (fragments != null && fragments.size() > 0) {
            VariableDeclaration decl = fragments.get(0);
            String fieldName = decl.getName().getIdentifier();
            String typeName = binding.getName();
            comment = CodeGeneration.getFieldComment(cu, typeName, fieldName, String.valueOf('\n'));
        }
        if (comment != null) {
            String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_field_description;
            proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_FIELD, declaration.getStartPosition(), comment));
        }
    } else if (declaration instanceof EnumConstantDeclaration) {
        EnumConstantDeclaration enumDecl = (EnumConstantDeclaration) declaration;
        String id = enumDecl.getName().getIdentifier();
        String comment = CodeGeneration.getFieldComment(cu, binding.getName(), id, String.valueOf('\n'));
        String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_enumconst_description;
        proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_ENUM, declaration.getStartPosition(), comment));
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) List(java.util.List) ArrayList(java.util.ArrayList) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 27 with BodyDeclaration

use of org.eclipse.jdt.core.dom.BodyDeclaration in project eclipse.jdt.ls by eclipse.

the class JavadocTagsSubProcessor method getUnusedAndUndocumentedParameterOrExceptionProposals.

public static void getUnusedAndUndocumentedParameterOrExceptionProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    IJavaProject project = cu.getJavaProject();
    if (!JavaCore.ENABLED.equals(project.getOption(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, true))) {
        return;
    }
    int problemId = problem.getProblemId();
    boolean isUnusedTypeParam = problemId == IProblem.UnusedTypeParameter;
    boolean isUnusedParam = problemId == IProblem.ArgumentIsNeverUsed || isUnusedTypeParam;
    String key = isUnusedParam ? JavaCore.COMPILER_PB_UNUSED_PARAMETER_INCLUDE_DOC_COMMENT_REFERENCE : JavaCore.COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_INCLUDE_DOC_COMMENT_REFERENCE;
    if (!JavaCore.ENABLED.equals(project.getOption(key, true))) {
        return;
    }
    ASTNode node = problem.getCoveringNode(context.getASTRoot());
    if (node == null) {
        return;
    }
    BodyDeclaration bodyDecl = ASTResolving.findParentBodyDeclaration(node);
    if (bodyDecl == null || ASTResolving.getParentMethodOrTypeBinding(bodyDecl) == null) {
        return;
    }
    String label;
    if (isUnusedTypeParam) {
        label = CorrectionMessages.JavadocTagsSubProcessor_document_type_parameter_description;
    } else if (isUnusedParam) {
        label = CorrectionMessages.JavadocTagsSubProcessor_document_parameter_description;
    } else {
        node = ASTNodes.getNormalizedNode(node);
        label = CorrectionMessages.JavadocTagsSubProcessor_document_exception_description;
    }
    ASTRewriteCorrectionProposal proposal = new AddMissingJavadocTagProposal(label, context.getCompilationUnit(), bodyDecl, node, IProposalRelevance.DOCUMENT_UNUSED_ITEM);
    proposals.add(proposal);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaProject(org.eclipse.jdt.core.IJavaProject) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 28 with BodyDeclaration

use of org.eclipse.jdt.core.dom.BodyDeclaration in project eclipse.jdt.ls by eclipse.

the class NewAnnotationMemberProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(fInvocationNode);
    ASTNode typeDecl = astRoot.findDeclaringNode(fSenderBinding);
    ASTNode newTypeDecl = null;
    if (typeDecl != null) {
        newTypeDecl = typeDecl;
    } else {
        astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
        newTypeDecl = astRoot.findDeclaringNode(fSenderBinding.getKey());
    }
    createImportRewrite(astRoot);
    if (newTypeDecl instanceof AnnotationTypeDeclaration) {
        AnnotationTypeDeclaration newAnnotationTypeDecl = (AnnotationTypeDeclaration) newTypeDecl;
        ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
        AnnotationTypeMemberDeclaration newStub = getStub(rewrite, newAnnotationTypeDecl);
        List<BodyDeclaration> members = newAnnotationTypeDecl.bodyDeclarations();
        int insertIndex = members.size();
        ListRewrite listRewriter = rewrite.getListRewrite(newAnnotationTypeDecl, AnnotationTypeDeclaration.BODY_DECLARATIONS_PROPERTY);
        listRewriter.insertAt(newStub, insertIndex, null);
        return rewrite;
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AnnotationTypeMemberDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite)

Example 29 with BodyDeclaration

use of org.eclipse.jdt.core.dom.BodyDeclaration in project eclipse.jdt.ls by eclipse.

the class NewVariableCorrectionProposal method doAddParam.

private ASTRewrite doAddParam(CompilationUnit cu) {
    AST ast = cu.getAST();
    SimpleName node = fOriginalNode;
    BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(node);
    if (decl instanceof MethodDeclaration) {
        MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ImportRewrite imports = createImportRewrite((CompilationUnit) decl.getRoot());
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
        SingleVariableDeclaration newDecl = ast.newSingleVariableDeclaration();
        newDecl.setType(evaluateVariableType(ast, imports, importRewriteContext, methodDeclaration.resolveBinding(), TypeLocation.PARAMETER));
        newDecl.setName(ast.newSimpleName(node.getIdentifier()));
        ListRewrite listRewriter = rewrite.getListRewrite(decl, MethodDeclaration.PARAMETERS_PROPERTY);
        listRewriter.insertLast(newDecl, null);
        // add javadoc tag
        Javadoc javadoc = methodDeclaration.getJavadoc();
        if (javadoc != null) {
            HashSet<String> leadingNames = new HashSet<>();
            for (Iterator<SingleVariableDeclaration> iter = methodDeclaration.parameters().iterator(); iter.hasNext(); ) {
                SingleVariableDeclaration curr = iter.next();
                leadingNames.add(curr.getName().getIdentifier());
            }
            SimpleName newTagRef = ast.newSimpleName(node.getIdentifier());
            TagElement newTagElement = ast.newTagElement();
            newTagElement.setTagName(TagElement.TAG_PARAM);
            newTagElement.fragments().add(newTagRef);
            TextElement commentStart = ast.newTextElement();
            newTagElement.fragments().add(commentStart);
            ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
            JavadocTagsSubProcessor.insertTag(tagsRewriter, newTagElement, leadingNames);
        }
        return rewrite;
    }
    return null;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) TextElement(org.eclipse.jdt.core.dom.TextElement) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) HashSet(java.util.HashSet)

Example 30 with BodyDeclaration

use of org.eclipse.jdt.core.dom.BodyDeclaration in project eclipse.jdt.ls by eclipse.

the class ReturnTypeSubProcessor method addMethodReturnsVoidProposals.

public static void addMethodReturnsVoidProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) throws JavaModelException {
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (!(selectedNode instanceof ReturnStatement)) {
        return;
    }
    ReturnStatement returnStatement = (ReturnStatement) selectedNode;
    Expression expression = returnStatement.getExpression();
    if (expression == null) {
        return;
    }
    BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
    if (decl instanceof MethodDeclaration) {
        MethodDeclaration methDecl = (MethodDeclaration) decl;
        Type retType = methDecl.getReturnType2();
        if (retType == null || retType.resolveBinding() == null) {
            return;
        }
        TypeMismatchSubProcessor.addChangeSenderTypeProposals(context, expression, retType.resolveBinding(), false, IProposalRelevance.METHOD_RETURNS_VOID, proposals);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Aggregations

BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)98 ASTNode (org.eclipse.jdt.core.dom.ASTNode)61 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)53 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)28 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)27 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)26 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)26 Type (org.eclipse.jdt.core.dom.Type)25 AST (org.eclipse.jdt.core.dom.AST)23 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)18 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)17 Expression (org.eclipse.jdt.core.dom.Expression)16 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)15 Initializer (org.eclipse.jdt.core.dom.Initializer)15 SimpleName (org.eclipse.jdt.core.dom.SimpleName)15 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)15 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)14 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)14 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)13 ArrayList (java.util.ArrayList)12