Pergunta

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");
Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top