Java Reflection Annotation getAnnotationClass(Class clazz, Class annotation)

Here you can find the source of getAnnotationClass(Class clazz, Class annotation)

Description

Loads annotation class, using class loader of the specified class.

License

Open Source License

Parameter

Parameter Description
clazz a class, which classloader will be used
annotation an annotation to load
T annotation parameter

Return

annotation class, loaded using class loader of the given class

Declaration

public static <T extends Annotation> Class<T> getAnnotationClass(Class<?> clazz, Class<T> annotation) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.lang.annotation.Annotation;

public class Main {
    /**//from w ww .j ava2s  .com
     * Loads annotation class, using class loader of the specified class.
     *
     * @param clazz a class, which classloader will be used
     * @param annotation an annotation to load
     * @param <T> annotation parameter
     * @return annotation class, loaded using class loader of the given class
     */
    public static <T extends Annotation> Class<T> getAnnotationClass(Class<?> clazz, Class<T> annotation) {
        ClassLoader clazzClassLoader = clazz.getClassLoader();
        ClassLoader annotationClassLoader = annotation.getClassLoader();

        if (null != clazzClassLoader && !clazzClassLoader.equals(annotationClassLoader)) {
            try {
                return (Class<T>) clazzClassLoader.loadClass(annotation.getName());
            } catch (ClassNotFoundException e) {
                // if the classloader for the given class cannot find the annotation class
                // then the given annotation class will be returned
            }
        }

        return annotation;
    }
}

Related

  1. getAnnotationAttributeValue(final Annotation annotation, final String attributeName)
  2. getAnnotationByType(AnnotatedElement element, Class annotationType)
  3. getAnnotationByType(List annotations, Class annotationType)
  4. getAnnotationClass(Annotation a)
  5. getAnnotationClass(Annotation annotation)
  6. getAnnotationClass(Class entityClass, Class annotationClass)
  7. getAnnotationClass(String name)
  8. getAnnotationDeep(Annotation from, Class toFind)
  9. getAnnotationDefault(Class annotationClass, String element)