Details
-
Bug
-
Status: Closed
-
Critical
-
Resolution: Fixed
-
2.0.2
-
None
-
Reproduced with Java 6 and Java 7.
Description
The below script works in Groovy 2.0.1 and earlier but stopped working in Groovy 2.0.2. I invoke a global glosure named "helloWorld". I use two different strings named "helloWorld". Sometimes this works, sometimes this fails.
I suspect the problem is in GroovyScriptEngineImpl. The globalClosures map was changed in 2.0.2. It is no longer a java.util.Map but is some other map (ManagedConcurrentMap). I suspect it may be a problem with the map using System.identityHashCode() is its mechanism for looking up Closures. Obviously, any string named "helloWorld" should work here.
SEVERITY:
This problem causes breakage in real applications that use JSR 223 API for Groovy and other script engines. A user would need to roll back to 2.0.1.
EXAMPLE:
import javax.script.Compilable; import javax.script.CompiledScript; import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class HelloWorld { public static void main(String[] args) throws Exception { String script = "void helloWorld() { println \"Hello World\" }"; ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("groovy"); CompiledScript compiled = ((Compilable) engine).compile(script); compiled.eval(); String name = "helloWorld"; String name2 = new String(name.toCharArray()); if (!name.equals(name2)) { System.out.println("should be equal!"); } ((Invocable) engine).invokeFunction(name); // works ((Invocable) engine).invokeFunction(name); // still works ((Invocable) engine).invokeFunction(name2); // fails since Groovy 2.0.2 } }