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

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

Introduction

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

Prototype

IAnnotation[] getAnnotations() throws JavaModelException;

Source Link

Document

Returns the annotations for this element.

Usage

From source file:com.aqua.wikiwizard.ObjectsJavaModel.java

License:Apache License

/**
 * Check if annotation is found//from  ww  w .j  ava 2 s  .c o  m
 */
private boolean isAnnotationFound(IAnnotatable type, String annotationName) {
    IAnnotation annotations[];
    try {
        annotations = type.getAnnotations();
        for (IAnnotation annotation : annotations) {
            if (annotation.getElementName().equals(annotationName)) {
                return true;
            }
        }
    } catch (JavaModelException e) {
    }

    return false;
}

From source file:com.codenvy.ide.ext.java.server.internal.compiler.parser.SourceTypeConverter.java

License:Open Source License

private Annotation[] convertAnnotations(IAnnotatable element) throws JavaModelException {
    IAnnotation[] annotations = element.getAnnotations();
    int length = annotations.length;
    Annotation[] astAnnotations = new Annotation[length];
    if (length > 0) {
        char[] cuSource = getSource();
        int recordedAnnotations = 0;
        for (int i = 0; i < length; i++) {
            ISourceRange positions = annotations[i].getSourceRange();
            int start = positions.getOffset();
            int end = start + positions.getLength();
            char[] annotationSource = CharOperation.subarray(cuSource, start, end);
            if (annotationSource != null) {
                Expression expression = parseMemberValue(annotationSource);
                /*//from w w  w.  j  ava  2 s  .co m
                 * expression can be null or not an annotation if the source has changed between
                 * the moment where the annotation source positions have been retrieved and the moment were
                 * this parsing occurred.
                 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=90916
                 */
                if (expression instanceof Annotation) {
                    astAnnotations[recordedAnnotations++] = (Annotation) expression;
                }
            }
        }
        if (length != recordedAnnotations) {
            // resize to remove null annotations
            System.arraycopy(astAnnotations, 0, (astAnnotations = new Annotation[recordedAnnotations]), 0,
                    recordedAnnotations);
        }
    }
    return astAnnotations;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
 * Create an handle for a local variable declaration (may be a local variable or type parameter).
 *//*from  w  ww.j a  v  a2 s  .  com*/
protected IJavaElement createHandle(Annotation annotation, IAnnotatable parent) {
    if (parent == null)
        return null;
    TypeReference typeRef = annotation.type;
    char[][] typeName = typeRef.getTypeName();
    String name = new String(typeName[typeName.length - 1]);
    try {
        IAnnotation[] annotations = parent.getAnnotations();
        int length = annotations == null ? 0 : annotations.length;
        for (int i = 0; i < length; i++) {
            if (annotations[i].getElementName().equals(name)) {
                return annotations[i];
            }
        }
    } catch (JavaModelException jme) {
        // skip
    }
    return null;
}

From source file:com.inepex.classtemplater.plugin.logic.Annotation.java

License:Eclipse Public License

public static Map<String, Annotation> getAnnotationsOf(IAnnotatable annotable, ICompilationUnit compilationUnit)
        throws Exception {
    Map<String, Annotation> annotations = new HashMap<String, Annotation>();
    for (IAnnotation annotation : annotable.getAnnotations()) {
        Annotation a = new Annotation(annotation, compilationUnit);
        annotations.put(a.getName(), a);
    }/*from   w w w .j ava2s .c o m*/
    return annotations;
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockUtil.java

License:Open Source License

private static void outputSymbol(IJavaElement elt, String what, String nm, String key, IvyXmlWriter xw) {
    if (what == null || nm == null)
        return;/*  ww  w  . j  a va 2s . co  m*/

    xw.begin("ITEM");
    xw.field("TYPE", what);
    xw.field("NAME", nm);
    xw.field("HANDLE", elt.getHandleIdentifier());

    xw.field("WORKING", (elt.getPrimaryElement() != elt));
    ICompilationUnit cu = (ICompilationUnit) elt.getAncestor(IJavaElement.COMPILATION_UNIT);
    if (cu != null) {
        xw.field("CUWORKING", cu.isWorkingCopy());
    }
    try {
        xw.field("KNOWN", elt.isStructureKnown());
    } catch (JavaModelException e) {
    }

    if (elt instanceof ISourceReference) {
        try {
            ISourceRange rng = ((ISourceReference) elt).getSourceRange();
            if (rng != null) {
                xw.field("STARTOFFSET", rng.getOffset());
                xw.field("ENDOFFSET", rng.getOffset() + rng.getLength());
                xw.field("LENGTH", rng.getLength());
            }
        } catch (JavaModelException e) {
            BedrockPlugin.logE("Problem getting source range: " + e);
        }
    }

    if (elt instanceof ILocalVariable) {
        ILocalVariable lcl = (ILocalVariable) elt;
        xw.field("SIGNATURE", lcl.getTypeSignature());
    }

    if (elt instanceof IMember) {
        try {
            IMember mem = ((IMember) elt);
            int fgs = mem.getFlags();
            if (mem.getParent() instanceof IType && !(elt instanceof IType)) {
                IType par = (IType) mem.getParent();
                if (par.isInterface()) {
                    if (elt instanceof IMethod)
                        fgs |= Flags.AccAbstract;
                    fgs |= Flags.AccPublic;
                }
                xw.field("QNAME", par.getFullyQualifiedName() + "." + nm);
            }
            xw.field("FLAGS", fgs);
        } catch (JavaModelException e) {
        }
    }

    if (elt instanceof IPackageFragment || elt instanceof IType) {
        Display d = BedrockApplication.getDisplay();
        if (d != null) {
            JavadocUrl ju = new JavadocUrl(elt);
            d.syncExec(ju);
            URL u = ju.getResult();
            if (u != null) {
                xw.field("JAVADOC", u.toString());
            }
        }
    }

    xw.field("SOURCE", "USERSOURCE");
    if (key != null)
        xw.field("KEY", key);

    boolean havepath = false;
    for (IJavaElement pe = elt.getParent(); pe != null; pe = pe.getParent()) {
        if (pe.getElementType() == IJavaElement.COMPILATION_UNIT) {
            IProject ip = elt.getJavaProject().getProject();
            File f = BedrockUtil.getFileForPath(elt.getPath(), ip);
            xw.field("PATH", f.getAbsolutePath());
            havepath = true;
            break;
        }
    }
    IJavaProject ijp = elt.getJavaProject();
    if (ijp != null)
        xw.field("PROJECT", ijp.getProject().getName());
    IPath p = elt.getPath();
    if (p != null) {
        xw.field("XPATH", p);
        if (!havepath) {
            IProject ip = elt.getJavaProject().getProject();
            File f = getFileForPath(elt.getPath(), ip);
            xw.field("PATH", f.getAbsolutePath());
        }
    }

    if (elt instanceof IMethod) {
        IMethod m = (IMethod) elt;
        try {
            xw.field("RESOLVED", m.isResolved());
            ISourceRange rng = m.getNameRange();
            if (rng != null) {
                xw.field("NAMEOFFSET", rng.getOffset());
                xw.field("NAMELENGTH", rng.getLength());
            }
            xw.field("RETURNTYPE", m.getReturnType());
            xw.field("NUMPARAM", m.getNumberOfParameters());
            String[] pnms = m.getParameterNames();
            String[] ptys = m.getParameterTypes();
            for (int i = 0; i < ptys.length; ++i) {
                xw.begin("PARAMETER");
                if (i < pnms.length)
                    xw.field("NAME", pnms[i]);
                xw.field("TYPE", ptys[i]);
                xw.end();
            }
            for (String ex : m.getExceptionTypes()) {
                xw.begin("EXCEPTION");
                xw.field("TYPE", ex);
                xw.end();
            }
        } catch (JavaModelException e) {
        }
    }

    // TODO: output parameters as separate elements with type and name

    if (elt instanceof IAnnotatable) {
        IAnnotatable ann = (IAnnotatable) elt;
        try {
            IAnnotation[] ans = ann.getAnnotations();
            for (IAnnotation an : ans) {
                xw.begin("ANNOTATION");
                xw.field("NAME", an.getElementName());
                xw.field("COUNT", an.getOccurrenceCount());
                try {
                    for (IMemberValuePair mvp : an.getMemberValuePairs()) {
                        xw.begin("VALUE");
                        xw.field("NAME", mvp.getMemberName());
                        if (mvp.getValue() != null)
                            xw.field("VALUE", mvp.getValue().toString());
                        xw.field("KIND", mvp.getValueKind());
                        xw.end("VALUE");
                    }
                } catch (JavaModelException e) {
                }
                xw.end("ANNOTATION");
            }
        } catch (JavaModelException e) {
        }
    }

    xw.end("ITEM");
}

From source file:io.sarl.eclipse.util.Jdt2Ecore.java

License:Apache License

/** Replies the annotation with the given qualified name.
 *
 * @param element - the annoted element.
 * @param qualifiedName - the qualified name of the element.
 * @return the annotation, or <code>null</code> if the element is not annoted.
 *//*from  w w w  .j  a  v a  2  s.  c om*/
public IAnnotation getAnnotation(IAnnotatable element, String qualifiedName) {
    if (element != null) {
        try {
            final int separator = qualifiedName.lastIndexOf('.');
            final String simpleName;
            if (separator >= 0 && separator < (qualifiedName.length() - 1)) {
                simpleName = qualifiedName.substring(separator + 1, qualifiedName.length());
            } else {
                simpleName = qualifiedName;
            }
            for (final IAnnotation annotation : element.getAnnotations()) {
                final String name = annotation.getElementName();
                if (name.equals(simpleName) || name.equals(qualifiedName)) {
                    return annotation;
                }
            }
        } catch (JavaModelException e) {
            //
        }
    }
    return null;
}

From source file:net.harawata.mybatipse.mybatis.JavaMapperUtil.java

License:Open Source License

public static IAnnotation getAnnotationAt(IAnnotatable annotatable, int offset) throws JavaModelException {
    IAnnotation[] annotations = annotatable.getAnnotations();
    for (IAnnotation annotation : annotations) {
        ISourceRange sourceRange = annotation.getSourceRange();
        if (isInRange(sourceRange, offset)) {
            return annotation;
        }/*from   w ww  .  j  a  v  a2 s  .  c o m*/
    }
    return null;
}

From source file:org.eclipse.ajdt.internal.ui.refactoring.PushInRefactoring.java

License:Open Source License

/**
 * Delete the specified annotations on the target
 * @param target target element to have annotations to delete
 * @param removedAnnotationTypes annotations to delete
 * @return the delete edit.  May be a MultiText edit
 * if multiple annotations are removed/*from   www.  j av a 2s . co  m*/
 * @throws JavaModelException 
 */
private TextEdit getAnnotationRemovalEdit(IMember target, String[] removedAnnotationTypes)
        throws JavaModelException {
    if (target instanceof IAnnotatable) {
        IAnnotatable annotatable = (IAnnotatable) target;
        IAnnotation[] anns = annotatable.getAnnotations();
        List<DeleteEdit> deletes = new ArrayList<DeleteEdit>(removedAnnotationTypes.length);
        for (String removedType : removedAnnotationTypes) {
            // there can be only one match per removed type
            // however, there can be two annotations with the
            // same simple name on the member, but at least one has
            // a matching qual name.
            // So, if there is a simple name match, keep on looking, 
            // but a qual name match, end it.
            DeleteEdit edit = null;
            for (IAnnotation ann : anns) {
                // don't know if ann is a simple or a qualified name, 
                // so check both.
                String annName = ann.getElementName();
                boolean qualMatch = removedType.equals(annName);
                boolean simpleMatch = removedType.endsWith("." + annName);
                if (simpleMatch || qualMatch) {
                    // match!
                    ISourceRange range = ann.getSourceRange();
                    edit = new DeleteEdit(range.getOffset(), range.getLength());
                    if (qualMatch) {
                        // can only be one qualified match
                        break;
                    }
                }
            }
            if (edit != null) {
                deletes.add(edit);
            }
        }
        if (deletes.size() == 0) {
            return null;
        } else if (deletes.size() == 1) {
            return deletes.get(0);
        } else {
            MultiTextEdit multi = new MultiTextEdit();
            for (DeleteEdit delete : deletes) {
                multi.addChild(delete);
            }
            return multi;
        }
    } else {
        // nothing found
        return null;
    }
}

From source file:org.eclipse.che.jdt.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
* Create an handle for a local variable declaration (may be a local variable or type parameter).
*//*  w  ww.ja  v a 2  s  .com*/
protected IJavaElement createHandle(Annotation annotation, IAnnotatable parent) {
    if (parent == null)
        return null;
    TypeReference typeRef = annotation.type;
    char[][] typeName = typeRef.getTypeName();
    String name = new String(typeName[typeName.length - 1]);
    try {
        IAnnotation[] annotations = parent.getAnnotations();
        int length = annotations == null ? 0 : annotations.length;
        for (int i = 0; i < length; i++) {
            if (annotations[i].getElementName().equals(name)) {
                return annotations[i];
            }
        }
        if (parent instanceof LocalVariable) {
            LocalVariable localVariable = (LocalVariable) parent;
            IAnnotation[][] annotationsOnDimensions = localVariable.annotationsOnDimensions;
            int noOfDimensions = annotationsOnDimensions == null ? 0 : annotationsOnDimensions.length;
            for (int i = 0; i < noOfDimensions; ++i) {
                IAnnotation[] dimAnnotations = annotationsOnDimensions[i];
                int noOfAnnotations = dimAnnotations.length;
                for (int j = 0; j < noOfAnnotations; ++j) {
                    if (dimAnnotations[j].getElementName().equals(name))
                        return dimAnnotations[j];
                }
            }
        }
    } catch (JavaModelException jme) {
        // skip
    }
    return null;
}

From source file:org.eclipse.pde.api.tools.internal.builder.IllegalAnnotationReferenceDetector.java

License:Open Source License

@Override
protected Position getSourceRange(IType type, IDocument doc, IReference reference)
        throws CoreException, BadLocationException {
    IApiMember member = reference.getMember();
    if (member != null) {
        IAnnotatable annotable = findAnnotableElement(type, member, reference, doc);
        if (annotable != null) {
            IAnnotation[] annots = annotable.getAnnotations();
            String typename = Signatures.getSimpleTypeName(reference.getResolvedReference().getName());
            for (int i = 0; i < annots.length; i++) {
                if (annots[i].getElementName().equals(typename)) {
                    ISourceRange range = annots[i].getNameRange();
                    if (range != null) {
                        // select the '@' as well - augment offset and
                        // length accordingly
                        return new Position(range.getOffset() - 1, range.getLength() + 1);
                    } else {
                        break;
                    }/* w ww . j  a v a2 s  .c o  m*/
                }
            }
        }
    }
    return defaultSourcePosition(type, reference);
}