Java Reflection Annotation getAnnotations(final Class clazz, final Class annotation)

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

Description

get Annotations

License

Open Source License

Declaration

public static <T extends Annotation> Set<T> getAnnotations(final Class<?> clazz, final Class<T> annotation) 

Method Source Code


//package com.java2s;
/*/*from  w  ww.j a  v  a2s. c  o m*/
 * #%L
 * GraphWalker Java
 * %%
 * Copyright (C) 2005 - 2014 GraphWalker
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
 */

import java.lang.annotation.Annotation;

import java.util.HashSet;
import java.util.Set;

public class Main {
    public static <T extends Annotation> Set<T> getAnnotations(final Class<?> clazz, final Class<T> annotation) {
        Set<T> annotations = new HashSet<>();
        Class<?> queryClass = clazz;
        while (null != queryClass) {
            addAnnotation(queryClass, annotations, annotation);
            for (Class<?> interfaceClass : queryClass.getInterfaces()) {
                addAnnotation(interfaceClass, annotations, annotation);
            }
            queryClass = queryClass.getSuperclass();
        }
        return annotations;
    }

    private static <T extends Annotation> void addAnnotation(final Class<?> clazz, final Set<T> annotations,
            final Class<T> annotation) {
        if (clazz.isAnnotationPresent(annotation)) {
            annotations.add(clazz.getAnnotation(annotation));
        }
    }

    private static boolean isAnnotationPresent(Class<?> type, Class<? extends Annotation> annotation) {
        return isAnnotationPresent(type, annotation, Thread.currentThread().getContextClassLoader());
    }

    @SuppressWarnings("unchecked")
    private static boolean isAnnotationPresent(Class<?> type, Class<? extends Annotation> annotation,
            ClassLoader classLoader) {
        try {
            Class<? extends Annotation> a = (Class<? extends Annotation>) classLoader
                    .loadClass(annotation.getName());
            return type.isAnnotationPresent(a);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException();
        }
    }
}

Related

  1. getAnnotations(Class annotation, Collection annotations)
  2. getAnnotations(Class ann, Object o, Method m, int param)
  3. getAnnotations(Enum targetEnum, Class targetAnnotationClass)
  4. getAnnotations(Field field)
  5. getAnnotations(final Class c, final Class annClass)
  6. getAnnotations(final Class clazz, final String fieldName)
  7. getAnnotations(Method method)
  8. getAnnotations(Method method)
  9. getAnnotations(Method method)