Java Reflection Annotation getAnnotation(Class clazz, Class annotation)

Here you can find the source of getAnnotation(Class clazz, Class annotation)

Description

Returns the specified T annotation for the potentially annotated Class class if it exists.

License

Open Source License

Parameter

Parameter Description
clazz The class to test contains the specified annotation .
annotation The annotation to check the specified class for.

Return

The annotation if and only if it exists otherwise .

Declaration

public static <T extends Annotation> Optional<T> getAnnotation(Class<?> clazz, Class<T> annotation) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**//from w ww  .j  a v  a  2 s.c  om
     * Returns the specified {@link T annotation} for the potentially annotated {@link Class class} if
     * it exists.
     * 
     * @param clazz The class to test contains the specified {@code annotation}.
     * @param annotation The annotation to check the specified {@code class} for.
     * @return The {@code annotation} if and only if it exists otherwise {@link Optional#empty()}.
     */
    public static <T extends Annotation> Optional<T> getAnnotation(Class<?> clazz, Class<T> annotation) {
        return Optional.ofNullable(clazz.getAnnotation(annotation));
    }
}

Related

  1. getAnnotation(Class clazz, Class annotClass, boolean forceInherit)
  2. getAnnotation(Class clazz, Class annotationClass)
  3. getAnnotation(Class clazz, Class ann)
  4. getAnnotation(Class clazz, Class annClazz)
  5. getAnnotation(Class clazz, Class annotation)
  6. getAnnotation(Class clazz, Class annotationClass)
  7. getAnnotation(Class clazz, Class annotationClass)
  8. getAnnotation(Class clazz, Class annotationClass)
  9. getAnnotation(Class clazz, Class annotationType)