views:

44

answers:

3

Hey,

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?

A: 

Try powermock. It's a testing library that can do all kinds of stuff that you are supposed not to in greenfield development.

Boris Pavlović
+3  A: 
Method method = Foo.class.getDeclaredMethod("methodName");
method.setAccessible(true);
Object result = method.invoke(null);
Bozho
Wow, after searching for hours I never knew there was a difference between class.getMethod() and class.getDeclaredMethod()... cheers
Grofit
A: 

Wrap those methods that can be accessed by an object. I believe that famous IDEs have already refactoring tools just for this purpose (class wrappers).

Hayati Guvence