Example usage for java.lang Class getDeclaredAnnotations

List of usage examples for java.lang Class getDeclaredAnnotations

Introduction

In this page you can find the example usage for java.lang Class getDeclaredAnnotations.

Prototype

public Annotation[] getDeclaredAnnotations() 

Source Link

Usage

From source file:com.mawujun.utils.AnnotationUtils.java

/**
 * Determine whether an annotation for the specified {@code annotationType} is
 * declared locally on the supplied {@code clazz}. The supplied {@link Class}
 * may represent any type./*from  w w  w .j  av  a2 s. c  o  m*/
 * <p>Note: This method does <strong>not</strong> determine if the annotation is
 * {@linkplain java.lang.annotation.Inherited inherited}. For greater clarity
 * regarding inherited annotations, consider using
 * {@link #isAnnotationInherited(Class, Class)} instead.
 * @param annotationType the Class object corresponding to the annotation type
 * @param clazz the Class object corresponding to the class on which to check for the annotation
 * @return {@code true} if an annotation for the specified {@code annotationType}
 * is declared locally on the supplied {@code clazz}
 * @see Class#getDeclaredAnnotations()
 * @see #isAnnotationInherited(Class, Class)
 */
public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> annotationType, Class<?> clazz) {
    Assert.notNull(annotationType, "Annotation type must not be null");
    Assert.notNull(clazz, "Class must not be null");
    boolean declaredLocally = false;
    for (Annotation annotation : clazz.getDeclaredAnnotations()) {
        if (annotation.annotationType().equals(annotationType)) {
            declaredLocally = true;
            break;
        }
    }
    return declaredLocally;
}

From source file:com.springframework.core.annotation.AnnotationUtils.java

/**
 * Perform the search algorithm for {@link #findAnnotation(Class, Class)},
 * avoiding endless recursion by tracking which annotations have already
 * been <em>visited</em>.//from   www . j a va2s .  c o m
 * @param clazz the class to look for annotations on
 * @param annotationType the type of annotation to look for
 * @param visited the set of annotations that have already been visited
 * @return the matching annotation, or {@code null} if not found
 */
@SuppressWarnings("unchecked")
private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType,
        Set<Annotation> visited) {
    Assert.notNull(clazz, "Class must not be null");

    try {
        Annotation[] anns = clazz.getDeclaredAnnotations();
        for (Annotation ann : anns) {
            if (ann.annotationType().equals(annotationType)) {
                return (A) ann;
            }
        }
        for (Annotation ann : anns) {
            if (!isInJavaLangAnnotationPackage(ann) && visited.add(ann)) {
                A annotation = findAnnotation(ann.annotationType(), annotationType, visited);
                if (annotation != null) {
                    return annotation;
                }
            }
        }
    } catch (Exception ex) {
        // Assuming nested Class values not resolvable within annotation attributes...
        logIntrospectionFailure(clazz, ex);
        return null;
    }

    for (Class<?> ifc : clazz.getInterfaces()) {
        A annotation = findAnnotation(ifc, annotationType, visited);
        if (annotation != null) {
            return annotation;
        }
    }

    Class<?> superclass = clazz.getSuperclass();
    if (superclass == null || superclass.equals(Object.class)) {
        return null;
    }
    return findAnnotation(superclass, annotationType, visited);
}

From source file:com.springframework.core.annotation.AnnotationUtils.java

/**
 * Determine whether an annotation of the specified {@code annotationType} is
 * declared locally (i.e., <em>directly present</em>) on the supplied
 * {@code clazz}. The supplied {@link Class} may represent any type.
 * <p>Meta-annotations will <em>not</em> be searched.
 * <p>Note: This method does <strong>not</strong> determine if the annotation is
 * {@linkplain java.lang.annotation.Inherited inherited}. For greater clarity
 * regarding inherited annotations, consider using
 * {@link #isAnnotationInherited(Class, Class)} instead.
 * @param annotationType the Class object corresponding to the annotation type
 * @param clazz the Class object corresponding to the class on which to check for the annotation
 * @return {@code true} if an annotation of the specified {@code annotationType}
 * is <em>directly present</em> on the supplied {@code clazz}
 * @see java.lang.Class#getDeclaredAnnotations()
 * @see java.lang.Class#getDeclaredAnnotation(Class)
 * @see #isAnnotationInherited(Class, Class)
 *//*  ww w.  ja va  2s  . c  o m*/
public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> annotationType, Class<?> clazz) {
    Assert.notNull(annotationType, "Annotation type must not be null");
    Assert.notNull(clazz, "Class must not be null");
    boolean declaredLocally = false;
    try {
        for (Annotation ann : clazz.getDeclaredAnnotations()) {
            if (ann.annotationType().equals(annotationType)) {
                declaredLocally = true;
                break;
            }
        }
    } catch (Exception ex) {
        // Assuming nested Class values not resolvable within annotation attributes...
        logIntrospectionFailure(clazz, ex);
    }
    return declaredLocally;
}

