Java Reflection Method Get from Object getMethodsAnnotatedWithValue(Class anno, Object o, String name, Object value)

Here you can find the source of getMethodsAnnotatedWithValue(Class anno, Object o, String name, Object value)

Description

get Methods Annotated With Value

License

Apache License

Declaration

public static List<Method> getMethodsAnnotatedWithValue(Class<? extends Annotation> anno, Object o, String name,
            Object value) throws Exception 

Method Source Code


//package com.java2s;
/*// ww  w  .  j  a v a  2s .c  om
 * 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 List<Method> getMethodsAnnotatedWithValue(Class<? extends Annotation> anno, Object o, String name,
            Object value) throws Exception {
        ArrayList<Method> methods = new ArrayList<Method>();
        List<Method> all = Arrays.asList(o.getClass().getDeclaredMethods());
        for (Iterator<Method> iterator = all.iterator(); iterator.hasNext();) {
            Method method = (Method) iterator.next();
            if (value != null && value.equals(getAnnotationValue(anno, method, name)))
                methods.add(method);
        }
        return methods;
    }

    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;
    }
}

Related

  1. getMethods(Object instance, String name, Class[] arguments, boolean isPrefix)
  2. getMethods(Object obj, boolean hasParent)
  3. getMethods(Object obj, int cmpModifier, String prefix)
  4. getMethodsAnnotatedWith(Class ann, Object o)
  5. getMethodsAnnotatedWith(Object target, Class annotation)
  6. getMethodsByStartsWithName(String name, Object o)
  7. getMethodsForObject(Object o2, String[] passedMethods)
  8. getMethodsIncludingHierarchy(Object comp, Class annotation)
  9. getMethodValue(Method method, Object instance)