Can you think of a private static method in Java?

First of all, this is not a normal action that I would like to do, but this case is with a lot of outdated codes that I cannot touch, and unit tests that need to be written for newer things.

In any case, I have a class, and I can access all fields and methods through reflection, with the exception of private / protected static ones. So, is there a way to access this private static method through reflection?

+3
source share
3 answers
Method method = Foo.class.getDeclaredMethod("methodName");
method.setAccessible(true);
Object result = method.invoke(null);
+7
source

powermock. , , , , .

0

Wrap the methods that the object can access. I believe that well-known IDEs already have refactoring tools for this purpose only (cool shells).

0
source

Source: https://habr.com/ru/post/1769704/


All Articles