简体   繁体   中英

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. Can any one tell me the best way to invoke private methods in GOSU Language.Here is the code

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.

1st as already mentioned, you need an instance of on object to run on, from the documentation of Method.invoke

/** @param obj the object the underlying method is invoked from */

public Object invoke(Object obj, Object... args)

2nd the ClaimSearchCriteriaImpl actually doesn't have a default constructor, so you need to use something like

var obj = clazz.getDeclaredConstructor({ConstructorArgType}).newInstance({argTypeObj})

3rd your argTypeObj (possibly obj itself) might actually need a transaction to be created.

**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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM