Java Reflection Annotation Find findAnnotationsMap(Class a, Class c)

Here you can find the source of findAnnotationsMap(Class a, Class c)

Description

Same as #findAnnotations(Class,Class) except returns the annotations as a map with the keys being the class on which the annotation was found.

License

Apache License

Parameter

Parameter Description
T The annotation class type.
a The annotation class type.
c The class being searched.

Return

The found matches, or an empty map if annotation was not found.

Declaration

public static <T extends Annotation> LinkedHashMap<Class<?>, T> findAnnotationsMap(Class<T> a, Class<?> c) 

Method Source Code

//package com.java2s;
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *

import java.lang.annotation.*;
import java.util.*;

public class Main {
    /**// w w w .j av  a 2  s .c  om
     * Same as {@link #findAnnotations(Class, Class)} except returns the annotations as a map
     * with the keys being the class on which the annotation was found.
     * <p>
     * Results are ordered child-to-parent.
     *
     * @param <T> The annotation class type.
     * @param a The annotation class type.
     * @param c The class being searched.
     * @return The found matches, or an empty map if annotation was not found.
     */
    public static <T extends Annotation> LinkedHashMap<Class<?>, T> findAnnotationsMap(Class<T> a, Class<?> c) {
        LinkedHashMap<Class<?>, T> m = new LinkedHashMap<Class<?>, T>();
        findAnnotationsMap(a, c, m);
        return m;
    }

    private static <T extends Annotation> void findAnnotationsMap(Class<T> a, Class<?> c, Map<Class<?>, T> m) {
        if (c == null)
            return;

        T t = getDeclaredAnnotation(a, c);
        if (t != null)
            m.put(c, t);

        findAnnotationsMap(a, c.getSuperclass(), m);

        for (Class<?> c2 : c.getInterfaces())
            findAnnotationsMap(a, c2, m);
    }

    /**
     * Returns the specified annotation only if it's been declared on the specified class.
     * <p>
     * More efficient than calling {@link Class#getAnnotation(Class)} since it doesn't
     *    recursively look for the class up the parent chain.
     *
     * @param <T> The annotation class type.
     * @param a The annotation class.
     * @param c The annotated class.
     * @return The annotation, or <jk>null</jk> if not found.
     */
    @SuppressWarnings("unchecked")
    public static <T extends Annotation> T getDeclaredAnnotation(Class<T> a, Class<?> c) {
        for (Annotation a2 : c.getDeclaredAnnotations())
            if (a2.annotationType() == a)
                return (T) a2;
        return null;
    }
}

Related

  1. findAnnotationIn(List modifiers, Class clazz)
  2. findAnnotationInAnnotations(AnnotatedElement annotatedElement, Class annotationClass)
  3. findAnnotationInAnyParameter(Method method, Class annotationClass)
  4. findAnnotationInMethodOrInItsAnnotations(Method method, Class annotationClass)
  5. findAnnotationMethods(String fullClassName, Class anno)
  6. findAnnotationSuperClasses(Class annotationClass, Class c)
  7. findAnnotationWithMetaAnnotation(Class clazz, Class metaAnnotationType)