Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
1.5.4, 1.5.5, 1.5.6
-
None
-
None
-
Patch
Description
The following code attempts to instantiate an object dynamically inside a groovy script. It currently fails with a NullPointerException:
String[] roots = new String[] { "some/path" }; GroovyScriptEngine gse = new GroovyScriptEngine(roots); binding.setVariable("instantiateMe", "com.package.TheClass"); gse.run("hello.groovy", binding);
*******Now in hello.groovy script
def clazz = getClass().getClassLoader().loadClass(instantiateMe, true) //<-- NullPointerException occurs here. def h = clazz.newInstance()
The fix is to change this code around line 101 in the GroovyScriptEngine:
currentCacheEntry.dependencies.put( dependentScriptConn.getURL(), new Long(dependentScriptConn.getLastModified()));
to this:
if(currentCacheEntry != null) currentCacheEntry.dependencies.put( dependentScriptConn.getURL(), new Long(dependentScriptConn.getLastModified()));
This sort of dynamic instantiation also fails in 1.6-beta-1, but I haven't looked at the code there. I expect the same fix will apply.
Note: I'll be glad to apply the fix myself. I'm very interested in having this work, because I have lots of code that depends on it.