質問

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");
役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top