You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Nikolay Totomanov <nt...@abv.bg> on 2015/07/13 23:34:34 UTC

Always add no-args constructor at compile time

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

Re: Always add no-args constructor at compile time

Posted by Nikolay Totomanov <nt...@abv.bg>.
Looks promising. Thank you for the heads up







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

 >От: Keegan Witt keeganwitt@gmail.com

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

 >До: users@groovy.incubator.apache.org

 >Изпратено на: 24.07.2015 02:29



 
 
   
   FYI: I opened 
    GROOVY-7522 
and 
    GROOVY-7523 
which, if agreed to, will make your config script a lot simpler.
   

    
    

    
    
    -Keegan
    
    
    

     
     On Fri, Jul 17, 2015 at 4:49 PM, Keegan Witt 
       keeganwitt@gmail.com >  wrote:
     

      
       
       Awesome!
 Glad I could help.
       

       
       
        
         
         

          
          On Fri, Jul 17, 2015 at 12:48 PM, Nikolay Totomanov 
            ntotomanov@abv.bg >  wrote:
          

           
            
             Thanks a lot,
Keegan! 
            
It works like a charm!
            

            

            

            
 >-------- Оригинално писмо -------- 
            
 >От: Nikolay Totomanov 
             ntotomanov@abv.bg  
            

              >Относно: Re: Always add no-args constructor at compile time 
 >До:  users@groovy.incubator.apache.org  
  >Изпратено на: 15.07.2015 01:57 
            

             
              
              
 
               
                The code looks very clean to me! 
               Thanks a lot,
Keegan! 
               
                I'll give it a try. 
                 
                
 
                
 
                
 
                
 >-------- Оригинално писмо -------- 
                
 >От: Keegan Witt 
                 keeganwitt@gmail.com  
                
 >Относно: Re: Always add no-args constructor at compile time 
                
 >До: 
                 users@groovy.incubator.apache.org  
                
 >Изпратено на: 14.07.2015 06:04 
                
 
                
 
                 
                  Oh, actually Opcodes is not needed, you can take that off so you don't need a dependency on 
                  org.ow2.asm:asm . 
                 
 
                  
                  
                 
 
                  
                   On Mon, Jul 13, 2015 at 10:53 PM, Keegan Witt 
                    keeganwitt@gmail.com >  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) {
    ast(DefaultConstructor)
}  
                     
 
                      
                     
                      Seemed to work in my tests, but everybody should feel free to point out anything I goofed up. 
                       
   
                      
                       
                        
                       
 
                       -Keegan
   
                     
                     
                      
                       
                      
 
                       
                        On Mon, Jul 13, 2015 at 6:24 PM, Keegan Witt 
                         keeganwitt@gmail.com >  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. 
                            
   
                           
                            
                             
                            
 
                            -Keegan
   
                          
                          
                           
                            
                           
 
                            
                             On Mon, Jul 13, 2015 at 5:45 PM, Keegan Witt 
                              keeganwitt@gmail.com >  wrote: 
                            
 
                              
                               
                                
                                 
                                 
                                  One way that comes to mind offhand would be 
                                 
 
                                  


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


 @groovy.transform.TupleConstructor(includes=[''])  
                                 
 
                                  
                                  
                                 
 
                                 Anybody know why 
                                
 
                                 


 @groovy.transform.TupleConstructor(includes=[])  
                                
 
                                doesn't work? 
                                 

   
                                
                                -Keegan
   
                               
                               
                                
                                 
                                
 
                                 
                                  On Mon, Jul 13, 2015 at 5:34 PM, Nikolay Totomanov 
                                   ntotomanov@abv.bg >  wrote: 
                                 
 
                                   
                                    
                                    
                                     Hello, 
                                     
                                     
                                    
 
                                     
                                    
                                     I need to always add the default constructor to my classes. 
                                     
                                    
                                     Is it possible to achieve that using CompilerConfiguration 
                                     
                                     
                                    
 
                                     
                                    
                                     Regards, 
                                     
                                    
                                     Nikki 
                                     
                                    
                                   
                                  
                                
 
                                 
                                
                               
                              
                             
                           
 
                            
                           
                          
                         
                        
                      
 
                       
                      
                     
                    
                   
                 
 
                  
                 
                
              
             
             
           
          
         

          
        
       
      
     
    

    
    
 

Re: Always add no-args constructor at compile time

Posted by Keegan Witt <ke...@gmail.com>.
FYI: I opened GROOVY-7522
<https://issues.apache.org/jira/browse/GROOVY-7522> and GROOVY-7523
<https://issues.apache.org/jira/browse/GROOVY-7523> which, if agreed to,
will make your config script a lot simpler.

-Keegan

On Fri, Jul 17, 2015 at 4:49 PM, Keegan Witt <ke...@gmail.com> wrote:

