Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you reflect on a private static method in Java

First of all this is not some normal action I would want to do, however this fringe case involving alot of legacy code I cannot touch, and unit tests that need to be written for newer stuff.

Anyway I have a class and I can get access to all fields and methods through reflection, except private/protected static ones. So is there any way to get access to these private static method through reflection?

like image 898
Grofit Avatar asked Jul 17 '26 16:07

Grofit


1 Answers

Method method = Foo.class.getDeclaredMethod("methodName");
method.setAccessible(true);
Object result = method.invoke(null);
like image 158
Bozho Avatar answered Jul 19 '26 05:07

Bozho