[英]How to invoke private method in Gosu
I am trying to call a private method from gosu scratchpad using invoke() method.But i am not able to access that private method. 我正在尝试使用invoke()方法从gosu暂存器中调用私有方法。但是我无法访问该私有方法。 Can any one tell me the best way to invoke private methods in GOSU Language.Here is the code
任何人都可以告诉我用GOSU语言调用私有方法的最佳方法。
try{
var clazz = java.lang.Class.forName(ClaimSearchCriteriaImpl)
var method = clazz.getDeclaredMethod("generateSimpleActiveClaimViewQuery", null)
method.setAccessible(true)
var ss = method.invoke(clazz, null)as ClaimSearchCriteriaImpl
print("Result.."+ss)
} catch(exception){
print("***********"+exception)
}
When i try to execute this code i am getting the following exception 当我尝试执行此代码时,出现以下异常
java.lang.IllegalAccessException: Class program_.__Program__505 can not access a member of class com.guidewire.cc.domain.claim.impl.ClaimSearchCriteriaImpl with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:105)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261)
at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253)
at java.lang.reflect.Method.invoke(Method.java:599)
at program_.__Program__505.evaluate(Unknown Source)
at gw.internal.gosu.parser.GosuProgram.runProgram(GosuProgram.java:421)
at gw.internal.gosu.parser.GosuProgram.evaluate(GosuProgram.java:253)
at gw.internal.gosu.parser.GosuProgram_Proxy.evaluate(gw.internal.gosu.parser.GosuProgram_Proxy:2)
at gw.internal.gosu.parser.ExecutionEnvironment$1.evaluate(ExecutionEnvironment.java:543)
at gw.internal.gosu.parser.ExecutionEnvironment$1.runScript(ExecutionEnvironment.java:523)
at gw.internal.gosu.parser.ExecutionEnvironment$1.run(ExecutionEnvironment.java:489)
at java.lang.Thread.run(Thread.java:724)
The solution by @Shivanandam Sirmarigari actually works now, but there are a few issues. @Shivanandam Sirmarigari的解决方案现在可以正常工作,但是存在一些问题。
1st as already mentioned, you need an instance of on object to run on, from the documentation of Method.invoke 如前所述,您需要从Method.invoke文档中获取on对象的实例才能在其上运行
/** @param obj the object the underlying method is invoked from */
/ ** @param obj从* /调用基础方法的对象
public Object invoke(Object obj, Object... args)
公共对象invoke(Object obj,Object ... args)
2nd the ClaimSearchCriteriaImpl actually doesn't have a default constructor, so you need to use something like 2 ClaimSearchCriteriaImpl实际上没有默认构造函数,因此您需要使用类似
var obj = clazz.getDeclaredConstructor({ConstructorArgType}).newInstance({argTypeObj})
3rd your argTypeObj (possibly obj itself) might actually need a transaction to be created. 第三,您的argTypeObj(可能是obj本身)可能实际上需要创建一个事务。
**Try this code.. u did't Instantiated ** **尝试使用此代码。您未实例化**
try{
var clazz = java.lang.Class.forName(ClaimSearchCriteriaImpl)
var method = clazz.getDeclaredMethod("generateSimpleActiveClaimViewQuery", null)
Object obj= clazz.newInstance();
method.setAccessible(true)
var ss = method.invoke(obj, null)as ClaimSearchCriteriaImpl
print("Result.."+ss)
} catch(exception){
print("***********"+exception)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.