Java Reflection Annotation getAnnotationFromAnnotation(Annotation annotation, Class annotationClass)

Here you can find the source of getAnnotationFromAnnotation(Annotation annotation, Class annotationClass)

Description

Check annotation and its meta reflection to see if it's match annotationClass if match, return the instance of that annotation, otherwise return null

License

Apache License

Parameter

Parameter Description
T a parameter
annotation a parameter
annotationClass a parameter

Declaration

@SuppressWarnings("unchecked")
public static <T extends Annotation> T getAnnotationFromAnnotation(Annotation annotation,
        Class<T> annotationClass) 

Method Source Code

//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;
    }
}

Related

  1. getAnnotationField(Annotation annotation, String field)
  2. getAnnotationFields(Class claz, Class annotationType)
  3. getAnnotationFields(Class clazz, Class annotationClass)
  4. getAnnotationForMethodOrContainingClass(Method m, Class annotationType)
  5. getAnnotationForProperty(Class cls, String name, Class anno)
  6. getAnnotationFromEntityFields(Class entity, Class annotation)
  7. getAnnotationFromEntityOrInterface( Class annotationType, Class entity)
  8. getAnnotationFromField(Object object, Class annotationClass)
  9. getAnnotationFromJoinpointMethod(ProceedingJoinPoint joinpoint, Class annotationClass)