You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2015/11/14 03:08:54 UTC
[1/2] incubator-groovy git commit: GROOVY-7672: TupleConstructor
should have only SOURCE retention policy (closes #187)
Repository: incubator-groovy
Updated Branches:
refs/heads/master d8b0ff4c4 -> e3e9d41a2
GROOVY-7672: TupleConstructor should have only SOURCE retention policy (closes #187)
Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/60194df5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/60194df5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/60194df5
Branch: refs/heads/master
Commit: 60194df5d3a34c7db55df68f2dc6b8c9a8a487a3
Parents: d8b0ff4
Author: paulk <pa...@asert.com.au>
Authored: Thu Nov 12 14:18:48 2015 +1000
Committer: paulk <pa...@asert.com.au>
Committed: Sat Nov 14 12:08:15 2015 +1000
----------------------------------------------------------------------
src/main/groovy/transform/TupleConstructor.java | 3 +-
.../TupleConstructorTransformTest.groovy | 31 ++++++++++++++++++--
2 files changed, 30 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/60194df5/src/main/groovy/transform/TupleConstructor.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/transform/TupleConstructor.java b/src/main/groovy/transform/TupleConstructor.java
index 6b239b5..d95e816 100644
--- a/src/main/groovy/transform/TupleConstructor.java
+++ b/src/main/groovy/transform/TupleConstructor.java
@@ -71,11 +71,10 @@ import java.lang.annotation.Target;
* has type {@code LinkedHashMap} or if there is a single Map, AbstractMap or HashMap property (or field)</li>
* </ul>
*
- * @author Paul King
* @since 1.8.0
*/
@java.lang.annotation.Documented
-@Retention(RetentionPolicy.RUNTIME)
+@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.TYPE})
@GroovyASTTransformationClass("org.codehaus.groovy.transform.TupleConstructorASTTransformation")
public @interface TupleConstructor {
http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/60194df5/src/test/org/codehaus/groovy/transform/TupleConstructorTransformTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/transform/TupleConstructorTransformTest.groovy b/src/test/org/codehaus/groovy/transform/TupleConstructorTransformTest.groovy
index e57ceee..68284e4 100644
--- a/src/test/org/codehaus/groovy/transform/TupleConstructorTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/TupleConstructorTransformTest.groovy
@@ -123,7 +123,7 @@ class TupleConstructorTransformTest extends GroovyShellTestCase {
@ToString(includeSuperProperties=true, ignoreNulls=true, includeNames=true)
@TupleConstructor(force=true, defaults=false)
- //@TupleConstructor(force=true, defaults=false, includeSuperProperties=true)
+ @TupleConstructor(force=true, defaults=false, includeSuperProperties=true)
@InheritConstructors
class Cat extends NameId {
Double age
@@ -131,10 +131,37 @@ class TupleConstructorTransformTest extends GroovyShellTestCase {
assert new Cat("Felix").toString() == 'Cat(name:Felix)'
assert new Cat("Felix", 42).toString() == 'Cat(name:Felix, id:42)'
- //assert new Cat("Felix", 42, 3.5d).toString() == 'Cat(age:3.5, name:Felix, id:42)'
+ assert new Cat("Felix", 42, 3.5d).toString() == 'Cat(age:3.5, name:Felix, id:42)'
assert new Cat(3.5d).toString() == 'Cat(age:3.5)'
assert new Cat().toString() == 'Cat()'
'''
}
+ void testMultipleUsages_groovy7672() {
+ assertScript '''
+ import groovy.transform.*
+ import java.awt.Color
+
+ class Named {
+ String name
+ }
+
+ @ToString(includeSuperProperties=true, ignoreNulls=true, includeNames=true, includeFields=true)
+ @TupleConstructor(force=true, defaults=false)
+ @TupleConstructor(force=true, defaults=false, includeFields=true)
+ @TupleConstructor(force=true, defaults=false, includeSuperProperties=true)
+ class Cat extends Named {
+ int age
+ private Color color
+ Cat() {}
+ }
+
+ assert new Cat("Felix", 3).toString() == 'Cat(age:3, name:Felix)'
+ assert new Cat(3, Color.BLACK).toString() == 'Cat(age:3, color:java.awt.Color[r=0,g=0,b=0])'
+ assert new Cat(3).toString() == 'Cat(age:3)'
+ assert new Cat().toString() == 'Cat(age:0)'
+ assert Cat.constructors.size() == 4
+ '''
+ }
+
}
\ No newline at end of file
[2/2] incubator-groovy git commit: GROOVY-7672: TupleConstructor
should have only SOURCE retention policy (additional doco)
Posted by pa...@apache.org.
GROOVY-7672: TupleConstructor should have only SOURCE retention policy (additional doco)
Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/e3e9d41a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/e3e9d41a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/e3e9d41a
Branch: refs/heads/master
Commit: e3e9d41a277a76c12bff8ad51dbb6fb228ff5302
Parents: 60194df
Author: paulk <pa...@asert.com.au>
Authored: Fri Nov 13 21:05:12 2015 +1000
Committer: paulk <pa...@asert.com.au>
Committed: Sat Nov 14 12:08:16 2015 +1000
----------------------------------------------------------------------
src/spec/doc/core-metaprogramming.adoc | 16 +++++++
.../test/CodeGenerationASTTransformsTest.groovy | 45 ++++++++++++++++++++
2 files changed, 61 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/e3e9d41a/src/spec/doc/core-metaprogramming.adoc
----------------------------------------------------------------------
diff --git a/src/spec/doc/core-metaprogramming.adoc b/src/spec/doc/core-metaprogramming.adoc
index 87733dc..0b7a853 100644
--- a/src/spec/doc/core-metaprogramming.adoc
+++ b/src/spec/doc/core-metaprogramming.adoc
@@ -966,6 +966,22 @@ include::{projectdir}/src/spec/test/CodeGenerationASTTransformsTest.groovy[tags=
----
|=======================================================================
+Setting the `defaults` annotation attribute to `false` and the `force` annotation attribute to `true` allows
+multiple tuple constructors to be created by using different customization options for the different cases
+(provided each case has a different type signature) as shown in the following example:
+
+[source,groovy]
+----
+include::{projectdir}/src/spec/test/CodeGenerationASTTransformsTest.groovy[tags=tupleconstructor_example_defaults_multiple,indent=0]
+----
+
+Similarly, here is another example using different options for `includes`:
+
+[source,groovy]
+----
+include::{projectdir}/src/spec/test/CodeGenerationASTTransformsTest.groovy[tags=tupleconstructor_example_defaults_multipleIncludes,indent=0]
+----
+
[[xform-MapConstructor]]
===== @groovy.transform.MapConstructor
http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/e3e9d41a/src/spec/test/CodeGenerationASTTransformsTest.groovy
----------------------------------------------------------------------
diff --git a/src/spec/test/CodeGenerationASTTransformsTest.groovy b/src/spec/test/CodeGenerationASTTransformsTest.groovy
index 381111b..8bdbd75 100644
--- a/src/spec/test/CodeGenerationASTTransformsTest.groovy
+++ b/src/spec/test/CodeGenerationASTTransformsTest.groovy
@@ -579,6 +579,51 @@ assert new Musician('Jimi', 'Guitar', 1942).toString() == 'Musician(Jimi, Guitar
assert Musician.constructors.size() == 1
// end::tupleconstructor_example_defaults_false[]
'''
+
+ assertScript '''
+import groovy.transform.*
+// tag::tupleconstructor_example_defaults_multiple[]
+class Named {
+ String name
+}
+
+@ToString(includeSuperProperties=true, ignoreNulls=true, includeNames=true, includeFields=true)
+@TupleConstructor(force=true, defaults=false)
+@TupleConstructor(force=true, defaults=false, includeFields=true)
+@TupleConstructor(force=true, defaults=false, includeSuperProperties=true)
+class Book extends Named {
+ Integer published
+ private Boolean fiction
+ Book() {}
+}
+
+assert new Book("Regina", 2015).toString() == 'Book(published:2015, name:Regina)'
+assert new Book(2015, false).toString() == 'Book(published:2015, fiction:false)'
+assert new Book(2015).toString() == 'Book(published:2015)'
+assert new Book().toString() == 'Book()'
+assert Book.constructors.size() == 4
+// end::tupleconstructor_example_defaults_multiple[]
+'''
+
+ assertScript '''
+import groovy.transform.*
+// tag::tupleconstructor_example_defaults_multipleIncludes[]
+@ToString(includeSuperProperties=true, ignoreNulls=true, includeNames=true, includeFields=true)
+@TupleConstructor(force=true, defaults=false, includes='name,year')
+@TupleConstructor(force=true, defaults=false, includes='year,fiction')
+@TupleConstructor(force=true, defaults=false, includes='name,fiction')
+class Book {
+ String name
+ Integer year
+ Boolean fiction
+}
+
+assert new Book("Regina", 2015).toString() == 'Book(name:Regina, year:2015)'
+assert new Book(2015, false).toString() == 'Book(year:2015, fiction:false)'
+assert new Book("Regina", false).toString() == 'Book(name:Regina, fiction:false)'
+assert Book.constructors.size() == 3
+// end::tupleconstructor_example_defaults_multipleIncludes[]
+'''
}
void testMapConstructor() {