> Awesome!  Glad I could help.
>
> On Fri, Jul 17, 2015 at 12:48 PM, Nikolay Totomanov <nt...@abv.bg>
> wrote:
>
>> Thanks a lot, Keegan!
>> It works like a charm!
>>
>>
>>
>> >-------- Оригинално писмо --------
>> >От: Nikolay Totomanov ntotomanov@abv.bg
>> >Относно: Re: Always add no-args constructor at compile time
>> >До: users@groovy.incubator.apache.org
>> >Изпратено на: 15.07.2015 01:57
>>
>>  The code looks very clean to me!
>> Thanks a lot, Keegan!
>> I'll give it a try.
>>
>>
>>
>>
>> >-------- Оригинално писмо --------
>> >От: Keegan Witt keeganwitt@gmail.com
>> >Относно: Re: Always add no-args constructor at compile time
>> >До: users@groovy.incubator.apache.org
>> >Изпратено на: 14.07.2015 06:04
>>
>>  Oh, actually Opcodes is not needed, you can take that off so you don't
>> need a dependency on *org.ow2.asm:asm*.
>>
>>  On Mon, Jul 13, 2015 at 10:53 PM, Keegan Witt <ke...@gmail.com>
>> 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) {
>>     ast(DefaultConstructor)
>> }
>>
>>
>>  Seemed to work in my tests, but everybody should feel free to point out
>> anything I goofed up.
>>
>> -Keegan
>>
>>  On Mon, Jul 13, 2015 at 6:24 PM, Keegan Witt <ke...@gmail.com>
>> 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.
>>
>> -Keegan
>>
>>  On Mon, Jul 13, 2015 at 5:45 PM, Keegan Witt <ke...@gmail.com>
>> wrote:
>>
>>   One way that comes to mind offhand would be
>>     @groovy.transform.TupleConstructor(includeFields=false,
>> includeProperties=false, includeSuperFields=false,
>> includeSuperProperties=false)
>>
>>  or
>>     @groovy.transform.TupleConstructor(includes=[''])
>>
>> Anybody know why
>>     @groovy.transform.TupleConstructor(includes=[])
>> doesn't work?
>>
>>  -Keegan
>>
>>  On Mon, Jul 13, 2015 at 5:34 PM, Nikolay Totomanov <nt...@abv.bg>
>> wrote:
>>
>>  Hello,
>>
>>  I need to always add the default constructor to my classes.
>>  Is it possible to achieve that using CompilerConfiguration
>>
>>  Regards,
>>  Nikki
>>
>>
>>
>>
>>
>>
>

Re: Always add no-args constructor at compile time

Posted by Keegan Witt <ke...@gmail.com>.
Awesome!  Glad I could help.

On Fri, Jul 17, 2015 at 12:48 PM, Nikolay Totomanov <nt...@abv.bg>
wrote:

> Thanks a lot, Keegan!
> It works like a charm!
>
>
>
> >-------- Оригинално писмо --------
> >От: Nikolay Totomanov ntotomanov@abv.bg
> >Относно: Re: Always add no-args constructor at compile time
> >До: users@groovy.incubator.apache.org
> >Изпратено на: 15.07.2015 01:57
>
>  The code looks very clean to me!
> Thanks a lot, Keegan!
> I'll give it a try.
>
>
>
>
> >-------- Оригинално писмо --------
> >От: Keegan Witt keeganwitt@gmail.com
> >Относно: Re: Always add no-args constructor at compile time
> >До: users@groovy.incubator.apache.org
> >Изпратено на: 14.07.2015 06:04
>
>  Oh, actually Opcodes is not needed, you can take that off so you don't
> need a dependency on *org.ow2.asm:asm*.
>
>  On Mon, Jul 13, 2015 at 10:53 PM, Keegan Witt <ke...@gmail.com>
> 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) {
>     ast(DefaultConstructor)
> }
>
>
>  Seemed to work in my tests, but everybody should feel free to point out
> anything I goofed up.
>
> -Keegan
>
>  On Mon, Jul 13, 2015 at 6:24 PM, Keegan Witt <ke...@gmail.com>
> 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.
>
> -Keegan
>
>  On Mon, Jul 13, 2015 at 5:45 PM, Keegan Witt <ke...@gmail.com>
> wrote:
>
>   One way that comes to mind offhand would be
>     @groovy.transform.TupleConstructor(includeFields=false,
> includeProperties=false, includeSuperFields=false,
> includeSuperProperties=false)
>
>  or
>     @groovy.transform.TupleConstructor(includes=[''])
>
> Anybody know why
>     @groovy.transform.TupleConstructor(includes=[])
> doesn't work?
>
>  -Keegan
>
>  On Mon, Jul 13, 2015 at 5:34 PM, Nikolay Totomanov <nt...@abv.bg>
> wrote:
>
>  Hello,
>
>  I need to always add the default constructor to my classes.
>  Is it possible to achieve that using CompilerConfiguration
>
>  Regards,
>  Nikki
>
>
>
>
>
>

