Skip to content

Instantly share code, notes, and snippets.

@timyates
Created April 24, 2014 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timyates/bffad7c32fcd6320bfdb to your computer and use it in GitHub Desktop.
Save timyates/bffad7c32fcd6320bfdb to your computer and use it in GitHub Desktop.
Trying to use ConsitionalInterrupt from Java
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() ;
}
}
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)
@trajano
Copy link

trajano commented Apr 24, 2014

Hmm can you have a simpler script? The way I did it was just def a=1; That way there shouldn't be any class issues. The intended result was a script that is written by the test team for automation so I don't think they'd do advanced stuff like classes at the moment (though we can add it later).

@SerCeMan
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment