Here you can find the source of findAnnotationDeclaringClass(Class extends Annotation> annotationType, Class> clazz)
Parameter | Description |
---|---|
annotationType | the annotation type to look for, both locally and as a meta-annotation |
clazz | the class on which to check for the annotation (may be null ) |
public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation> annotationType, Class<?> clazz)
//package com.java2s; /*/*from w ww . j a va2 s. c om*/ * Copyright 2002-2014 the original author or authors. * * 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 { /** * Find the first {@link Class} in the inheritance hierarchy of the specified {@code clazz} * (including the specified {@code clazz} itself) which declares an annotation for the * specified {@code annotationType}, or {@code null} if not found. If the supplied * {@code clazz} is {@code null}, {@code null} will be returned. * <p>If the supplied {@code clazz} is an interface, only the interface itself will be checked; * the inheritance hierarchy for interfaces will not be traversed. * <p>The standard {@link Class} API does not provide a mechanism for determining which class * in an inheritance hierarchy actually declares an {@link Annotation}, so we need to handle * this explicitly. * @param annotationType the annotation type to look for, both locally and as a meta-annotation * @param clazz the class on which to check for the annotation (may be {@code null}) * @return the first {@link Class} in the inheritance hierarchy of the specified {@code clazz} * which declares an annotation for the specified {@code annotationType}, or {@code null} * if not found * @see Class#isAnnotationPresent(Class) * @see Class#getDeclaredAnnotations() * @see #findAnnotationDeclaringClassForTypes(List, Class) * @see #isAnnotationDeclaredLocally(Class, Class) */ public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation> annotationType, Class<?> clazz) { if (clazz == null || clazz.equals(Object.class)) { return null; } if (isAnnotationDeclaredLocally(annotationType, clazz)) { return clazz; } return findAnnotationDeclaringClass(annotationType, clazz.getSuperclass()); } public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> annotationType, Class<?> clazz) { boolean declaredLocally = false; for (Annotation annotation : clazz.getDeclaredAnnotations()) { if (annotation.annotationType().equals(annotationType)) { declaredLocally = true; break; } } return declaredLocally; } }