Here you can find the source of findAnnotationsMap(Class
Parameter | Description |
---|---|
T | The annotation class type. |
a | The annotation class type. |
c | The class being searched. |
public static <T extends Annotation> LinkedHashMap<Class<?>, T> findAnnotationsMap(Class<T> a, Class<?> c)
//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; } }