Search in sources :

Example 11 with NormalAnnotation

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

the class MissingAnnotationAttributesProposal method addMissingAtributes.

private void addMissingAtributes(ITypeBinding binding, ListRewrite listRewriter) {
    Set<String> implementedAttribs = new HashSet<String>();
    if (fAnnotation instanceof NormalAnnotation) {
        List<MemberValuePair> list = ((NormalAnnotation) fAnnotation).values();
        for (int i = 0; i < list.size(); i++) {
            MemberValuePair curr = list.get(i);
            implementedAttribs.add(curr.getName().getIdentifier());
        }
    } else if (fAnnotation instanceof SingleMemberAnnotation) {
        //$NON-NLS-1$
        implementedAttribs.add("value");
    }
    ASTRewrite rewriter = listRewriter.getASTRewrite();
    AST ast = rewriter.getAST();
    ImportRewriteContext context = null;
    ASTNode bodyDeclaration = ASTResolving.findParentBodyDeclaration(listRewriter.getParent());
    if (bodyDeclaration != null) {
        context = new ContextSensitiveImportRewriteContext(bodyDeclaration, getImportRewrite());
    }
    IMethodBinding[] declaredMethods = binding.getDeclaredMethods();
    for (int i = 0; i < declaredMethods.length; i++) {
        IMethodBinding curr = declaredMethods[i];
        if (!implementedAttribs.contains(curr.getName()) && curr.getDefaultValue() == null) {
            MemberValuePair pair = ast.newMemberValuePair();
            pair.setName(ast.newSimpleName(curr.getName()));
            pair.setValue(newDefaultExpression(ast, curr.getReturnType(), context));
            listRewriter.insertLast(pair, null);
            //$NON-NLS-1$
            addLinkedPosition(rewriter.track(pair.getName()), false, "val_name_" + i);
            //$NON-NLS-1$
            addLinkedPosition(rewriter.track(pair.getValue()), false, "val_type_" + i);
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) AST(org.eclipse.jdt.core.dom.AST) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) HashSet(java.util.HashSet)

Example 12 with NormalAnnotation

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

the class MissingAnnotationAttributesProposal method getRewrite.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
	 */
@Override
protected ASTRewrite getRewrite() throws CoreException {
    AST ast = fAnnotation.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    createImportRewrite((CompilationUnit) fAnnotation.getRoot());
    ListRewrite listRewrite;
    if (fAnnotation instanceof NormalAnnotation) {
        listRewrite = rewrite.getListRewrite(fAnnotation, NormalAnnotation.VALUES_PROPERTY);
    } else {
        NormalAnnotation newAnnotation = ast.newNormalAnnotation();
        newAnnotation.setTypeName((Name) rewrite.createMoveTarget(fAnnotation.getTypeName()));
        rewrite.replace(fAnnotation, newAnnotation, null);
        listRewrite = rewrite.getListRewrite(newAnnotation, NormalAnnotation.VALUES_PROPERTY);
    }
    addMissingAtributes(fAnnotation.resolveTypeBinding(), listRewrite);
    return rewrite;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite)

Example 13 with NormalAnnotation

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

the class SuppressWarningsSubProcessor method addRemoveUnusedSuppressWarningProposals.

public static void addRemoveUnusedSuppressWarningProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ASTNode coveringNode = problem.getCoveringNode(context.getASTRoot());
    if (!(coveringNode instanceof StringLiteral))
        return;
    StringLiteral literal = (StringLiteral) coveringNode;
    if (coveringNode.getParent() instanceof MemberValuePair) {
        coveringNode = coveringNode.getParent();
    }
    ASTNode parent = coveringNode.getParent();
    ASTRewrite rewrite = ASTRewrite.create(coveringNode.getAST());
    if (parent instanceof SingleMemberAnnotation) {
        rewrite.remove(parent, null);
    } else if (parent instanceof NormalAnnotation) {
        NormalAnnotation annot = (NormalAnnotation) parent;
        if (annot.values().size() == 1) {
            rewrite.remove(annot, null);
        } else {
            rewrite.remove(coveringNode, null);
        }
    } else if (parent instanceof ArrayInitializer) {
        rewrite.remove(coveringNode, null);
    } else {
        return;
    }
    String label = Messages.format(CorrectionMessages.SuppressWarningsSubProcessor_remove_annotation_label, literal.getLiteralValue());
    //JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_TOOL_DELETE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_ANNOTATION, image);
    proposals.add(proposal);
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) Image(org.eclipse.swt.graphics.Image) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 14 with NormalAnnotation

