Package asteroid

Class AbstractLocalTransformation<T extends Annotation,​S extends AnnotatedNode>

  • Type Parameters:
    T - The annotation type used to mark the transformation
    S - The annotated node type. It has to be a subtype of AnnotatedNode. As a rule of thumb think of any type that can be annotated (a method, a type...)
    All Implemented Interfaces:
    groovyjarjarasm.asm.Opcodes, ASTTransformation, ErrorCollecting
    Direct Known Subclasses:
    PhaseTransformation

    public abstract class AbstractLocalTransformation<T extends Annotation,​S extends AnnotatedNode>
    extends AbstractASTTransformation
    This class is an abstraction to process certain nodes annotated with a specific annotation node type

    Types indicate wich nodes are affected:

    Lets say we wanted to build a transformation to transform methods annotated by @MyAnnotation:
    
     class MyCustomTransformation extends AbstractLocalTransformation<MyAnnotation, MethodNode> {
         @Override
         void doVisit(AnnotationNode annotation, final MethodNode annotated){
             // implementation
         }
     }
     
    In this example transformation will be applied only to those ASTNode instances of type MethodNode annotated by @MyAnnotation

    Checks:

    If you would like to check something before applying the transformation you can use a contract-like programming structure. If you have worked with Spock or GContracts you are already used to it. The idea is to have two blocks within doVisit(org.codehaus.groovy.ast.AnnotationNode, S) method, one for assertions, the other to call the transformation.
    
     @Override
     void doVisit(AnnotationNode annotation, final ClassNode annotated){
         check: 'class has correct name'
         annotated.name == 'MyBusinessService'
    
         then: 'we will add a new method'
         // transformation code
     }
     
    Any expression within the check block will be treated as an assertion statement. If any of the assertion fails the compilation will fail.

    When using Groovy to implement your transformation. If you annotate your transformation with Phase you will benefit of the following:

    Since:
    0.2.0
    • Constructor Detail

      • AbstractLocalTransformation

        public AbstractLocalTransformation​(Class<T> annotation)
        Default constructor
        Parameters:
        annotation - The type of the annotatino used to trigger the transformation
        Since:
        0.2.0
    • Method Detail

      • doVisit

        public abstract void doVisit​(AnnotationNode annotation,
                                     S annotated)
        This method processes all annotated nodes annotated with a specific annotation node.
        Parameters:
        annotation - the annotation information
        annotated - the ast node annotated with the specific annotation
        Since:
        0.2.0