import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author weiweishen
* 帮助测试私有方法的工具类
*/
public class PrivateMethodTestUtil {
/**
* 测试私有方法
* @param classInstance
* @param methodName
* @param paramTypes
* @param args
*/
public static Object invoke(Class<?> clz,String methodName,Class<?>[] paramTypes,Object[] args){
try{
Method method = clz.getDeclaredMethod(methodName, paramTypes);
method.setAccessible(true);
try {
return method.invoke(clz, args);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(SecurityException e){
e.printStackTrace();
}catch(NoSuchMethodException e){
e.printStackTrace();
}
return null;
}
}
转:http://www.oschina.net/code/snippet_1174477_38976