From source file:gov.nih.nci.caarray.validation.UniqueConstraintValidator.java

private boolean declaresConstraint(Class<?> entityClass) {
    Annotation[] annotations = entityClass.getDeclaredAnnotations();
    for (Annotation a : annotations) {
        if (a.equals(uniqueConstraint)) {
            return true;
        } else if (UniqueConstraints.class.equals(a.annotationType())) {
            UniqueConstraints ucs = (UniqueConstraints) a;
            return ArrayUtils.contains(ucs.constraints(), uniqueConstraint);
        }/*  w  ww  . j a  v a  2 s.com*/
    }
    return false;
}

From source file:org.vaadin.spring.stuff.sidebar.SideBarItemDescriptor.java

private Annotation findIconAnnotation() {
    Class<?> type = applicationContext.getType(beanName);
    while (type != null) {
        Annotation[] annotations = type.getDeclaredAnnotations();
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().isAnnotationPresent(SideBarItemIcon.class)) {
                return annotation;
            }//from  w  w  w.j a va  2  s  . c o  m
        }
        type = type.getSuperclass();
    }
    return null;
}

From source file:org.vaadin.spring.sidebar.SideBarItemDescriptor.java

private Annotation findIconAnnotation() {
    Class<?> type = applicationContext.getType(beanName);
    while (type != null) {
        LOGGER.trace("Checking class [{}] for icon annotations", type.getName());
        Annotation[] annotations = type.getDeclaredAnnotations();
        for (Annotation annotation : annotations) {
            LOGGER.trace("Checking annotation [{}] for icon annotations", annotation);
            if (annotation.annotationType().isAnnotationPresent(SideBarItemIcon.class)) {
                LOGGER.trace("Found icon annotation on [{}]", annotation);
                return annotation;
            }//from w  w w .ja  va  2  s  .  com
        }
        type = type.getSuperclass();
    }
    LOGGER.trace("Found no icon annotation");
    return null;
}

From source file:org.unitils.core.reflect.ClassWrapper.java

protected void addAnnotations(Class<?> clazz, List<Annotation> classAnnotations) {
    if (Object.class.equals(clazz)) {
        return;/*from   w w w. j av  a 2 s .  c  om*/
    }
    Annotation[] annotations = clazz.getDeclaredAnnotations();
    classAnnotations.addAll(asList(annotations));
    addAnnotations(clazz.getSuperclass(), classAnnotations);
}

From source file:org.silverpeas.core.test.extention.SilverTestEnv.java

private void manageBean(final Object bean, final Class<?> beanType) {
    Annotation[] qualifiers = Stream.of(beanType.getDeclaredAnnotations())
            .filter(a -> a.annotationType().getAnnotationsByType(Qualifier.class).length > 0)
            .toArray(Annotation[]::new);
    registerInBeanContainer(bean, qualifiers);
}

From source file:org.omnaest.utils.reflection.ReflectionUtils.java

/**
 * Returns all {@link Class#getDeclaredAnnotations()} as {@link List}
 * // w  w  w  .  ja  va2 s  . c o  m
 * @param type
 * @return
 */
public static List<Annotation> declaredAnnotationList(Class<?> type) {
    //    
    List<Annotation> retlist = new ArrayList<Annotation>();

    //
    if (type != null) {
        //
        retlist.addAll(Arrays.asList(type.getDeclaredAnnotations()));
    }

    //
    return retlist;
}

From source file:org.mule.util.scan.ClasspathScanner.java

/**
 * Works out the correct scanner based on the class passed in
 * <p/>//from w w w  .  j ava  2 s  .c o  m
 * Note that these could be better architected by breaking out filters into strategy objects, but for now this
 * suits my needs
 *
 * @param clazz the type to scan for
 * @return a scanner suitable for handling the type passed in
 * @see AnnotationsScanner
 * @see InterfaceClassScanner
 * @see ImplementationClassScanner
 */
protected ClassScanner getScanner(Class<?> clazz) {
    if (clazz.isInterface()) {
        if (clazz.isAnnotation()) {
            @SuppressWarnings("unchecked")
            Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) clazz;

            AnnotationFilter filter = null;
            Annotation[] annos = clazz.getDeclaredAnnotations();
            for (int i = 0; i < annos.length; i++) {
                Annotation anno = annos[i];
                if (anno instanceof Target) {
                    if (((Target) anno).value()[0] == ElementType.ANNOTATION_TYPE) {
                        filter = new MetaAnnotationTypeFilter(annotationClass, classLoader);
                    }
                }
            }
            if (filter == null) {
                filter = new AnnotationTypeFilter(annotationClass);
            }
            return new AnnotationsScanner(filter);
        } else {
            return new InterfaceClassScanner(clazz);
        }
    } else {
        return new ImplementationClassScanner(clazz);
    }
}