get Annotation Parameter - Java java.lang.annotation

Java examples for java.lang.annotation:Annotation Parameter

Description

get Annotation Parameter

Demo Code


//package com.java2s;
import java.lang.annotation.Annotation;

import java.lang.reflect.Method;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T getAnnotationParameter(Annotation annotation,
            String parameterName, Class<T> type) {
        T result = null;//from  w  w w  .j  a v a  2s . c om
        Method m;
        try {
            m = annotation.getClass().getMethod(parameterName);
            m.setAccessible(true);
            Object o = m.invoke(annotation);
            if (o.getClass().getName().equals(type.getName())) {
                return (T) o;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    public static Object getAnnotationParameter(Annotation annotation,
            Method m) {
        try {
            m.setAccessible(true);
            return m.invoke(annotation);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

Related Tutorials