Example usage for org.eclipse.jdt.core IAnnotatable getAnnotation

List of usage examples for org.eclipse.jdt.core IAnnotatable getAnnotation

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IAnnotatable getAnnotation.

Prototype

IAnnotation getAnnotation(String name);

Source Link

Document

Returns the annotation with the given name declared on this element.

Usage

From source file:de.devboost.eclipse.jdtutilities.JDTUtility.java

License:Open Source License

/**
 * Returns the value of the given annotation property. If no annotation or
 * property is found, null is returned.//ww w  .  ja  v  a 2 s  . c  o m
 */
public Object getAnnotationValue(IAnnotatable annotable, String simpleAnnotationName,
        String annotationProperty) {
    IAnnotation annotation = annotable.getAnnotation(simpleAnnotationName);
    if (annotation == null) {
        return null;
    }
    return getAnnotationPropertyValue(annotation, annotationProperty);
}

From source file:org.jboss.tools.seam.text.ext.hyperlink.SeamComponentHyperlinkDetector.java

License:Open Source License

public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region,
        boolean canShowMultipleHyperlinks) {
    ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class);
    if (region == null ||
    //            canShowMultipleHyperlinks || 
            !(textEditor instanceof JavaEditor))
        return null;

    int offset = region.getOffset();

    IJavaElement input = EditorUtility.getEditorInputJavaElement(textEditor, false);
    if (input == null)
        return null;

    if (input.getResource() == null || input.getResource().getProject() == null)
        return null;

    ISeamProject seamProject = SeamCorePlugin.getSeamProject(input.getResource().getProject(), true);
    if (seamProject == null) {
        return null;
    }/*  w  w w.j a  va2s. c  o  m*/
    SeamELCompletionEngine engine = new SeamELCompletionEngine();

    IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    IRegion wordRegion = JavaWordFinder.findWord(document, offset);
    if (wordRegion == null)
        return null;

    IFile file = null;

    try {
        IResource resource = input.getCorrespondingResource();
        if (resource instanceof IFile)
            file = (IFile) resource;
    } catch (JavaModelException e) {
        // It is probably because of Java element's resource is not found
        SeamExtPlugin.getDefault().logError(e);
    }

    int[] range = new int[] { wordRegion.getOffset(), wordRegion.getOffset() + wordRegion.getLength() };

    IJavaElement[] elements = null;

    try {
        elements = ((ICodeAssist) input).codeSelect(wordRegion.getOffset(), wordRegion.getLength());
        if (elements == null)
            return null;
        if (elements.length != 1)
            return null;

        ArrayList<IHyperlink> hyperlinks = new ArrayList<IHyperlink>();
        if (elements[0] instanceof IAnnotatable) {
            IAnnotatable annotatable = (IAnnotatable) elements[0];

            IAnnotation annotation = annotatable.getAnnotation("In");
            if (annotation == null || !annotation.exists())
                return null;

            String nameToSearch = elements[0].getElementName();

            IMemberValuePair[] mvPairs = annotation.getMemberValuePairs();
            if (mvPairs != null) {
                for (IMemberValuePair mvPair : mvPairs) {
                    if ("value".equals(mvPair.getMemberName()) && mvPair.getValue() != null) {
                        String name = mvPair.getValue().toString();
                        if (name != null && name.trim().length() != 0) {
                            nameToSearch = name;
                            break;
                        }
                    }
                }
            }

            if (nameToSearch == null && nameToSearch.trim().length() == 0)
                return null;

            ISeamJavaComponentDeclaration declaration = null;

            if (file != null) {
                Set<ISeamComponent> cs = seamProject.getComponentsByPath(file.getFullPath());
                for (ISeamComponent c : cs) {
                    ISeamJavaComponentDeclaration d = c.getJavaDeclaration();
                    if (d != null && file.getFullPath().equals(d.getSourcePath())
                            && !((SeamJavaComponentDeclaration) d).getImports().isEmpty()) {
                        declaration = d;
                    }
                }

            }
            Set<ISeamContextVariable> vars = seamProject.getVariables(declaration);
            if (vars != null) {
                for (ISeamContextVariable var : vars) {
                    if (nameToSearch.equals(var.getName())) {
                        while (var instanceof ISeamContextShortVariable) {
                            var = ((ISeamContextShortVariable) var).getOriginal();
                        }
                        if (var == null)
                            continue;

                        if (var instanceof ISeamXmlFactory) {
                            ISeamXmlFactory xmlFactory = (ISeamXmlFactory) var;

                            String value = xmlFactory.getValue();
                            if (value == null || value.trim().length() == 0) {
                                value = xmlFactory.getMethod();
                            }

                            if (value == null || value.trim().length() == 0)
                                continue;

                            List<IJavaElement> javaElements = null;

                            try {
                                javaElements = engine.getJavaElementsForExpression(seamProject, file, value,
                                        region.getOffset());
                            } catch (StringIndexOutOfBoundsException e) {
                                SeamExtPlugin.getDefault().logError(e);
                            } catch (BadLocationException e) {
                                SeamExtPlugin.getDefault().logError(e);
                            }
                            if (javaElements != null) {
                                for (IJavaElement javaElement : javaElements) {
                                    String resourceName = null;
                                    if (javaElement.getResource() != null) {
                                        resourceName = javaElement.getResource().getName();
                                    }
                                    hyperlinks.add(new SeamComponentHyperlink(wordRegion, resourceName,
                                            javaElement, nameToSearch));
                                }
                            }
                        } else if (var instanceof ISeamComponent) {
                            String resourceName = null;
                            ISeamComponent comp = (ISeamComponent) var;
                            Set<ISeamComponentDeclaration> decls = comp.getAllDeclarations();
                            for (ISeamComponentDeclaration decl : decls) {
                                if (decl.getResource() != null) {
                                    resourceName = decl.getResource().getName();
                                    break;
                                }
                            }
                            hyperlinks.add(new SeamComponentHyperlink(wordRegion, resourceName,
                                    (ISeamComponent) var, nameToSearch));
                        } else if (var instanceof IRole) {
                            String resourceName = null;
                            if (var.getResource() != null) {
                                resourceName = var.getResource().getName();
                            }
                            hyperlinks.add(new SeamComponentHyperlink(wordRegion, resourceName, (IRole) var,
                                    nameToSearch));
                        } else if (var instanceof IBijectedAttribute) {
                            String resourceName = null;
                            if (var.getResource() != null) {
                                resourceName = var.getResource().getName();
                            }
                            IBijectedAttribute attr = (IBijectedAttribute) var;
                            if (attr.getSourceMember() != null) {
                                hyperlinks.add(new SeamComponentHyperlink(wordRegion, resourceName,
                                        (IBijectedAttribute) var, nameToSearch));
                            }
                        }
                    }
                }
            }
        }
        if (hyperlinks != null && !hyperlinks.isEmpty()) {
            return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[hyperlinks.size()]);
        }
    } catch (JavaModelException jme) {
        SeamExtPlugin.getDefault().logError(jme);
    }
    return null;
}

