Can you reflect on private static methods in Java?

First of all, this is not some normal action I want to do, but this marginal situation involves a lot of legacy code that I can't touch and unit tests that need to be written for updates

Anyway, I have a class where I can access all fields and methods through reflection, except private / protected static Is there any way to access these private static methods through reflection?

Solution

Method method = Foo.class.getDeclaredMethod("methodName");
Method method = Foo.class.getDeclaredMethod("methodName");
method.setAccessible(true);
Object result = method.invoke(null);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>