我有一个 Groovy 脚本,其运行方式如下:
File scriptFile = ...;
ScriptEngine engine = ...;
String script = FileUtils.readFileToString(scriptFile);
Object evalResult = engine.eval(script, bindings);
不出所料,脚本文件中设置的断点不会触发。我可以改变什么来让它发挥作用?该脚本需要在较大程序的上下文中运行(没有单独的启动配置),并通过ScriptEngine
,并且该文件仅在运行时已知。
我正在使用这个 hack:我定义了一个 Java 类Debugger
看起来像这样:
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!" );
}
}
我有一个断点log.error
Eclipse 中的行。
现在,我可以将此行放入脚本中想要断点的位置:
Debugger.stopInDebugger();
当然,它不允许我轻松地单步执行脚本,但总比没有好。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)