Here you can find the source of getAnnotationAttribute(final Annotation anno, final String attrName, final Class
@SuppressWarnings("unchecked") public static <T> Optional<T> getAnnotationAttribute(final Annotation anno, final String attrName, final Class<T> attrType)
//package com.java2s; //License from project: Apache License import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.Optional; public class Main { @SuppressWarnings("unchecked") public static <T> Optional<T> getAnnotationAttribute(final Annotation anno, final String attrName, final Class<T> attrType) { try {/*w w w . java 2s . com*/ final Method method = anno.annotationType().getMethod(attrName); method.setAccessible(true); if (!attrType.equals(method.getReturnType())) { return Optional.empty(); } final Object value = method.invoke(anno); return Optional.of((T) value); } catch (Exception e) { return Optional.empty(); } } }