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;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值