use of org.eclipse.jdt.core.dom.NormalAnnotation in project fabric8 by jboss-fuse.

the class AddSwaggerAnnotationMojo method addSwaggerApiAnnotation.

private void addSwaggerApiAnnotation(CompilationUnit unit, AnnotationVisitor visitor, File file, Document document) throws JavaModelException, IllegalArgumentException, MalformedTreeException, BadLocationException, IOException {
    AST ast = unit.getAST();
    ASTRewrite rewriter = ASTRewrite.create(ast);
    ListRewrite listRewrite = rewriter.getListRewrite(unit, CompilationUnit.TYPES_PROPERTY);
    NormalAnnotation normalAnnotation = rewriter.getAST().newNormalAnnotation();
    Name name = ast.newName("com.wordnik.swagger.annotations.Api");
    normalAnnotation.setTypeName(name);
    MemberValuePair memberValuePair = ast.newMemberValuePair();
    memberValuePair.setName(ast.newSimpleName("value"));
    StringLiteral stringLiteral = ast.newStringLiteral();
    String rootPath = visitor.getRootPath();
    rootPath = rootPath.substring(1, rootPath.length() - 1);
    if (rootPath.endsWith("/")) {
        rootPath = rootPath.substring(0, rootPath.length() - 1);
    }
    stringLiteral.setLiteralValue(rootPath);
    memberValuePair.setValue(stringLiteral);
    normalAnnotation.values().add(memberValuePair);
    memberValuePair = ast.newMemberValuePair();
    memberValuePair.setName(ast.newSimpleName("description"));
    stringLiteral = ast.newStringLiteral();
    stringLiteral.setLiteralValue("Operations about " + visitor.getRestServiceClass());
    memberValuePair.setValue(stringLiteral);
    normalAnnotation.values().add(memberValuePair);
    listRewrite.insertAt(normalAnnotation, 0, null);
    for (MethodDeclaration method : visitor.getRestMethod()) {
        listRewrite = rewriter.getListRewrite(method, MethodDeclaration.MODIFIERS2_PROPERTY);
        normalAnnotation = rewriter.getAST().newNormalAnnotation();
        name = ast.newName("com.wordnik.swagger.annotations.ApiOperation");
        normalAnnotation.setTypeName(name);
        memberValuePair = ast.newMemberValuePair();
        memberValuePair.setName(ast.newSimpleName("value"));
        stringLiteral = ast.newStringLiteral();
        stringLiteral.setLiteralValue(method.getName().toString());
        memberValuePair.setValue(stringLiteral);
        normalAnnotation.values().add(memberValuePair);
        Javadoc doc = method.getJavadoc();
        String comment = null;
        if (doc != null) {
            comment = method.getJavadoc().toString();
        }
        if (comment != null && comment.length() > 0) {
            // add notes from method java doc
            memberValuePair = ast.newMemberValuePair();
            memberValuePair.setName(ast.newSimpleName("notes"));
            stringLiteral = ast.newStringLiteral();
            stringLiteral.setLiteralValue(comment);
            memberValuePair.setValue(stringLiteral);
            normalAnnotation.values().add(memberValuePair);
        }
        listRewrite.insertAt(normalAnnotation, 0, null);
        listRewrite = rewriter.getListRewrite((ASTNode) ((List) method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY)).get(0), SingleVariableDeclaration.MODIFIERS2_PROPERTY);
        normalAnnotation = rewriter.getAST().newNormalAnnotation();
        name = ast.newName("com.wordnik.swagger.annotations.ApiParam");
        normalAnnotation.setTypeName(name);
        ((VariableDeclaration) ((List) method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY)).get(0)).getName();
        memberValuePair = ast.newMemberValuePair();
        memberValuePair.setName(ast.newSimpleName("value"));
        stringLiteral = ast.newStringLiteral();
        stringLiteral.setLiteralValue(((VariableDeclaration) ((List) method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY)).get(0)).getName().toString());
        memberValuePair.setValue(stringLiteral);
        normalAnnotation.values().add(memberValuePair);
        listRewrite.insertAt(normalAnnotation, 0, null);
    }
    TextEdit edits = rewriter.rewriteAST(document, null);
    edits.apply(document);
    FileUtils.writeStringToFile(file, document.get());
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Name(org.eclipse.jdt.core.dom.Name) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) TextEdit(org.eclipse.text.edits.TextEdit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Example 15 with NormalAnnotation

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

the class UnresolvedElementsSubProcessor method getAnnotationMemberProposals.

public static void getAnnotationMemberProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) throws CoreException {
    CompilationUnit astRoot = context.getASTRoot();
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    Annotation annotation;
    String memberName;
    if (selectedNode.getLocationInParent() == MemberValuePair.NAME_PROPERTY) {
        if (selectedNode.getParent().getLocationInParent() != NormalAnnotation.VALUES_PROPERTY) {
            return;
        }
        annotation = (Annotation) selectedNode.getParent().getParent();
        memberName = ((SimpleName) selectedNode).getIdentifier();
    } else if (selectedNode.getLocationInParent() == SingleMemberAnnotation.VALUE_PROPERTY) {
        annotation = (Annotation) selectedNode.getParent();
        // $NON-NLS-1$
        memberName = "value";
    } else {
        return;
    }
    ITypeBinding annotBinding = annotation.resolveTypeBinding();
    if (annotBinding == null) {
        return;
    }
    if (annotation instanceof NormalAnnotation) {
        // similar names
        IMethodBinding[] otherMembers = annotBinding.getDeclaredMethods();
        for (int i = 0; i < otherMembers.length; i++) {
            IMethodBinding binding = otherMembers[i];
            String curr = binding.getName();
            int relevance = NameMatcher.isSimilarName(memberName, curr) ? IProposalRelevance.CHANGE_TO_ATTRIBUTE_SIMILAR_NAME : IProposalRelevance.CHANGE_TO_ATTRIBUTE;
            String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_UnresolvedElementsSubProcessor_changetoattribute_description, BasicElementLabels.getJavaElementName(curr));
            proposals.add(new RenameNodeCorrectionProposal(label, cu, problem.getOffset(), problem.getLength(), curr, relevance));
        }
    }
    if (annotBinding.isFromSource()) {
        ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, annotBinding);
        if (targetCU != null) {
            String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_UnresolvedElementsSubProcessor_createattribute_description, BasicElementLabels.getJavaElementName(memberName));
            proposals.add(new NewAnnotationMemberProposal(label, targetCU, selectedNode, annotBinding, IProposalRelevance.CREATE_ATTRIBUTE));
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) Annotation(org.eclipse.jdt.core.dom.Annotation) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation)

Aggregations

NormalAnnotation (org.eclipse.jdt.core.dom.NormalAnnotation)23 MemberValuePair (org.eclipse.jdt.core.dom.MemberValuePair)13 SingleMemberAnnotation (org.eclipse.jdt.core.dom.SingleMemberAnnotation)10 ASTNode (org.eclipse.jdt.core.dom.ASTNode)9 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)8 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)7 Annotation (org.eclipse.jdt.core.dom.Annotation)6 Expression (org.eclipse.jdt.core.dom.Expression)6 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)6 AST (org.eclipse.jdt.core.dom.AST)5 StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)4 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)4 HashSet (java.util.HashSet)3 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)3 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)3 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)3 ImportDeclaration (org.eclipse.jdt.core.dom.ImportDeclaration)3 PackageDeclaration (org.eclipse.jdt.core.dom.PackageDeclaration)3