Re: Always add no-args constructor at compile time

Posted by Nikolay Totomanov <nt...@abv.bg>.
Thanks a lot,
Keegan!
  
It works like a charm!








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

 >От: Nikolay Totomanov ntotomanov@abv.bg

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

 >До: users@groovy.incubator.apache.org

 >Изпратено на: 15.07.2015 01:57



 
  
   
   The code looks very clean to me!
   Thanks a lot,
Keegan!
   
   I'll give it a try.
    
    

    

    

    
 >-------- Оригинално писмо -------- 
    
 >От: Keegan Witt keeganwitt@gmail.com 
    
 >Относно: Re: Always add no-args constructor at compile time 
    
 >До: users@groovy.incubator.apache.org 
    
 >Изпратено на: 14.07.2015 06:04 
    

    
 
     
      Oh, actually Opcodes is not needed, you can take that off so you don't need a dependency on 
      org.ow2.asm:asm . 
     
 
      
      
     
 
      
       On Mon, Jul 13, 2015 at 10:53 PM, Keegan Witt 
        keeganwitt@gmail.com >  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) {
    ast(DefaultConstructor)
}  
         
 
          
         
          Seemed to work in my tests, but everybody should feel free to point out anything I goofed up. 
           
   
          
           
            
           
 
           -Keegan
   
         
         
          
           
          
 
           
            On Mon, Jul 13, 2015 at 6:24 PM, Keegan Witt 
             keeganwitt@gmail.com >  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. 
                
   
               
                
                 
                
 
                -Keegan
   
              
              
               
                
               
 
                
                 On Mon, Jul 13, 2015 at 5:45 PM, Keegan Witt 
                  keeganwitt@gmail.com >  wrote: 
                
 
                  
                   
                    
                     
                     
                      One way that comes to mind offhand would be 
                     
 
                      


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


 @groovy.transform.TupleConstructor(includes=[''])  
                     
 
                      
                      
                     
 
                     Anybody know why 
                    
 
                     


 @groovy.transform.TupleConstructor(includes=[])  
                    
 
                    doesn't work? 
                     

   
                    
                    -Keegan
   
                   
                   
                    
                     
                    
 
                     
                      On Mon, Jul 13, 2015 at 5:34 PM, Nikolay Totomanov 
                       ntotomanov@abv.bg >  wrote: 
                     
 
                       
                        
                        
                         Hello, 
                         
                         
                        
 
                         
                        
                         I need to always add the default constructor to my classes. 
                         
                        
                         Is it possible to achieve that using CompilerConfiguration 
                         
                         
                        
 
                         
                        
                         Regards, 
                         
                        
                         Nikki 
                         
                        
                       
                      
                    
 
                     
                    
                   
                  
                 
               
 
                
               
              
             
            
          
 
           
          
         
        
       
     
 
      
    
    
 

Re: Always add no-args constructor at compile time

Posted by Nikolay Totomanov <nt...@abv.bg>.
 The code looks very clean to me! Thanks a lot,
Keegan! I'll give it a try. 








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

 >От: Keegan Witt keeganwitt@gmail.com

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

 >До: users@groovy.incubator.apache.org

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



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

   
   
   

    
    On Mon, Jul 13, 2015 at 10:53 PM, Keegan Witt 
      keeganwitt@gmail.com >  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) {
    ast(DefaultConstructor)
} 
       

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

         -Keegan
  
      
      
       
        
        

         
         On Mon, Jul 13, 2015 at 6:24 PM, Keegan Witt 
           keeganwitt@gmail.com >  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.
              
  
            
             
              
              

              -Keegan
  
           
           
            
             
             

              
              On Mon, Jul 13, 2015 at 5:45 PM, Keegan Witt 
                keeganwitt@gmail.com >  wrote:
              

               
                
                 
                  
                   
                   One way that comes to mind offhand would be 
                   

                    


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

                   

                   
                   
                   or
                   

                    


 @groovy.transform.TupleConstructor(includes=['']) 
                   

                   
                   
                   

                   Anybody know why 
                  

                   


 @groovy.transform.TupleConstructor(includes=[]) 
                  

                  doesn't work?
                   

  
                 
                  -Keegan
  
                
                
                 
                  
                  

                   
                   On Mon, Jul 13, 2015 at 5:34 PM, Nikolay Totomanov 
                     ntotomanov@abv.bg >  wrote:
                   

                    
                      
                      
                      Hello,
                      
                      
                      

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

                      
                      
                      Regards,
                      
                      
                      Nikki
                      
                      
                    
                   
                  

                   
                 
                
               
              
             

              
            
           
          
         
        

         
       
      
     
    
   

    
 
  

