Here you can find the source of getAnnotation(Class> componentClass, Class
Parameter | Description |
---|---|
T | the generic type |
componentClass | the component class |
annotationClass | the annotation class |
public static <T extends Annotation> T getAnnotation(Class<?> componentClass, Class<T> annotationClass)
//package com.java2s; /**//from ww w .j a va 2 s.c o m * Copyright (C) 2007 Asterios Raptis * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.lang.annotation.Annotation; public class Main { /** * Search for the given annotationClass in the given componentClass and return it if search was * successful. * * @param <T> * the generic type * @param componentClass * the component class * @param annotationClass * the annotation class * @return the annotation */ public static <T extends Annotation> T getAnnotation(Class<?> componentClass, Class<T> annotationClass) { T annotation = componentClass.getAnnotation(annotationClass); if (annotation != null) { return annotation; } for (Class<?> ifc : componentClass.getInterfaces()) { annotation = getAnnotation(ifc, annotationClass); if (annotation != null) { return annotation; } } if (!Annotation.class.isAssignableFrom(componentClass)) { for (Annotation ann : componentClass.getAnnotations()) { annotation = getAnnotation(ann.annotationType(), annotationClass); if (annotation != null) { return annotation; } } } Class<?> superClass = componentClass.getSuperclass(); if (superClass == null || superClass.equals(Object.class)) { return null; } return getAnnotation(superClass, annotationClass); } }