Here you can find the source of getAnnotationFromAnnotation(Annotation annotation, Class
Parameter | Description |
---|---|
T | a parameter |
annotation | a parameter |
annotationClass | a parameter |
@SuppressWarnings("unchecked") public static <T extends Annotation> T getAnnotationFromAnnotation(Annotation annotation, Class<T> annotationClass)
//package com.java2s; /******************************************************************************* * Copyright 2001, 2007 JamesLuo(JamesLuo.au@gmail.com) * //from w ww .j a v a2 s .c o m * 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. * * Contributors: *******************************************************************************/ import java.lang.annotation.Annotation; public class Main { /** * Check annotation and its meta reflection to see if it's match annotationClass * if match, return the instance of that annotation, otherwise return null * @param <T> * @param annotation * @param annotationClass * @return */ @SuppressWarnings("unchecked") public static <T extends Annotation> T getAnnotationFromAnnotation(Annotation annotation, Class<T> annotationClass) { if (annotation.annotationType() == annotationClass) { return (T) annotation; } else if (annotation.annotationType().getName().startsWith("java.lang.annotation")) { return null; //Document's parent is itself? must check here } else { Class<? extends Annotation> annotationType = annotation.annotationType(); Annotation[] metaAnnotations = annotationType.getAnnotations(); for (Annotation metaAnnotation : metaAnnotations) { T result = getAnnotationFromAnnotation(metaAnnotation, annotationClass); if (result != null) { return result; } } } return null; } }