Example usage for org.eclipse.jdt.core.dom ITypeBinding getAnnotations

List of usage examples for org.eclipse.jdt.core.dom ITypeBinding getAnnotations

Introduction

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

Prototype

public IAnnotationBinding[] getAnnotations();

Source Link

Document

Returns the resolved declaration annotations associated with this binding.

Usage

From source file:com.google.devtools.j2objc.util.BindingUtil.java

License:Apache License

/**
 * Returns true if the specified binding is of an annotation that has
 * a runtime retention policy.// ww  w .java2  s .c  o m
 */
public static boolean isRuntimeAnnotation(ITypeBinding binding) {
    if (binding != null && binding.isAnnotation()) {
        for (IAnnotationBinding ann : binding.getAnnotations()) {
            if (ann.getName().equals("Retention")) {
                IVariableBinding retentionBinding = (IVariableBinding) ann.getDeclaredMemberValuePairs()[0]
                        .getValue();
                return retentionBinding.getName().equals(RetentionPolicy.RUNTIME.name());
            }
        }
        if (binding.isNested()) {
            return BindingUtil.isRuntimeAnnotation(binding.getDeclaringClass());
        }
    }
    return false;
}

From source file:com.halware.nakedide.eclipse.core.util.TypeUtils.java

License:Open Source License

@SuppressWarnings("restriction")
static boolean hasAnnotation(ITypeBinding typeBinding, String annotationTypeName) {

    if (typeBinding == null) {
        return false;
    }/*  ww  w .j  av  a2 s.c  o  m*/

    String annotationTypeSig = ("L" + annotationTypeName + ";");

    IAnnotationBinding[] annotations = typeBinding.getAnnotations();
    for (IAnnotationBinding annotationBinding : annotations) {
        IJavaElement javaElement = annotationBinding.getJavaElement();
        if (javaElement == null) {
            continue;
        }
        if (!(javaElement instanceof org.eclipse.jdt.internal.core.ResolvedBinaryType)) {
            continue;
        }
        org.eclipse.jdt.internal.core.ResolvedBinaryType rbt = (org.eclipse.jdt.internal.core.ResolvedBinaryType) javaElement;
        BindingKey bindingKey = new BindingKey(rbt.getKey());
        if (annotationTypeSig.equals(bindingKey.toSignature())) {
            return true;
        }
    }
    return false;
}

From source file:edu.cmu.cs.crystal.annotations.AnnotationDatabase.java

License:Open Source License

/**
 * This method will return the list of annotations associated with the
 * given type. To work, this binding had to be parsed by Crystal. If you do not have
 * a type that was parsed by Crystal, use {@link #getAnnosForType(IType)}.
 *//* w  w  w. j  a va  2s.co m*/
public List<ICrystalAnnotation> getAnnosForType(ITypeBinding type) {
    while (type != type.getTypeDeclaration())
        type = type.getTypeDeclaration();
    if (type.isPrimitive())
        return Collections.emptyList();
    if (type.isArray()) {
        log.warning("Annotations for array type requested: " + type.getName());
    }

    String name = type.getKey();

    List<ICrystalAnnotation> result = classes.get(name);
    if (result == null) {
        result = createAnnotations(type.getAnnotations());
        classes.put(name, result);
    }
    return result;
}

From source file:edu.cmu.cs.crystal.annotations.AnnotationDatabase.java

License:Open Source License

/**
 * Checks whether this annotation is marked as a multi annotation, as described by
 * MultiAnnotation/*from w ww  .j  a  v  a2  s  .c  o m*/
 * 
 * @param annoBinding
 * @return true id this is a multi annotation, and false if it is not.
 */
public boolean isMulti(IAnnotationBinding annoBinding) {
    ITypeBinding binding = annoBinding.getAnnotationType();

    for (IAnnotationBinding meta : binding.getAnnotations()) {
        if (meta.getAnnotationType().getQualifiedName().equals(MultiAnnotation.class.getName()))
            return true;
    }
    return false;
}

From source file:edu.cmu.cs.crystal.annotations.AnnotationDatabase.java

