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

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

Description

Finds an annotation of given annotationType from the given annotations .

License

Apache License

Parameter

Parameter Description
annotations The annotations to search in
annotationType The type of annotation to search for
T The type of annotation to search for

Return

the first annotation found, or null if no such annotation is present

Declaration

@SuppressWarnings({ "unchecked" })
@Deprecated
public static <T> T getAnnotation(Annotation[] annotations, Class<T> annotationType) 

Method Source Code


//package com.java2s;
/*// w w  w .  j  a  v  a 2s  . c o m
 * Copyright (c) 2010-2016. Axon Framework
 *
 * 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 {
    /**
     * Finds an annotation of given {@code annotationType} from the given {@code annotations}. If
     * {@code annotations} contains multiple annotations of the given type, the first one is returned. If none
     * is found, this method returns {@code null}.
     *
     * @param annotations    The annotations to search in
     * @param annotationType The type of annotation to search for
     * @param <T>            The type of annotation to search for
     * @return the first annotation found, or {@code null} if no such annotation is present
     */
    @SuppressWarnings({ "unchecked" })
    @Deprecated
    public static <T> T getAnnotation(Annotation[] annotations, Class<T> annotationType) {
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().equals(annotationType)) {
                return (T) annotation;
            }
        }
        return null;
    }
}

Related

  1. getAnnotation(AnnotatedElement target, String annotationType)
  2. getAnnotation(Annotation ann, Class annotationType)
  3. getAnnotation(Annotation[] annotaions, Class T)
  4. getAnnotation(Annotation[] annotations, Class annotationType)
  5. getAnnotation(Annotation[] annotations, Class annotationClazz)
  6. getAnnotation(Annotation[] annotations, Class clazz)
  7. getAnnotation(Class clazz, Class annotation)
  8. getAnnotation(Class cls, Class annotationCls)
  9. getAnnotation(Class cls, Class annotationCls)