groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nikolay Totomanov <ntotoma...@abv.bg>
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 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
                      
                      
                    
                   
                  

                   
                 
                
               
              
             

              
            
           
          
         
        

         
       
      
     
    
   

    
 
  
Mime
View raw message