From users-return-460-apmail-groovy-users-archive=groovy.apache.org@groovy.incubator.apache.org Tue Jul 14 22:57:51 2015 Return-Path: X-Original-To: apmail-groovy-users-archive@minotaur.apache.org Delivered-To: apmail-groovy-users-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7734318BF4 for ; Tue, 14 Jul 2015 22:57:51 +0000 (UTC) Received: (qmail 92073 invoked by uid 500); 14 Jul 2015 22:57:51 -0000 Delivered-To: apmail-groovy-users-archive@groovy.apache.org Received: (qmail 92049 invoked by uid 500); 14 Jul 2015 22:57:51 -0000 Mailing-List: contact users-help@groovy.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@groovy.incubator.apache.org Delivered-To: mailing list users@groovy.incubator.apache.org Received: (qmail 92034 invoked by uid 99); 14 Jul 2015 22:57:51 -0000 Received: from Unknown (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 14 Jul 2015 22:57:51 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 8B9581801DB for ; Tue, 14 Jul 2015 22:57:50 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 3.89 X-Spam-Level: *** X-Spam-Status: No, score=3.89 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_REPLY=1, HTML_MESSAGE=3, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01, URIBL_BLOCKED=0.001] autolearn=disabled Authentication-Results: spamd3-us-west.apache.org (amavisd-new); domainkeys=pass (1024-bit key) header.from=ntotomanov@abv.bg header.d=abv.bg; dkim=pass (1024-bit key) header.d=abv.bg Received: from mx1-us-east.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id qX4hTdN7QkFQ for ; Tue, 14 Jul 2015 22:57:42 +0000 (UTC) Received: from smtp-out.abv.bg (smtp-out.abv.bg [194.153.145.80]) by mx1-us-east.apache.org (ASF Mail Server at mx1-us-east.apache.org) with ESMTP id 3C23743E1F for ; Tue, 14 Jul 2015 22:57:42 +0000 (UTC) Received: from nm61.abv.bg (nm3.ni.bg [192.168.151.92]) by smtp-out.abv.bg (Postfix) with ESMTP id 3921956EFDB for ; Wed, 15 Jul 2015 01:57:35 +0300 (EEST) DomainKey-Signature: a=rsa-sha1; s=smtp-out; d=abv.bg; c=simple; q=dns; b=WMcXRhYXuvf/qadPt6AcmJQiK5kIi6pNfQX5iJPccmcUow+05w4dmHV9hlFalI3I3 eN3jteaNE5NDkIZV8xUaDqUvUKoojY/vGLG28iEnU6EHQl5vp+5Wd3r3qrN9PtMtRpF t8iOxwpTIrXz68g98s02/tMakyWSYdK1Tpzss5w= DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=abv.bg; s=smtp-out; t=1436914655; bh=bgisYEOvOV2qWL89rHUWYbTm5CX7ltv/1U94XeW8m4E=; h=Date:From:To:Message-ID:In-Reply-To:References:Subject: MIME-Version:Content-Type:DKIM; b=lwR+N1D4rTP4hTBsmGMgGx9Yevxd6zmOyfxfdDtfSx3cDOlZiNzD9/k+2/QLElhN+ lisqAV6ct2DQvWX/D2Sv9+N73ed0HouqnPOGGVFRjLP4zdEltz/K8ToT+r9oDGQSgS zmE2MtoohaoDYw1s6wb0pKbbZkDZecY3SQ1XNaxo= Received: from nm3.abv.bg (localhost [127.0.0.1]) by nm61.abv.bg (Postfix) with ESMTP id 201E31FC59 for ; Wed, 15 Jul 2015 01:57:35 +0300 (EEST) Date: Wed, 15 Jul 2015 01:57:35 +0300 (EEST) From: Nikolay Totomanov To: users@groovy.incubator.apache.org Message-ID: <822319429.1828834.1436914655124.JavaMail.apache@nm3.abv.bg> In-Reply-To: References: <1201237332.1898371.1436823274496.JavaMail.apache@nm2.abv.bg> Subject: Re: Always add no-args constructor at compile time MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_1828833_799525644.1436914655120" X-Mailer: AbvMail 3.0 X-Originating-IP: 188.126.6.222 ------=_Part_1828833_799525644.1436914655120 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit 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 ------=_Part_1828833_799525644.1436914655120 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable
The code looks very clean to me!
Thanks a lot, Keegan!
I= 'll give it a try.




