-
1. Re: Reflection to call Java method from Application from Helper Class
adinn Apr 10, 2015 4:45 AM (in response to john.martin.3728)Hi John,
Hmm, seems like you already tried to follow the advice I just posted in response to your last note.
My User defined Helper Class want to Access method from Class B using Reflection but it throws Exception at
Class.forName("A.B"); as ClassNotFoundException.
Class.forName uses its caller's classloader to resolve the class lookup. If your helper is in the bootstrap or system classpath then this will only find a class that is also in the bootstrap or system classpath, respectively.
There are two things you can do to allow your helper to find the required class:
1) use the target class's classloader to the lookup the class you need by calling Classloader.findClass(String). Here is one way of providing the helper method with the required loader
RULE helper rule CLASS org.my.app.TargetClass METHOD org.my.app.TargetMethod AT ENTRY HELPER org.my.helper.MyHelper BIND target = $0.getClass() loader = target.getClassLoader() IF TRUE DO helperDoReflectiveCall($1, $2, loader) ENDRULE
You helper method will need to call loader.findClass("com.mysql.jdbc.Driver") to lookup the desired class.
2) Just refer to the desired class by name in the rule and pass it into the helper call. Class literals are perfectly legitimate Byteman rule expressions. They are resolved using the loader of the target class:
RULE helper rule CLASS org.my.app.TargetClass METHOD org.my.app.TargetMethod AT ENTRY HELPER org.my.helper.MyHelper IF TRUE DO helperDoReflectiveCall($1, $2, com.mysql.jdbc.Driver.class) ENDRULE