Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-6501

Decouple AST transformation from its interface via ASTTransformationCustomizer

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.2.1
    • 2.3.0-beta-1
    • Compiler
    • 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 {
      }""")
      

      Attachments

        Activity

          People

            melix Cédric Champeau
            aalmiray Andres Almiray
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: