package com.pigetest.util; import java.lang.reflect.Method; public class PrivateMethodTestHelper { public static Object invoke(String clazzName,String methodName,Object...params){ try { Class<?> clazz=Class.forName(clazzName); Object obj=clazz.newInstance(); Method[] methods = clazz.getDeclaredMethods(); Method callMethod=null; for(Method method:methods){ if(method.getName().equals(methodName)){ callMethod=method; break; } } callMethod.setAccessible(true); return (Object) callMethod.invoke(obj,params); } catch (Exception e) { e.printStackTrace(); } return null; } public static void main(String[] args) { int value=(Integer) PrivateMethodTestHelper.invoke("com.pigetest.util.AddNumber","addNumber",1,2); System.out.println(value); } }
时间: 2024-10-13 17:48:13