Java Reflection Annotation getAnnotation(Class annotationClass, Annotation[] annotations)

Here you can find the source of getAnnotation(Class annotationClass, Annotation[] annotations)

Description

Searches for annotation instance of the type specified by annotationClass in the array of annotations.

License

Apache License

Parameter

Parameter Description
annotationClass type of the annotation to be searched for.
annotations array of annotation instances where the search is performed

Return

found annotation instance or null if the search fails

Declaration

public static Annotation getAnnotation(Class<?> annotationClass, Annotation[] annotations) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Charles University in Prague
 * /* w w w .  j a  va  2  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.
 ******************************************************************************/

import java.lang.annotation.Annotation;

public class Main {
    /**
     * Searches for annotation instance of the type specified by
     * <code>annotationClass</code> in the array of annotations.
     * 
     * @param annotationClass
     *            type of the annotation to be searched for.
     * @param annotations
     *            array of annotation instances where the search is performed
     * @return found annotation instance or null if the search fails
     */
    public static Annotation getAnnotation(Class<?> annotationClass, Annotation[] annotations) {
        for (Annotation a : annotations) {
            if (annotationClass.isInstance(a)) {
                return a;
            }
        }
        return null;
    }
}

Related

  1. getAnnotation(Class clazz, Class annotation)
  2. getAnnotation(Class cls, Class annotationCls)
  3. getAnnotation(Class cls, Class annotationCls)
  4. getAnnotation(Class theExaminedClass, Class theExpectedAnnotation)
  5. getAnnotation(Class anno, Class cl)
  6. getAnnotation(Class base, Class annotationClass)
  7. getAnnotation(Class c, Class annotationClass)
  8. getAnnotation(Class classForAnnotation, Class annotationClass)
  9. getAnnotation(Class clazz, Class annotationClass)