如何使用反射 (Java) 调用私有静态方法?
我想调用一个私有静态方法。我有它的名字。我听说可以使用Java反射机制来完成。我该怎么做呢?
编辑:我在尝试调用该方法时遇到的一个问题是如何指定其参数的类型。我的方法接收一个参数,其类型是 Map。因此我无法执行 Map
(在运行时,由于 Java 类型擦除,不存在 Map 这样的东西)。还有其他方法可以获取该方法吗?
I would like to invoke a private static method. I have its name. I've heard it can be done using Java reflection mechanism. How can I do it?
EDIT: One problem I encountered when trying to invoke the method is how to specify the type of its argument. My method receives one argument and its type is Map. Therefore I cannot do Map<User, String>.TYPE
(In run time there's no such a thing as Map because of Java Type erasure). Is there another way to get the method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设您想调用 MyClass.myMethod(int x);
Let's say you want to call MyClass.myMethod(int x);
从 反射教程 调用 main
Invoke main from reflection tutorial
不,你不能说
Map.class
。这是因为类型擦除。在运行时,没有这样的事情。幸运的是,您可以说只是普通的旧
Map.class
。运行时都是一样的。如果这些警告让您感到困扰,请搜索与泛型和类型擦除相关的其他问题,这里有关于该主题的大量信息。
No, you can't say
Map<K,V>.class
. This is because of type erasure. At runtime, there's no such thing.Luckily, you can say just plain old
Map.class
. It's all the same at runtime.If the warnings bother you, search for other questions related to generics and type erasure, there's a wealth of information on the subject here.
我使用一个方法来封装获取目标方法然后调用它。当然,可能有一些限制。以下是放入类中的方法及其 JUnit 测试:
}
I use a single method that encapsulates getting the target method and then invoking it. Probably has some limitations, of course. Here is the method put into a class and its JUnit test:
}
有许多可能引发的检查异常。 parameterTypes和parameters都是椭圆形参数(可变长度),根据需要填写。 JVM 按照规范具有强类型调用约定,因此您需要了解参数类型。
话虽如此,除非您正在编写某种应用程序容器、服务器组件容器、类似 RMI 的系统或基于 JVM 的语言,否则您应该避免这样做。
There are a number of checked exceptions which may be thrown. Both parameterTypes and parameters are ellipse arguments (variable length), fill them in as needed. The JVM by specification has a strongly typed calling convention so you need to know the parameter types.
With that said, unless you are writing some sort of application container, server component container, RMI-like system, or JVM based langauge you should avoid doing this.