-
-
Save timyates/bffad7c32fcd6320bfdb to your computer and use it in GitHub Desktop.
Trying to use ConsitionalInterrupt from Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import groovy.lang.Binding; | |
import groovy.lang.GroovyShell; | |
import groovy.transform.ConditionalInterrupt; | |
import org.codehaus.groovy.ast.builder.AstBuilder; | |
import org.codehaus.groovy.ast.stmt.BlockStatement; | |
import org.codehaus.groovy.ast.stmt.ExpressionStatement; | |
import org.codehaus.groovy.control.CompilePhase; | |
import org.codehaus.groovy.control.CompilerConfiguration; | |
import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class Main { | |
public void test() { | |
Binding binding = new Binding() ; | |
binding.setProperty( "p", "X" ) ; | |
Map<String,Object> annotationParameters = new HashMap<>() ; | |
BlockStatement block = (BlockStatement)new AstBuilder().buildFromString( CompilePhase.CONVERSION, "{ -> p.isEmpty() }" ).get( 0 ) ; | |
ExpressionStatement expr = (ExpressionStatement)block.getStatements().get( 0 ) ; | |
annotationParameters.put( "value", expr.getExpression() ) ; | |
CompilerConfiguration config = new CompilerConfiguration() ; | |
config.addCompilationCustomizers( new ASTTransformationCustomizer( annotationParameters, ConditionalInterrupt.class ) ) ; | |
StringBuilder script = new StringBuilder() | |
.append( "class MyClass {\n" ) | |
.append( " void doit() {}\n" ) | |
.append( "}\n" ) | |
.append( "new MyClass().doit()" ) ; | |
new GroovyShell( binding, config ).evaluate( script.toString() ) ; | |
} | |
public static void main( String[] args ) { | |
new Main().test() ; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Exception in thread "main" groovy.lang.MissingPropertyException: No such property: binding for class: MyClass | |
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50) | |
at org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:86) | |
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231) | |
at MyClass.conditionalTransform1101184763$condition(Script1.groovy:1) | |
at MyClass.doit(Script1.groovy) | |
at MyClass$doit.call(Unknown Source) | |
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) | |
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108) | |
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112) | |
at Script1.run(Script1.groovy:4) | |
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:518) | |
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:556) | |
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:527) | |
at test.Main.test(Main.java:37) | |
at test.Main.main(Main.java:40) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.lang.reflect.Method.invoke(Method.java:483) | |
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can build closure expression yourself
http://stackoverflow.com/questions/23271000/how-do-you-set-up-a-groovy-conditionalinterrupt-from-java/