Search in sources :

Example 1 with Annotation

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

the class ModifierRewrite method copyAllAnnotations.

public void copyAllAnnotations(ASTNode otherDecl, TextEditGroup editGroup) {
    ListRewrite modifierList = evaluateListRewrite(fModifierRewrite.getASTRewrite(), otherDecl);
    List<IExtendedModifier> originalList = modifierList.getOriginalList();
    for (Iterator<IExtendedModifier> iterator = originalList.iterator(); iterator.hasNext(); ) {
        IExtendedModifier modifier = iterator.next();
        if (modifier.isAnnotation()) {
            fModifierRewrite.insertLast(fModifierRewrite.getASTRewrite().createCopyTarget((Annotation) modifier), editGroup);
        }
    }
}
Also used : ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Annotation(org.eclipse.jdt.core.dom.Annotation) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 2 with Annotation

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

the class UnresolvedElementsSubProcessor method getAnnotationMemberProposals.

public static void getAnnotationMemberProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> 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));
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_MISC_PUBLIC);
            proposals.add(new NewAnnotationMemberProposal(label, targetCU, selectedNode, annotBinding, IProposalRelevance.CREATE_ATTRIBUTE, image));
        }
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Image(org.eclipse.swt.graphics.Image) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) Annotation(org.eclipse.jdt.core.dom.Annotation) RenameNodeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.RenameNodeCorrectionProposal) NewAnnotationMemberProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.NewAnnotationMemberProposal) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation)

Example 3 with Annotation

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

the class ModifierCorrectionSubProcessor method findAnnotation.

private static Annotation findAnnotation(String qualifiedTypeName, List<IExtendedModifier> modifiers) {
    for (int i = 0; i < modifiers.size(); i++) {
        IExtendedModifier curr = modifiers.get(i);
        if (curr instanceof Annotation) {
            Annotation annot = (Annotation) curr;
            ITypeBinding binding = annot.getTypeName().resolveTypeBinding();
            if (binding != null && qualifiedTypeName.equals(binding.getQualifiedName())) {
                return annot;
            }
        }
    }
    return null;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Annotation(org.eclipse.jdt.core.dom.Annotation) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 4 with Annotation

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

the class ModifierCorrectionSubProcessor method addOverridingDeprecatedMethodProposal.

public static void addOverridingDeprecatedMethodProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof MethodDeclaration)) {
        return;
    }
    boolean is50OrHigher = JavaModelUtil.is50OrHigher(cu.getJavaProject());
    MethodDeclaration methodDecl = (MethodDeclaration) selectedNode;
    AST ast = methodDecl.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    if (is50OrHigher) {
        Annotation annot = ast.newMarkerAnnotation();
        //$NON-NLS-1$
        annot.setTypeName(ast.newName("Deprecated"));
        rewrite.getListRewrite(methodDecl, methodDecl.getModifiersProperty()).insertFirst(annot, null);
    }
    Javadoc javadoc = methodDecl.getJavadoc();
    if (javadoc != null || !is50OrHigher) {
        if (!is50OrHigher) {
            javadoc = ast.newJavadoc();
            rewrite.set(methodDecl, MethodDeclaration.JAVADOC_PROPERTY, javadoc, null);
        }
        TagElement newTag = ast.newTagElement();
        newTag.setTagName(TagElement.TAG_DEPRECATED);
        JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
    }
    String label = CorrectionMessages.ModifierCorrectionSubProcessor_overrides_deprecated_description;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.OVERRIDES_DEPRECATED, image);
    proposals.add(proposal);
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Javadoc(org.eclipse.jdt.core.dom.Javadoc) TagElement(org.eclipse.jdt.core.dom.TagElement) Image(org.eclipse.swt.graphics.Image) Annotation(org.eclipse.jdt.core.dom.Annotation)

Example 5 with Annotation

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

the class NullAnnotationsRewriteOperations method hasNullAnnotation.

private static boolean hasNullAnnotation(MethodDeclaration decl) {
    List<IExtendedModifier> modifiers = decl.modifiers();
    String nonnull = NullAnnotationsFix.getNonNullAnnotationName(decl.resolveBinding().getJavaElement(), false);
    String nullable = NullAnnotationsFix.getNullableAnnotationName(decl.resolveBinding().getJavaElement(), false);
    for (Object mod : modifiers) {
        if (mod instanceof Annotation) {
            Name annotationName = ((Annotation) mod).getTypeName();
            String fullyQualifiedName = annotationName.getFullyQualifiedName();
            if (annotationName.isSimpleName() ? nonnull.endsWith(fullyQualifiedName) : fullyQualifiedName.equals(nonnull))
                return true;
            if (annotationName.isSimpleName() ? nullable.endsWith(fullyQualifiedName) : fullyQualifiedName.equals(nullable))
                return true;
        }
    }
    return false;
}
Also used : MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) Annotation(org.eclipse.jdt.core.dom.Annotation) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Aggregations

Annotation (org.eclipse.jdt.core.dom.Annotation)56 ASTNode (org.eclipse.jdt.core.dom.ASTNode)20 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)19 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)14 SingleMemberAnnotation (org.eclipse.jdt.core.dom.SingleMemberAnnotation)14 NormalAnnotation (org.eclipse.jdt.core.dom.NormalAnnotation)13 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)11 ArrayList (java.util.ArrayList)10 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)10 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)8 List (java.util.List)7 AST (org.eclipse.jdt.core.dom.AST)7 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)6 ArrayType (org.eclipse.jdt.core.dom.ArrayType)5 IAnnotationBinding (org.eclipse.jdt.core.dom.IAnnotationBinding)5 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)5 MemberValuePair (org.eclipse.jdt.core.dom.MemberValuePair)5 Name (org.eclipse.jdt.core.dom.Name)5 Type (org.eclipse.jdt.core.dom.Type)5 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)4