>-------- =D0=9E=D1=80=D0=B8=D0=B3=D0=B8=D0=BD=D0=B0=D0=BB=D0=BD=D0=BE = =D0=BF=D0=B8=D1=81=D0=BC=D0=BE --------
>=D0=9E=D1=82: Keegan Witt keeganwitt@gmail.com
>=D0=9E=D1=82=D0=BD=D0=BE=D1=81=D0=BD=D0=BE: Re: Always add no-args= constructor at compile time
>=D0=94=D0=BE: users@groovy.incubator.apache.org
>=D0=98=D0=B7=D0=BF=D1=80=D0=B0=D1=82=D0=B5=D0=BD=D0=BE =D0=BD=D0= =B0: 14.07.2015 06:04

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

On Mon, Jul 13, 2015 at 10:53 PM, Keegan Witt=20 <
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.Cl= assNode
import org.codehaus= .groovy.ast.ConstructorNode
import= org.codehaus.groovy.ast.Parameter
import org.codehaus.groovy.ast.stmt.BlockStatement
import org.codehaus.groovy.control.C= ompilePhase
import org.code= haus.groovy.control.SourceUnit
imp= ort org.codehaus.groovy.transform.AbstractASTTransformation
import org.codehaus.groovy.transform.GroovyASTTransformation
<= span style=3D"color:rgb(204,120,50)">import org.codehaus.groovy.transform.GroovyASTTransformationClassimport org.objectweb.a= sm.Opcodes

@GroovyASTTransform= ation(phase =3D CompilePhase.CANONICALIZATION)
public class DefaultConstructorASTTransformation= extends AbstractASTTransforma= tion implements Opcodes {
= public void visit(ASTNode[]= nodes, SourceUnit source) {
init(nodes, source)
Anno= tatedNode parent =3D (AnnotatedNode) nodes[1]

if (parent instanceof ClassN= ode) {
ClassNode cNode =3D (ClassNode) parent
= if (!cNode.getDeclaredConstru= ctor(new Parameter[0])) {
final BlockStatement body =3D new BlockStatement()
cNod= e.addConstructor(new Construct= orNode(ACC_PUBLIC, new Paramet= er[0], cNode.EMPTY_ARRAY, bod= y))
}
}
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@GroovyASTTransformationClass("DefaultConstructorASTTransformation")
public
@interface DefaultC= onstructor {}

withConfig(configuration) {
ast(DefaultC= onstructor)
}

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=20 <keeganwitt@gmail.com> wrote:
Sorry, I should have given you the example in the configuration= script syntax

withConfig(configu=
ration) {
ast(groovy.transform.TupleConstructor, includes:[= ''])
}

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

-Keegan

On Mon, Jul 13, 2015 at 5:45 PM, Keegan Witt=20 <keeganwitt@gmail.com> wrote:
One way that comes to mind offhand would be=20
 &n= bsp;  @groovy.transform.TupleConstructor(includeFields=3Dfalse, includ= eProperties=3Dfalse, includeSuperFields=3Dfalse, includeSuperProperties=3Df= alse)

or
 &n= bsp;  @groovy.transform.TupleConstructor(includes=3D[''])

Anybody know why=20
 &nb= sp;  @groovy.transform.TupleConstructor(includes=3D[])
doesn't work?

-Keegan

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

I need to always add the default constructor to my cl= asses.
Is it possible to achieve that using CompilerConfigur= ation

Regards,
Nikki
=20

=20

=20

=20

=20 =20
------=_Part_1828833_799525644.1436914655120--