在 Antlr 4 中,此类代码在通用 main 函数中工作。
public static void main(String[] args) {
.....
SicstusPrologParser parser = new SicstusPrologParser(tokens);
ParserRuleContext tree =(ParserRuleContext)parser.program();
tree.inspect(parser);
}
最后一条语句弹出一个模型 JDialog,其中显示解析器树结构。但我将代码复制到 junit 测试用例中,如下所示:
@Test
public void testParserClause() { //clause
.....
SicstusPrologParser parser = new SicstusPrologParser(tokens);
ParserRuleContext tree =(ParserRuleContext)parser.program();
tree.inspect(parser);
}
在我单击“确定”按钮之前,junt 刚刚关闭了由“tree.inpect(parser)”创建的 JDialog。我深入研究了“inspect”函数,它的主要逻辑流程如下:
.....
Callable<JDialog> callable = new Callable<JDialog>() {
JDialog result;
@Override
public JDialog call() throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
//fill the tree nodes and show the dialog.
result = showInDialog(viewer);
}
});
return result;
}
};
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
return executor.submit(callable);
}
finally {
executor.shutdown();
}
为什么模型 JDialog 在我使用它之前就关闭了?我使用了“inspect”的返回值,但它仍然有效。
Future<JDialog> fu = tree.inpect(parser);
fu.get();
有什么帮助吗?
最佳答案
提供了一个实用方法,以防您需要等待窗口关闭才能继续:
Future<JDialog> future = tree.inspect(parser);
Utils.waitForClose(future.get());
关于java - 由tree.inspect(parser)创建的Antlr 4 JDialog被JUnit自动关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18481687/