Java Reflection Annotation Find findAnnotation(Annotation parentAnnotation, Class annotationType)

Here you can find the source of findAnnotation(Annotation parentAnnotation, Class annotationType)

Description

Finds annotation of type specified in parameter, inside of other annotation.

License

Open Source License

Parameter

Parameter Description
parentAnnotation parent annotation
annotationType type of annotation that will be searched inside of parent annotation.
T searched annotation type

Return

occurrence of searched annotation or null it no occurrence could be found.

Declaration

@SuppressWarnings("unchecked")
public static <T extends Annotation> T findAnnotation(Annotation parentAnnotation, Class<T> annotationType) 

Method Source Code


//package com.java2s;
/*/*from w w  w. j  av a  2s  . c  o m*/
 * Bristleback Websocket Framework - Copyright (c) 2010-2013 http://bristleback.pl
 * ---------------------------------------------------------------------------
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 3 of the License, or (at your
 * option) any later version.
 * This library is distributed in the hope that it will be useful,
 * but without any warranty; without even the implied warranty of merchantability
 * or fitness for a particular purpose.
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program; if not, see <http://www.gnu.org/licenses/lgpl.html>.
 * ---------------------------------------------------------------------------
 */

import java.lang.annotation.Annotation;

public class Main {
    /**
     * Finds annotation of type specified in parameter, inside of other annotation.
     * Returns null if parent annotation is not marked with such annotation type.
     *
     * @param parentAnnotation parent annotation
     * @param annotationType   type of annotation that will be searched inside of parent annotation.
     * @param <T>              searched annotation type
     * @return occurrence of searched annotation or null it no occurrence could be found.
     */
    @SuppressWarnings("unchecked")
    public static <T extends Annotation> T findAnnotation(Annotation parentAnnotation, Class<T> annotationType) {
        for (Annotation childAnnotation : parentAnnotation.annotationType().getAnnotations()) {
            if (childAnnotation.annotationType().equals(annotationType)) {
                return (T) childAnnotation;
            }
        }
        return null;
    }
}

Related

  1. findAnnotation(AnnotatedElement annotated, Class annotationClass)
  2. findAnnotation(AnnotatedElement annotatedElement, Class annotationClass)
  3. findAnnotation(AnnotatedElement annotatedElement, Class annotationType)
  4. findAnnotation(AnnotatedElement element, Class annotationType)
  5. findAnnotation(Annotation[] parameterAnnotations, Class targetAnnotation)
  6. findAnnotation(Annotation[] searchList, Class annotation)
  7. findAnnotation(Class clazz, Class annotationType)
  8. findAnnotation(Class clazz, Class annotationType)