License:Open Source License

public ICrystalAnnotation createCrystalAnnotation(ITypeBinding typeBinding) {
    Class<? extends ICrystalAnnotation> annoClass = qualNames.get(typeBinding.getQualifiedName());

    if (annoClass == null) {
        IAnnotationBinding[] metas = typeBinding.getAnnotations();
        // might still be a meta annotation. Check for this.
        for (int ndx = 0; ndx < metas.length && annoClass == null; ndx++) {
            IAnnotationBinding meta = metas[ndx];
            String metaName = meta.getAnnotationType().getQualifiedName();
            annoClass = metaQualNames.get(metaName);
        }/*from ww w .  ja  va 2 s  . c o  m*/
    }

    if (annoClass == null)
        return new CrystalAnnotation();

    try {
        // this annotation is registered directly
        return annoClass.newInstance();
    } catch (InstantiationException e) {
        log.log(Level.WARNING, "Error instantiating custom annotation parser.  Using default representation.",
                e);
        return new CrystalAnnotation();
    } catch (IllegalAccessException e) {
        log.log(Level.WARNING, "Error accessing custom annotation parser.  Using default representation.", e);
        return new CrystalAnnotation();
    }
}

From source file:jmockit.assist.MockUtil.java

License:Open Source License

public static boolean hasMockClass(final ITypeBinding type) {
    return ASTUtil.findAnnotation(type.getAnnotations(), MOCK_CLASS) != null;
}

From source file:jmockit.assist.MockUtil.java

License:Open Source License

public static ITypeBinding findRealClassType(final ITypeBinding mockClass) {
    IAnnotationBinding ann = ASTUtil.findAnnotation(mockClass.getAnnotations(), MOCK_CLASS);

    for (IMemberValuePairBinding pair : ann.getDeclaredMemberValuePairs()) {
        if ("realClass".equals(pair.getName())) {
            if (pair.getValue() instanceof ITypeBinding) {
                return (ITypeBinding) pair.getValue();
            }//from  ww  w  .  j av  a  2 s .c  o  m
        }
    }

    return null;
}

From source file:org.bundlemaker.core.ui.editor.sourceviewer.referencedetail.JdtAstVisitor.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*w w  w  . jav  a  2  s .  c o m*/
public boolean visit(TypeDeclaration node) {

    // declared type superclass type
    Type superclassType = node.getSuperclassType();
    if (superclassType != null) {
        resolveType(superclassType);
    }
    // declared type implemented interfaces types
    List<Type> interfaces = node.superInterfaceTypes();
    for (Type iface : interfaces) {
        resolveType(iface);
    }

    //
    ITypeBinding typeBinding = node.resolveBinding();
    if (typeBinding != null) {
        IAnnotationBinding[] annotationBindings = typeBinding.getAnnotations();
        for (IAnnotationBinding annotationBinding : annotationBindings) {
            resolveTypeBinding(annotationBinding.getAnnotationType(), node.getStartPosition(),
                    node.getLength());
        }
    }
    // visit the child nodes
    return true;
}

From source file:org.eclipse.che.plugin.java.testing.JavaTestAnnotations.java

License:Open Source License

/**
 * Check if type annotated as test type.
 *
 * @param type type which should be checked
 * @return {@code true} if type annotated as test otherwise returns {@code false}
 *//*from   ww w  . ja  v  a2s .com*/
public boolean annotatesTypeOrSuperTypes(ITypeBinding type) {
    while (type != null) {
        if (annotates(type.getAnnotations())) {
            return true;
        }
        type = type.getSuperclass();
    }
    return false;
}

From source file:org.eclipse.recommenders.codesearch.rcp.index.indexer.AnnotationsIndexer.java

License:Open Source License

@Override
public void indexType(final Document document, final TypeDeclaration type) {
    final ITypeBinding clazz = type.resolveBinding();
    if (clazz == null) {
        return;/*from w ww  . jav a2 s  . com*/
    }

    addAnnotations(document, clazz.getAnnotations());
}