1

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)
Dummy Data
  • 49
  • 1
  • 10
  • Isn't the whole point of the 'private' keyword to disallow other classes or objects from using methods with that modifier? This seems to me that the application is working as designed. – ebwb Nov 07 '16 at 22:16
  • did you figure this out? – dawogfather Mar 29 '17 at 09:59

2 Answers2

0

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.

hakamairi
  • 4,464
  • 4
  • 30
  • 53
-1

**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)
}
Py-Coder
  • 2,024
  • 1
  • 22
  • 28