Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging Groovy scripts running in a ScriptEngine from Eclipse

I have a Groovy script which is run like this:

File scriptFile = ...;
ScriptEngine engine = ...;
String script = FileUtils.readFileToString(scriptFile);
Object evalResult = engine.eval(script, bindings);

Unsurprisingly, breakpoint set in the script file doesn't trigger. What can I change to make it work? The script needs to be run in the context of the larger program (no separate launch configuration), and through a ScriptEngine, and the file is only known at runtime.

like image 906
Alexey Romanov Avatar asked Aug 01 '12 11:08

Alexey Romanov


People also ask

How do I debug a Groovy script?

Open your Groovy application in the editor. In the left gutter, set your breakpoints for the lines of code you want to debug. The debugger is aware of the Groovy syntax, and you can evaluate an expression on the breakpoint if you need.

How do I debug API in eclipse?

Enabling remote debuggingSelect Run → Debug Configurations…. The Debug Configurations dialog box is displayed. Select Remote Java Applications. Select New launch configuration as illustrated in Remote Java application's debug configuration.


1 Answers

I'm using this hack: I've defined a Java class Debugger which looks like this:

public class Debugger {

    private static final Logger log = LoggerFactory.getLogger( Debugger.class );

    /**
     * Invoke this method anywhere to end up in the Java debugger.
     * 
     * <p>Sometimes, you have code that might eventually be executed or you have a place
     * where you want to stop but no code line.
     * 
     * <p>In these case, use this method.
     * 
     * <p>Don't forget to set a breakpoint, first :-)
     * */
    public static void stopInDebugger() {
        log.error( "Please set a breakpoint in Debugger.stopInDebugger() and try again whatever you just did!" );
    }
}

I have a breakpoint in the log.error line in Ecipse.

Now, I can put this line into the script where I want a breakpoint:

Debugger.stopInDebugger();

Granted, it doesn't allow me to easily step through the script but it's better than nothing.

like image 58
Aaron Digulla Avatar answered Oct 01 '22 05:10

Aaron Digulla