如果您需要通过 Reflection 调用类,则无需额外的第三方库即可执行此操作,例如
//calling class via reflection
Class cls = Class.forName("com.test.reflection.AppTest");
Object obj = cls.newInstance();
//call the printIt method
Method method = cls.getDeclaredMethod("printIt", noparams);
method.invoke(obj, null);
//call the printItString method, pass a String param
method = cls.getDeclaredMethod("printItString", paramString);
method.invoke(obj, new String("mkyong"));
//call the printCounter method
method = cls.getDeclaredMethod("printCounter", noparams);
method.invoke(obj, null);