java 使用反射调用返回值的静态方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12367961/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:35:56  来源:igfitidea点击:

Using reflection to call static method for return value

javareflection

提问by Austin

Possible Duplicate:
How do I invoke a private static method using reflection (Java)?

可能的重复:
如何使用反射(Java)调用私有静态方法?

So there is a method named something along the lines of "getInstance" which just returns an instance of a certain class. It's a static method with no arguments.

因此,有一个名为“getInstance”的方法,它只返回某个类的实例。这是一个没有参数的静态方法。

How could I call that method, and get the return value (the instance) of the class? Every method I try to use requires me to have an instance of the class in the arguments it seems.

我如何调用该方法并获取该类的返回值(实例)?我尝试使用的每个方法都要求我在参数中包含一个类的实例。

For example, I try to use

例如,我尝试使用

Method method = classLoader.loadClass("testClass").getMethod("getInstance", null);
            Object object = method.invoke(null, null);

but I always get a null pointer exception at this line,

但我总是在这一行得到一个空指针异常,

Object object = method.invoke(null, null);

Which I'm assuming I get since the object it asks for is null.

我假设我得到了,因为它要求的对象为空。

Thanks for any help.

谢谢你的帮助。

Edit: Method is not null. I am doing a System.out.println(method == null);and it prints out false.

编辑:方法不为空。我正在做一个System.out.println(method == null);,它打印出来是假的。

回答by Peter Lawrey

You don't want nullas your argument or parameter list. Instead you can do

你不想null作为你的参数或参数列表。相反,你可以做

Method method = classLoader.loadClass("testClass").getMethod("getInstance", new Class[0]);
Object object = method.invoke(null, new Object[0]);

or the following as they are varargs methods.

或以下,因为它们是可变参数方法。

Method method = classLoader.loadClass("testClass").getMethod("getInstance");
Object object = method.invoke(null);
// or works but is perhaps confusing.
Object object = method.invoke(null, null);

回答by quickanalysis

For me the following two pieces of code both work correctly for static method and print out the returned value.

对我来说,以下两段代码都可以正确用于静态方法并打印出返回值。

Method method = myClass.getMethod("getInstance", null);
Object object = method.invoke(null);
System.out.println("returned value: "+object);

as well as

Method method = myClass.getMethod("getInstance", null);
Object object = method.invoke(null,null);
System.out.println("returned value: "+object);

I think that it depend on which Java version you are using (no varargs before 1.5). I'm using Java runtime v1.6.

我认为这取决于您使用的 Java 版本(1.5 之前没有可变参数)。我正在使用 Java 运行时 v1.6。

回答by ILMTitan

That is not the problem. If the underlying method is static, then the specified obj argument is ignored. It may be null.

那不是问题。 如果底层方法是静态的,则指定的 obj 参数将被忽略。它可能为空。

Therefore it seems that method itself must be null.

因此似乎方法本身必须为空。

回答by TedTrippin

I bet the problem is in your getInstancemethod. Can you debug it or post it.

我敢打赌,问题出在你的getInstance方法上。你可以调试它或发布它。

回答by J.A.I.L.

With getMethod("getInstance", null)you are saying "get a method with name 'getInstance' and no parameters".

有了getMethod("getInstance", null)你说的“得与名‘的getInstance’无参数的方法”。

When you type method.invoke(null, null)you are saying "invoke this method on object 'null', and pass 'null' as the first parameter".

当您键入时,method.invoke(null, null)您是在说“在对象 'null' 上调用此方法,并将 'null' 作为第一个参数传递”。

You should remove that last nulland just type:

您应该删除最后一个,null然后输入:

Object object = method.invoke(null);