Skip to content
Advertisement

Invoke private static method with MethodUtils from Apache commons-lang3

Is it possible to invoke a private static method with MethodUtils?

JavaScript

This code throws the exception:

JavaScript

If I change method’s access modifier to public it works.

Advertisement

Answer

No, because MethodUtils.invokeStaticMethod() calls Class.getMethod() under the hood. Even if you try to hack the modifier it won’t be visible to the MethodUtils as it won’t see the modified Method reference:

JavaScript

will still fail with NoSuchMethodException just like plain reflection:

JavaScript

This will only work when the Method object is reused:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement