Java Method Call invokeValue(AnnotatedElement element, Class annotationClass)

Here you can find the source of invokeValue(AnnotatedElement element, Class annotationClass)

Description

Reads the value from the annotation using reflection.

License

Apache License

Parameter

Parameter Description
element The annotated element.
annotationClass The type of the annotation.

Return

The value of the annotation or null if the annotation is not applied.

Declaration

private static <T> Object invokeValue(AnnotatedElement element, Class<? extends Annotation> annotationClass) 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    /**/*from  w  w  w  .j  a v a2s  . c  o  m*/
     * Reads the value from the annotation using reflection.
     * 
     * @param element
     *            The annotated element.
     * @param annotationClass
     *            The type of the annotation.
     * @return The value of the annotation or null if the annotation is not
     *         applied.
     */
    private static <T> Object invokeValue(AnnotatedElement element, Class<? extends Annotation> annotationClass) {
        try {
            final Annotation annotation = element.getAnnotation(annotationClass);
            if (annotation != null) {
                final Method valueMethod = annotationClass.getDeclaredMethod("value");
                return valueMethod.invoke(annotation);
            }
        } catch (final SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (final NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (final IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (final IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (final InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. invokeTargetCheckPoint(String resuming_checkpoint_path, Object jobObject, final java.util.Map globalMap)
  2. invokeTargetMethods(Object obj, T data, List methods)
  3. invokeToStringMethod(Object value, Class type)
  4. invokeUnchecked(Method method, Object target, Object... arguments)
  5. invokeUnwrapException(final Object target, final Method method, @Nullable final Object[] args)
  6. invokeVirtual(String methodName, Object onObj, Class[] declaredArgTypes, Object... withArgs)
  7. invokeVirtual(T o, Method method, Object... pa)
  8. invokeVoid(MethodHandle mh)
  9. invokeVoidNoArgMethod(Class declaringClass, String methodName, Object instance)