Here you can find the source of getAnnotationValue(Class extends Annotation> anno, Field field, String paramName)
public static Object getAnnotationValue(Class<? extends Annotation> anno, Field field, String paramName) throws Exception
//package com.java2s; /*/*from w w w . j a v a2s . co m*/ * Copyright 2002-2006,2009 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; public class Main { public static Object getAnnotationValue(Class<? extends Annotation> anno, Field field, String paramName) throws Exception { Object value = null; if (hasAnnotation(anno, field) && (paramName != null && !paramName.isEmpty())) { value = callMethodNamed(paramName, field.getAnnotation(anno)); } return value; } public static Object getAnnotationValue(Class<? extends Annotation> anno, Method meth, String paramName) throws Exception { Object value = null; if (hasAnnotation(anno, meth) && (paramName != null && !paramName.isEmpty())) { value = callMethodNamed(paramName, meth.getAnnotation(anno)); } return value; } public static Object getAnnotationValue(Class<? extends Annotation> anno, Object obj, String paramName) throws Exception { return getAnnotationValue(anno, obj.getClass(), paramName); } public static Object getAnnotationValue(Class<? extends Annotation> anno, Class<?> cl, String paramName) throws Exception { Object value = null; if (hasAnnotation(anno, cl) && (paramName != null && !paramName.isEmpty())) { value = callMethodNamed(paramName, cl.getAnnotation(anno)); } return value; } public static Boolean hasAnnotation(Class<? extends Annotation> anno, Field field) { return field.isAnnotationPresent(anno); } public static Boolean hasAnnotation(Class<? extends Annotation> anno, Method meth) { return meth.isAnnotationPresent(anno); } public static Boolean hasAnnotation(Class<? extends Annotation> anno, Object obj) { return (getAnnotation(anno, obj) != null); } public static Boolean hasAnnotation(Class<? extends Annotation> anno, Class<?> cl) { return (getAnnotation(anno, cl) != null); } public static Object callMethodNamed(String name, Object o, Object... p) throws Exception { Method toCall = getMethodNamed(name, o); if (toCall == null) throw new Exception("Il n'y a aucune methode " + name); return call(toCall, o, p); } public static Annotation getAnnotation(Class<? extends Annotation> anno, Class<?> cl) { return cl.getAnnotation(anno); } public static Annotation getAnnotation(Class<? extends Annotation> anno, Object obj) { return obj.getClass().getAnnotation(anno); } public static Annotation getAnnotation(Class<? extends Annotation> anno, Method meth) { return meth.getAnnotation(anno); } public static Annotation getAnnotation(Class<? extends Annotation> anno, Field field) { return field.getAnnotation(anno); } public static Method getMethodNamed(String methodName, Object holder) { Method method = null; List<Method> all = Arrays.asList(holder.getClass().getDeclaredMethods()); for (Iterator<Method> iterator = all.iterator(); iterator.hasNext();) { Method m = (Method) iterator.next(); if (methodName.equalsIgnoreCase(m.getName())) { method = m; break; } } return method; } public static Object call(Method m, Object o, Object... p) throws Exception { Object value = null; try { m.setAccessible(true); value = m.invoke(o, p); m.setAccessible(false); } catch (IllegalArgumentException e) { e.printStackTrace(); throw new Exception("Les arguments transmis sont mauvais."); } catch (IllegalAccessException e) { e.printStackTrace(); throw new Exception("Impossible d'acceder a la methode."); } catch (InvocationTargetException e) { e.printStackTrace(); throw new Exception("Erreur dans l'execution."); } return value; } public static List<Object> call(final List<Method> lm, final Object o, final Object... p) throws Exception { List<Object> values = new ArrayList<Object>(); for (Iterator<Method> iterator = lm.iterator(); iterator.hasNext();) { Method m = (Method) iterator.next(); values.add(call(m, o, p)); } return values; } }