Here you can find the source of getAnnotation(Class
Parameter | Description |
---|---|
T | The annotation class type. |
a | The annotation class. |
c | The annotated class. |
public static <T extends Annotation> T getAnnotation(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.*; public class Main { /**/*from ww w.j a va 2s .com*/ * Similar to {@link Class#getAnnotation(Class)} except also searches annotations on interfaces. * * @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. */ public static <T extends Annotation> T getAnnotation(Class<T> a, Class<?> c) { if (c == null) return null; T t = getDeclaredAnnotation(a, c); if (t != null) return t; t = getAnnotation(a, c.getSuperclass()); if (t != null) return t; for (Class<?> c2 : c.getInterfaces()) { t = getAnnotation(a, c2); if (t != null) return t; } return null; } /** * 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; } }