如何使用反射调用java中的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2407071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to invoke a method in java using reflection
提问by Steven
How can I invoke a method with parameters using reflection ?
如何使用反射调用带参数的方法?
I want to specify the values of those parameters.
我想指定这些参数的值。
采纳答案by polygenelubricants
Here's a simple example of invoking a method using reflection involving primitives.
这是一个使用涉及基元的反射调用方法的简单示例。
import java.lang.reflect.*;
public class ReflectionExample {
public int test(int i) {
return i + 1;
}
public static void main(String args[]) throws Exception {
Method testMethod = ReflectionExample.class.getMethod("test", int.class);
int result = (Integer) testMethod.invoke(new ReflectionExample(), 100);
System.out.println(result); // 101
}
}
To be robust, you should catch and handle all checked reflection-related exceptions NoSuchMethodException
, IllegalAccessException
, InvocationTargetException
.
为了健壮,您应该捕获并处理所有已检查的与反射相关的异常NoSuchMethodException
, IllegalAccessException
, InvocationTargetException
。
回答by leonm
You can use getClassin any Object to discover its class. Then you can use getMethodsto discover all the available methods. Once you have the correct method you can call invokewith any number of parameters
您可以在任何对象中使用getClass来发现它的类。然后您可以使用getMethods来发现所有可用的方法。一旦你有了正确的方法,你就可以用任意数量的参数调用invoke
回答by shukshuk
this is the easiest way I know of, it needs to be surrounded with try & catch:
这是我所知道的最简单的方法,它需要被 try & catch 包围:
Method m = .class.getDeclaredMethod("", arg_1.class, arg_2.class, ... arg_n.class); result = () m.invoke(null,(Object) arg_1, (Object) arg_2 ... (Object) arg_n);
方法 m = .class.getDeclaredMethod("", arg_1.class, arg_2.class, ... arg_n.class); 结果 = () m.invoke(null,(Object) arg_1, (Object) arg_2 ... (Object) arg_n);
this is for invoking a static method, if you want to invoke a non static method, you need to replace the first argument of m.invoke() from null to the object the underlying method is invoked from.
this 用于调用静态方法,如果要调用非静态方法,则需要将 m.invoke() 的第一个参数从 null 替换为调用底层方法的对象。
don't forget to add an import to java.lang.reflect.*;
不要忘记向 java.lang.reflect.* 添加一个导入;
回答by sumit sharma
To call a class method using reflection is very simple. You need to create a class and generate method in it. like as follows.
使用反射调用类方法非常简单。您需要创建一个类并在其中生成方法。如下。
package reflectionpackage;
public class My {
public My() {
}
public void myReflectionMethod() {
System.out.println("My Reflection Method called");
}
}
and call this method in another class using reflection.
并使用反射在另一个类中调用此方法。
package reflectionpackage;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectionClass {
public static void main(String[] args)
throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class c=Class.forName("reflectionpackage.My");
Method m=c.getDeclaredMethod("myReflectionMethod");
Object t = c.newInstance();
Object o= m.invoke(t);
}
}