Java Reflection Annotation getAnnotationAttribute(final Annotation anno, final String attrName, final Class attrType)

Here you can find the source of getAnnotationAttribute(final Annotation anno, final String attrName, final Class attrType)

Description

get Annotation Attribute

License

Apache License

Declaration

@SuppressWarnings("unchecked")
public static <T> Optional<T> getAnnotationAttribute(final Annotation anno, final String attrName,
        final Class<T> attrType) 

Method Source Code


//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();
        }

    }
}

Related

  1. getAnnotation(Object obj, Class annClass)
  2. getAnnotation(Object obj, Class acl)
  3. getAnnotation(Object obj, Class annotation)
  4. getAnnotation(ProceedingJoinPoint pjp, Class annotationClass)
  5. getAnnotationAttribute(Annotation a, String attrName)
  6. getAnnotationAttributes(Annotation annotation)
  7. getAnnotationAttributes(Annotation annotation)
  8. getAnnotationAttributes(final Annotation annotation)
  9. getAnnotationAttributeValue(final Annotation annotation, final String attributeName)