ReflectUtil

package jp.co.ntt.ansl.picax.acc.common.util;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.InvocationTargetException;

/**
* some refact utilities for java class refact
*
*/
public class ReflectUtil {

    /**
     * invoke object method
     *
     * @param target the object to run method
     * @param methodName method name
     * @return object
     */
    public static Object invoke(Object target, String methodName) {
        return invoke(target, methodName, null);
    }

    /**
     * invoke object method
     *
     * @param target the object to run method
     * @param methodName method name
     * @param paramValues parameter values
     * @return object
     */
    public static Object invoke(Object target, String methodName, Object[] paramValues) {
        Class[] parameterTypes = getClassArray(paramValues);
        return invoke(target, methodName, parameterTypes, paramValues);
    }

    /**
     * invoke object method
     *
     * @param target the object to run method
     * @param methodName method name
     * @param paramTypes parameter types
     * @param paramValues parameter values
     * @return object
     */
    public static Object invoke(Object target, String methodName, Class[] paramTypes, Object[] paramValues) {
        try {
            Class clazz = target.getClass();
            Method method = clazz.getDeclaredMethod(methodName, paramTypes);
            return invoke(target, method, paramValues);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * invoke the target object method
     *
     * @param target the target object
     * @param method the method to execute
     * @param paramValues the method parameters
     * @return object of result
     */
    public static Object invoke(Object target, Method method, Object[] paramValues) {
        boolean accessible = method.isAccessible();
        try {
            method.setAccessible(true);
            if (Modifier.isStatic(method.getModifiers())) {
                return method.invoke(null, paramValues);
            } else {
                return method.invoke(target, paramValues);
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } finally {
            method.setAccessible(accessible);
        }
    }

    /**
     * judge if the object has the given method by name
     *
     * @param obj the object to judge
     * @param methodName the method name
     * @param paramTypes the parameterTypes
     * @return true or false
     */
    public static boolean hasMethod(Object obj, String methodName, Class[] paramTypes) {
        Class clazz = obj.getClass();
        try {
            clazz.getDeclaredMethod(methodName, paramTypes);
            return true;
        } catch (NoSuchMethodException e) {
            return false;
        }
    }

    /**
     * invoke the target class method
     *
     * @param clazz the clazz to run parameter
     * @param methodName the method to execute
     * @param paramValues the method parameters
     * @return object of result
     */
    public static Object invokeStatic(Class clazz, String methodName, Object[] paramValues) {
        try {
            Class[] parameterTypes = getClassArray(paramValues);;
            Method method = clazz.getDeclaredMethod(methodName,parameterTypes);
            return invokeStatic(method, paramValues);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * invoke the target class method
     *
     * @param clazz the clazz to run parameter
     * @param methodName the method to execute
     * @param paramTypes the method parameter types
     * @param paramValues the method parameters
     * @return object of result
     */
    public static Object invokeStatic(Class clazz, String methodName, Class[] paramTypes, Object[] paramValues) {
        try {
            Method method = clazz.getDeclaredMethod(methodName,paramTypes);
            return invokeStatic(method, paramValues);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * invoke the target class method
     *
     * @param clazz the clazz to run parameter
     * @param methodName the method to execute
     * @return object of result
     */
    public static Object invokeStatic(Class clazz, String methodName) {
        try {
            Method method = clazz.getDeclaredMethod(methodName,null);
            return invokeStatic(method, null);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * invoke the target class method
     *
     * @param method the method to execute
     * @param paramValues the method parameters
     * @return object of result
     */
    public static Object invokeStatic(Method method, Object[] paramValues) {
        boolean accessible = method.isAccessible();
        try {
            method.setAccessible(true);
            if (Modifier.isStatic(method.getModifiers())) {
                return method.invoke(null, paramValues);
            } else {
                throw new RuntimeException("can not run none static class without object");
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } finally {
            method.setAccessible(accessible);
        }
    }

    /**
     * judge if the object has the given method by name
     *
     * @param clazz the object to judge
     * @param methodName the method name
     * @param paramTypes the parameterTypes
     * @return true or false
     */
    public static boolean hasStaticMethod(Class clazz, String methodName, Class[] paramTypes) {
        try {
            clazz.getDeclaredMethod(methodName, paramTypes);
            return true;
        } catch (NoSuchMethodException e) {
            return false;
        }
    }

    /**
     * get class types according to the objects
     *
     * @param objects
     * @return null if the array is null
     */
    public static Class[] getClassArray(Object[] objects) {
        if(objects==null) return null;
        int arguments = objects.length;
        Class objectTypes [] = new Class[arguments];
        for (int i = 0; i < arguments; i++) {
            objectTypes[i] = objects[i].getClass();
        }
        return objectTypes;
    }
}

你可能感兴趣的:(reflect)