Java Reflection Annotation Find findAnnotationIn(List modifiers, Class clazz)

Here you can find the source of findAnnotationIn(List modifiers, Class clazz)

Description

Finds an annotation from a list that is an instance of the provided class.

License

Open Source License

Parameter

Parameter Description
A the type of the annotation to find
modifiers the list to search in
clazz the class to search for (subclasses included)

Return

an optional containing the first annotation that is an instance of given class, or an empty optional if there is no such annotation.

Declaration

@SuppressWarnings("unchecked")
public static <A extends Annotation> Optional<A> findAnnotationIn(List<? extends Annotation> modifiers,
        Class<? extends A> clazz) 

Method Source Code


//package com.java2s;
/*// www. j  a v a 2 s  . c o m
 * Intake-Spigot, a Spigot bridge for the Intake command framework.
 * Copyright (C) Philipp Nowak (Literallie)
 *
 * 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 program 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. See the GNU Lesser General Public License
 * for more details.
 *
 * 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/>.
 */

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Optional;

public class Main {
    /**
     * Finds an annotation from a list that is an instance of the provided class.
     *
     * @param <A>       the type of the annotation to find
     * @param modifiers the list to search in
     * @param clazz     the class to search for (subclasses included)
     * @return an optional containing the first annotation that is an instance of given class, or an empty optional if
     * there is no such annotation.
     */
    @SuppressWarnings("unchecked")
    public static <A extends Annotation> Optional<A> findAnnotationIn(List<? extends Annotation> modifiers,
            Class<? extends A> clazz) {
        return modifiers.stream().filter(mod -> clazz.isAssignableFrom(mod.getClass())).map(mod -> (A) mod)
                .findFirst();
    }
}

Related

  1. findAnnotationDeclaringClass(Class annotationType, Class classToFind)
  2. findAnnotationDeclaringClass(Class annotationType, Class clazz)
  3. findAnnotationFromClass(Class target, Class annotation)
  4. findAnnotationFromMethodOrClass(final Method method, final Class annotationClass)
  5. findAnnotationHelper( Class base, Type iface)
  6. findAnnotationInAnnotations(AnnotatedElement annotatedElement, Class annotationClass)
  7. findAnnotationInAnyParameter(Method method, Class annotationClass)
  8. findAnnotationInMethodOrInItsAnnotations(Method method, Class annotationClass)
  9. findAnnotationMethods(String fullClassName, Class anno)