简体   繁体   中英

Debugging a compiled Groovy script in Eclipse

I'm trying to debug a Groovy script in Eclipse from a JUnit test. The Groovy code is part of a larger Java application that runs in Tomcat. For various reasons our system is set up to use compiled JSR223 expressions. Here's the abbreviated code snippet:

GroovyScriptEngineImpl engine = new GroovyScriptEngineImpl();
Resource r =
  new ClassPathResource("groovy/transformations/input/Foo.groovy");
String expression = IOUtils.toString(r.getInputStream());
CompiledScript script = engine.compile(expression);
String result = (String) script.eval(new SimpleBindings(bindings));

The test runs fine, but even though I have a breakpoint set in Foo.groovy, and the file is on the classpath, the breakpoint never gets hit when debugging. I'm guessing this doesn't work because there's no association between the expression in String format and the actual file that contains it. So is there a way of creating this association between the String and its corresponding file name? As mentioned, I need to use a CompiledScript. As a side note, I have been able to hit the breakpoint in the debugger with the same Groovy script when using this approach:

Resource r =
  new ClassPathResource("groovy/transformations/input/Foo.groovy");
GroovyShell shell = new GroovyShell(new Binding(bindings));
String str = (String) shell.evaluate(r.getFile());

But of course, in this case the Groovy engine loads the file directly. Any hints as to how to get the first example to work are greatly appreciated. Thanks.

You are exactly right that this has to do with creating a class from a string. GroovyScriptEngineImpl likes to assign arbitrary names to the compiled script since it assumes everything comes from a string. The GroovyShell, however, generates the script name based off of the file that the script comes from, and this is the link that the debugger needs.

I'd perhaps recommend that you avoid using GroovyScriptEngineImpl and use GroovyShell.parse instead. And then, you can create a GroovyCompiledScript from the result of GroovyShell.parse and using a new GroovyScriptEngineImpl. Something like this:

File f = getScriptFile();
Script s = new GroovyShell().parse(f);
CompiledScript cs = new GroovyCompiledScript(new GroovyScriptEngineImpl(), s.getClass());
...

Note that I haven't tried this yet, but based on my experience, this should work.

If you are feeling really good-spirited, I'd raise a jira on the groovy issue tracker to ensure that you can pass in a proper name for scripts created using the GroovyScriptEngineImpl.

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