groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nikolay Totomanov <>
Subject Re: Always add no-args constructor at compile time
Date Tue, 14 Jul 2015 22:57:35 GMT
 The code looks very clean to me! Thanks a lot,
Keegan! I'll give it a try. 

 >-------- Оригинално писмо --------

 >От: Keegan Witt

 >Относно: Re: Always add no-args constructor at compile time


 >Изпратено на: 14.07.2015 06:04

   Oh, actually Opcodes is not needed, you can take that off so you don't need a dependency
    org.ow2.asm:asm .


    On Mon, Jul 13, 2015 at 10:53 PM, Keegan Witt >  wrote:

       Here's what I'm thinking.
 Use this as a configuration script.

         import  java.lang.annotation.ElementType
 import   java.lang.annotation.Retention
  import  java.lang.annotation.RetentionPolicy
 import   java.lang.annotation.Target
  import  org.codehaus.groovy.ast.ASTNode
 import  org.codehaus.groovy.ast.AnnotatedNode
 import  org.codehaus.groovy.ast.ClassNode
 import  org.codehaus.groovy.ast.ConstructorNode
 import  org.codehaus.groovy.ast.Parameter
 import  org.codehaus.groovy.ast.stmt.BlockStatement
 import  org.codehaus.groovy.control.CompilePhase
 import  org.codehaus.groovy.control.SourceUnit
 import  org.codehaus.groovy.transform.AbstractASTTransformation
 import   org.codehaus.groovy.transform.GroovyASTTransformation
  import   org.codehaus.groovy.transform.GroovyASTTransformationClass
  import  org.objectweb.asm.Opcodes

 @GroovyASTTransformation (phase = CompilePhase.CANONICALIZATION)
 public class  DefaultConstructorASTTransformation  extends  AbstractASTTransformation  implements
 Opcodes {
     public void  visit(ASTNode[] nodes, SourceUnit source) {
        init(nodes, source)
        AnnotatedNode parent = (AnnotatedNode) nodes[ 1 ]

         if  (parent  instanceof  ClassNode) {
            ClassNode cNode = (ClassNode) parent
             if  (!cNode.getDeclaredConstructor( new  Parameter[ 0 ])) {
                 final  BlockStatement body =  new  BlockStatement()
                cNode.addConstructor( new  ConstructorNode(ACC_PUBLIC,  new  Parameter[ 0
], cNode.EMPTY_ARRAY, body))
 @Retention (RetentionPolicy.RUNTIME)
 @Target (ElementType.TYPE)
 @GroovyASTTransformationClass ( "DefaultConstructorASTTransformation" )
 public  @ interface   DefaultConstructor  {}

withConfig(configuration) {

       Seemed to work in my tests, but everybody should feel free to point out anything I
goofed up.


         On Mon, Jul 13, 2015 at 6:24 PM, Keegan Witt 
  >  wrote:

            Sorry, I should have given you the example in the configuration script syntax


             withConfig(configuration) {
    ast(groovy.transform.TupleConstructor,  includes :[ '' ])

            But it doesn't matter, because the transformation removes existing constructors
from the class.
 Sorry, I got ahead of myself.


              On Mon, Jul 13, 2015 at 5:45 PM, Keegan Witt 
       >  wrote:

                   One way that comes to mind offhand would be 


 @groovy.transform.TupleConstructor(includeFields=false, includeProperties=false, includeSuperFields=false,






                   Anybody know why 



                  doesn't work?


                   On Mon, Jul 13, 2015 at 5:34 PM, Nikolay Totomanov 
            >  wrote:


                      I need to always add the default constructor to my classes.
                      Is it possible to achieve that using CompilerConfiguration





View raw message