1. Overview
1.概述
In this quick tutorial, we’ll discuss how to invoke a static method in Java by using the Reflection API.
在这个快速教程中,我们将讨论如何通过使用反射API在Java中调用一个静态方法。
We’ll cover two different scenarios:
我们将介绍两种不同的情况。
- The static method is public.
- The static method is private.
2. An Example Class
2.一个实例班
To make the demonstration and explanation easier, let’s first create a GreetingAndBye class as the example:
为了使演示和解释更容易,让我们首先创建一个GreetingAndBye类作为例子。
public class GreetingAndBye {
public static String greeting(String name) {
return String.format("Hey %s, nice to meet you!", name);
}
private static String goodBye(String name) {
return String.format("Bye %s, see you next time.", name);
}
}
The GreetingAndBye class looks pretty simple. It has two static methods, one public and one private.
GreetingAndBye类看起来相当简单。它有两个static方法,一个public和一个private。
Both methods accept a String argument and return a String as the result.
这两个方法都接受一个String参数并返回一个String作为结果。
Now, let’s call the two static methods using the Java Reflection API. In this tutorial, we’ll address the code as unit test methods.
现在,让我们使用Java Reflection API调用这两个静态方法。在本教程中,我们将把这些代码作为单元测试方法来处理。
3. Invoking a public static Method
3.调用一个public static方法
First, let’s see how to call the public static method:
首先,让我们看看如何调用public static方法。
@Test
void invokePublicMethod() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<GreetingAndBye> clazz = GreetingAndBye.class;
Method method = clazz.getMethod("greeting", String.class);
Object result = method.invoke(null, "Eric");
Assertions.assertEquals("Hey Eric, nice to meet you!", result);
}
We should note that we need to handle the required checked exceptions when we use the Reflection API.
我们应该注意,当我们使用Reflection API时,我们需要处理所需的checked exceptions。
In the example above, we first obtain the instance of the class we want to test, which is GreetingAndBye.
在上面的例子中,我们首先获得我们要测试的类的实例,也就是GreetingAndBye。
After we have the class instance, we can get the public static method object by calling the getMethod method.
在我们有了类的实例之后,我们可以通过调用getMethod方法获得公共静态方法对象。
Once we hold the method object, we can invoke it simply by calling the invoke method.
一旦我们持有method对象,我们就可以通过调用invoke方法来调用它。
It’s worthwhile to explain the first argument of the invoke method. If the method is an instance method, the first argument is the object from which the underlying method is invoked.
值得解释的是,invoke方法的第一个参数。如果该方法是一个实例方法,第一个参数是底层方法被调用的对象。
However, when we invoke a static method, we pass null as the first argument, as static methods don’t require an instance in order to be called.
然而,当我们调用一个静态方法时,我们传递null作为第一个参数,因为静态方法不需要一个实例就可以被调用。
Finally, if we run the test, it’ll pass.
最后,如果我们运行测试,它将通过。
3. Invoking a private static Method
3.调用一个private static方法
Invoking a private static method is pretty similar to invoking a public one. Let’s take a look at the code first:
调用一个private static方法与调用一个public方法非常相似。让我们先看一下代码。
@Test
void invokePrivateMethod() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<GreetingAndBye> clazz = GreetingAndBye.class;
Method method = clazz.getDeclaredMethod("goodBye", String.class);
method.setAccessible(true);
Object result = method.invoke(null, "Eric");
Assertions.assertEquals("Bye Eric, see you next time.", result);
}
As we can see in the code above, when we try to get the Method object of a private method, we should use getDeclaredMethod instead of getMethod.
正如我们在上面的代码中所看到的,当我们试图获得一个private方法的Method对象时,我们应该使用getDeclaredMethod,而不是getMethod。
Moreover, we need to call method.setAccessible(true) to invoke a private method. This will ask the JVM to suppress the access control checks on this method.
此外,我们需要调用method.setAccessible(true)来调用一个private方法。这将要求JVM抑制对该方法的访问控制检查。
Thus, it allows us to invoke the private method. Otherwise, an IllegalAccessException exception will be raised.
因此,它允许我们调用该私有方法。否则,将引发一个IllegalAccessException异常。
The test will pass if we execute it.
如果我们执行该测试,就会通过。
4. Conclusion
4.总结
In this short article, we’ve addressed how to invoke static methods using the Java Reflection API.
在这篇短文中,我们已经解决了如何使用Java Reflection API调用静态方法的问题。
As always, the complete code can be found over on GitHub.
一如既往,完整的代码可以在GitHub上找到,。