简体   繁体   English

如何从Java设置Groovy ConditionalInterrupt?

[英]How do you set up a Groovy ConditionalInterrupt from Java?

I am trying to make a Groovy throw an InterruptedException using ConditionalInterrupt. 我正在尝试使用ConditionalInterrupt使Groovy抛出InterruptedException。 I am trying to build it from Java. 我正在尝试从Java构建它。 This is what I have done so far 这是我到目前为止所做的

final Binding binding = new Binding();
final CompilerConfiguration config = new CompilerConfiguration();
final Map<String, Object> annotationParameters = new HashMap<String, Object>();

// this is my control variable, the code should only work if p is ""
final String p = "X"; 

// at this point I tried various things primarily to get the following closure
// { p.isEmpty() }
// intetionally removed because it looked really messy

annotationParameters.put("value", aStatementContainingClosure);

config.addCompilationCustomizers(new ASTTransformationCustomizer(
    annotationParameters, ConditionalInterrupt.class));

final GroovyShell shell = new GroovyShell(binding, config);
try {
    shell.evaluate("def a = 1;");
fail("Should not reach here");
} catch (final Exception e) {
    e.printStackTrace();
assertTrue(e instanceof InterruptedException);
}

Just an FYI the following code block represents the last few tries I have done to get this working... (will remove this once we get a proper answer) 仅供参考,以下代码块代表我为完成此工作而进行的最后几次尝试...(一旦获得正确答案,将删除此内容)

    binding.setVariable("p", p);

    final GroovyShell shell1 = new GroovyShell(binding);
    final Closure closure2 = (Closure) shell1
            .evaluate("{ it -> p.isEmpty() }");
    //final Closure closure2 = (Closure) shell1
    //      .evaluate("{ p -> p.isEmpty() }");
    final BlockStatement code = (BlockStatement) new AstBuilder()
            .buildFromString("def p = { System.out.println(1) }").get(0);
    final ClosureExpression cex = (ClosureExpression) ((ReturnStatement) code
            .getStatements().get(0)).getExpression();
    // final Expression expression = new BooleanExpression(
    // new MethodCallExpression(new VariableExpression("p"),
    // "isEmpty", new TupleExpression()));
    // final Expression expression = new MethodPointerExpression(
    // new VariableExpression("p"), new ConstantExpression("isEmpty"));
    // // final Statement code = new ExpressionStatement(expression);
    final ClosureExpression closure = new ClosureExpression(
            new Parameter[0], code);
    annotationParameters.put("value", cex);

You can do it, but you should build closure yourself! 您可以做到,但是您应该自己构建闭包!

For example, my code, closure expression check static volatile field 例如我的代码,关闭表达式检查静态volatile字段

    Map<String, Object> map = new HashMap<>();
    try {
        map.put("value", new ClosureExpression(Parameter.EMPTY_ARRAY, 
                            GeneralUtils.returnS(GeneralUtils.eqX(
                                        GeneralUtils.fieldX(FieldNode.newStatic(ScriptService.class, "run")), 
                                        GeneralUtils.constX(false)) 
                                        )));
    } catch (SecurityException | NoSuchFieldException e) {
        throw new RuntimeException(e);
    }
    configuration.addCompilationCustomizers(
            new ASTTransformationCustomizer(map, ConditionalInterrupt.class));

Groovy is really powerful! Groovy真的很强大!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM