Frage

Is there a way to invoke private methods in Seam Component. I used the following code but I found that there is no private methods in declared methods. So, I get NoSuchMethodException.

Object obj = Component.getInstance("myComponent");
Method myMethod = obj.getClass.getDeclaredMethod("myPrivateMethod",String.class);
myMethod.invoke(obj,"myParameter");
War es hilfreich?

Lösung

Make setAccessible true.

 Method myMethod = obj.getClass.getDeclaredMethod("myPrivateMethod",String.class);
 method.setAccessible(true);
 Object r = myMethod.invoke(obj,"myParameter");

A value of true indicates that the reflected object should suppress Java language access checking when it is used. for more look in API.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top