groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Keegan Witt <keeganw...@gmail.com>
Subject Re: Always add no-args constructor at compile time
Date Tue, 14 Jul 2015 02:53:29 GMT
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