Re: Always add no-args constructor at compile time

Posted by Keegan Witt <ke...@gmail.com>.
Oh, actually Opcodes is not needed, you can take that off so you don't need
a dependency on *org.ow2.asm:asm*.

On Mon, Jul 13, 2015 at 10:53 PM, Keegan Witt <ke...@gmail.com> 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) {
>     ast(DefaultConstructor)
> }
>
>
> Seemed to work in my tests, but everybody should feel free to point out
> anything I goofed up.
>
> -Keegan
>
> On Mon, Jul 13, 2015 at 6:24 PM, Keegan Witt <ke...@gmail.com> 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.
>>
>> -Keegan
>>
>> On Mon, Jul 13, 2015 at 5:45 PM, Keegan Witt <ke...@gmail.com>
>> wrote:
>>
>>> One way that comes to mind offhand would be
>>>     @groovy.transform.TupleConstructor(includeFields=false,
>>> includeProperties=false, includeSuperFields=false,
>>> includeSuperProperties=false)
>>>
>>> or
>>>     @groovy.transform.TupleConstructor(includes=[''])
>>>
>>> Anybody know why
>>>     @groovy.transform.TupleConstructor(includes=[])
>>> doesn't work?
>>>
>>> -Keegan
>>>
>>> On Mon, Jul 13, 2015 at 5:34 PM, Nikolay Totomanov <nt...@abv.bg>
>>> wrote:
>>>
>>>> Hello,
>>>>
>>>> I need to always add the default constructor to my classes.
>>>> Is it possible to achieve that using CompilerConfiguration
>>>>
>>>> Regards,
>>>> Nikki
>>>>
>>>
>>>
>>
>

Re: Always add no-args constructor at compile time

Posted by Keegan Witt <ke...@gmail.com>.
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) {
    ast(DefaultConstructor)
}


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

-Keegan

On Mon, Jul 13, 2015 at 6:24 PM, Keegan Witt <ke...@gmail.com> 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.
>
> -Keegan
>
> On Mon, Jul 13, 2015 at 5:45 PM, Keegan Witt <ke...@gmail.com> wrote:
>
>> One way that comes to mind offhand would be
>>     @groovy.transform.TupleConstructor(includeFields=false,
>> includeProperties=false, includeSuperFields=false,
>> includeSuperProperties=false)
>>
>> or
>>     @groovy.transform.TupleConstructor(includes=[''])
>>
>> Anybody know why
>>     @groovy.transform.TupleConstructor(includes=[])
>> doesn't work?
>>
>> -Keegan
>>
>> On Mon, Jul 13, 2015 at 5:34 PM, Nikolay Totomanov <nt...@abv.bg>
>> wrote:
>>
>>> Hello,
>>>
>>> I need to always add the default constructor to my classes.
>>> Is it possible to achieve that using CompilerConfiguration
>>>
>>> Regards,
>>> Nikki
>>>
>>
>>
>

Re: Always add no-args constructor at compile time

Posted by Keegan Witt <ke...@gmail.com>.
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.

-Keegan

On Mon, Jul 13, 2015 at 5:45 PM, Keegan Witt <ke...@gmail.com> wrote:

> One way that comes to mind offhand would be
>     @groovy.transform.TupleConstructor(includeFields=false,
> includeProperties=false, includeSuperFields=false,
> includeSuperProperties=false)
>
> or
>     @groovy.transform.TupleConstructor(includes=[''])
>
> Anybody know why
>     @groovy.transform.TupleConstructor(includes=[])
> doesn't work?
>
> -Keegan
>
> On Mon, Jul 13, 2015 at 5:34 PM, Nikolay Totomanov <nt...@abv.bg>
> wrote:
>
>> Hello,
>>
>> I need to always add the default constructor to my classes.
>> Is it possible to achieve that using CompilerConfiguration
>>
>> Regards,
>> Nikki
>>
>
>

Re: Always add no-args constructor at compile time

Posted by Keegan Witt <ke...@gmail.com>.
One way that comes to mind offhand would be
    @groovy.transform.TupleConstructor(includeFields=false,
includeProperties=false, includeSuperFields=false,
includeSuperProperties=false)

or
    @groovy.transform.TupleConstructor(includes=[''])

Anybody know why
    @groovy.transform.TupleConstructor(includes=[])
doesn't work?

-Keegan

On Mon, Jul 13, 2015 at 5:34 PM, Nikolay Totomanov <nt...@abv.bg>
wrote:

> Hello,
>
> I need to always add the default constructor to my classes.
> Is it possible to achieve that using CompilerConfiguration
>
> Regards,
> Nikki
>