문제

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