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

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

Description

Method searchs annotation in annotations array and return it.

License

Apache License

Parameter

Parameter Description
annotations a parameter

Declaration

@SuppressWarnings("unchecked")
public static final <T extends Annotation> T getAnnotation(Class<T> lookingFor, Annotation[] annotations) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * Copyright 2011 Zdenko Vrabel/*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.
 * 
 *****************************************************************************/

import java.lang.annotation.Annotation;

public class Main {
    /**
     * Method searchs annotation in annotations array and return
     * it. If annotation is not occured, then method return null.
     *
     * @param annotations
     * @return 
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public static final <T extends Annotation> T getAnnotation(Class<T> lookingFor, Annotation[] annotations) {
        if (annotations != null) {
            for (int i = 0; i < annotations.length; ++i) {
                if (lookingFor.isInstance(annotations[i])) {
                    return (T) annotations[i];
                }
            }
        }
        return null;
    }
}

Related

  1. getAnnotation(Class annotationType, AnnotatedElement... objects)
  2. getAnnotation(Class cl, Object o)
  3. getAnnotation(Class clazz, Method method, int parameterIndex)
  4. getAnnotation(Class cls, Annotation... annotations)
  5. getAnnotation(Class cls, Method... methods)
  6. getAnnotation(Enum enumConstant, Class annotationClass)
  7. getAnnotation(final AnnotatedElement annotatedElement, final Class annotationClass)
  8. getAnnotation(final Class c, final Class annClass)
  9. getAnnotation(final Class annotatedClass, final Class annotationClass)