Details
-
Bug
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
2.2.1
-
None
Description
Say for example there's the following annotation provided by a library (acme-lib) that is in no way related to Groovy
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) public @interface DependsOn { String[] value(); }
You'd like to reuse this annotation to trigger a local AST transformation, problem is that the annotation's source must be changed to the following
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) @GroovyASTTransformationClass("com.acme.ast.DependsOnASTTransformation") public @interface DependsOn { String[] value(); }
Now acme-lib has a hard dependency on Groovy, and that's assuming you can even touch the code and recompile it. Say for the sake of example you'd like to "hijack" some of the JPA annotations, you can't simply push Groovy to whatever JSR controls JPA's codebase (extra points for inserting Boromir's meme here?
So what if we could write the following code instead
def configuration = new CompilerConfiguration() configuration.addCompilationCustomizers(new ASTTransformationCustomizer(DependsOn, "com.acme.ast.DependsOnASTTransformation")) def shell = new GroovyShell(configuration) shell.evaluate(""" class MyClass { }""")