From source file:org.jboss.tools.ws.jaxrs.core.jdt.JdtUtils.java

License:Open Source License

/**
 * Resolves the annotation on the given member and its associated AST given its fully qualified name.
 * //from ww w  .j a  v a 2s  . c o m
 * @param member the annotated member to analyze 
 * @param ast the AST associated with the member to analyze
 * @param annotationName the name of the annotation to resolve 
 * @return the resolved {@link Annotation} or {@code null} if it could not be resolved.
 * @throws JavaModelException
 */
public static Annotation resolveAnnotation(final IMember member, final CompilationUnit ast,
        final String annotationName) throws JavaModelException {
    if (member.isBinary()) {
        IAnnotatable javaElement = (IAnnotatable) member;
        final IAnnotation javaAnnotation = javaElement.getAnnotation(annotationName);
        if (javaAnnotation != null && javaAnnotation.exists()) {
            return new Annotation(javaAnnotation, javaAnnotation.getElementName(),
                    resolveAnnotationElements(javaAnnotation));
        }
        return null;
    }
    // when the compilation is being created, the AST may not be available
    if (ast == null) {
        return null;
    }
    final ASTNode memberNode = findDeclaringNode(member, ast);
    final List<?> memberModifiers = getNodeModifiers(memberNode);
    return findAnnotation(memberModifiers, annotationName);
}

From source file:org.nuxeo.ide.sdk.comp.contentassist.contextualproposal.AbstractNodeContextualProposalComputer.java

License:Open Source License

protected IAnnotation extractXmapAnnotation(IAnnotatable annotatableElement) {
    IAnnotation xnodeAnnotation = annotatableElement.getAnnotation(NuxeoXmlComponentProposalComputer.XNODE);
    if (xnodeAnnotation == null || !xnodeAnnotation.exists()) {
        xnodeAnnotation = annotatableElement.getAnnotation(NuxeoXmlComponentProposalComputer.XNODEMAP);
    }//from  w w w.j  a v  a 2 s. c  om
    if (xnodeAnnotation == null || !xnodeAnnotation.exists()) {
        xnodeAnnotation = annotatableElement.getAnnotation(NuxeoXmlComponentProposalComputer.XNODELIST);
    }
    if (xnodeAnnotation == null || !xnodeAnnotation.exists()) {
        return null;
    }
    return xnodeAnnotation;
}

From source file:org.polarsys.reqcycle.jdt.traceability.JDTTraceabilityVisitor.java

License:Open Source License

private void visit(IAnnotatable annot) {
    for (String s : mapOfTypes.keySet()) {
        IAnnotation ann = annot.getAnnotation(s);
        handleAnnot(annot, ann, mapOfTypes.get(s));
    }//from w w w  . j  a  va 2s  .c  om

}