Java Reflection Annotation getAnnotation(Annotation ann, Class annotationType)

Here you can find the source of getAnnotation(Annotation ann, Class annotationType)

Description

get Annotation

License

Apache License

Declaration

public static <T extends Annotation> T getAnnotation(Annotation ann, Class<T> annotationType) 

Method Source Code


//package com.java2s;
/*//w  ww . j  a  v  a  2 s. c  o  m
 * 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;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;

public class Main {

    public static <T extends Annotation> T getAnnotation(Annotation ann, Class<T> annotationType) {
        if (annotationType.isInstance(ann)) {
            return (T) ann;
        }
        return ann.annotationType().getAnnotation(annotationType);
    }

    public static <T extends Annotation> T getAnnotation(AnnotatedElement ae, Class<T> annotationType) {
        T ann = ae.getAnnotation(annotationType);
        if (ann == null) {
            for (Annotation metaAnn : ae.getAnnotations()) {
                ann = metaAnn.annotationType().getAnnotation(annotationType);
                if (ann != null) {
                    break;
                }
            }
        }
        return ann;
    }

    /**
     * Get a single {@link Annotation} of {@code annotationType} from the supplied {@link Method}.
     * <p>Correctly handles bridge {@link Method Methods} generated by the compiler.
     * @param method the method to look for annotations on
     * @param annotationType the annotation type to look for
     * @return the annotations found
     */
    public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) {
        //Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);
        //return getAnnotation((AnnotatedElement) resolvedMethod, annotationType);
        return getAnnotation((AnnotatedElement) method, annotationType);
    }
}

Related

  1. getAnnotation(AnnotatedElement ae, Class annotationType)
  2. getAnnotation(AnnotatedElement aobj, Class aClass)
  3. getAnnotation(AnnotatedElement element, Class annotation)
  4. getAnnotation(AnnotatedElement element, String annotationTypeName)
  5. getAnnotation(AnnotatedElement target, String annotationType)
  6. getAnnotation(Annotation[] annotaions, Class T)
  7. getAnnotation(Annotation[] annotations, Class annotationType)
  8. getAnnotation(Annotation[] annotations, Class annotationClazz)
  9. getAnnotation(Annotation[] annotations, Class annotationType)