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/12 01:05:58 UTC

incubator-groovy git commit: GROOVY-7524: Document that mixing TupleConstructor and InheritConstructors will likely not give you what you want (tweak wording)

Repository: incubator-groovy
Updated Branches:
  refs/heads/master 3b85e8610 -> 06d7ec99d


GROOVY-7524: Document that mixing TupleConstructor and InheritConstructors will likely not give you what you want (tweak wording)


Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/06d7ec99
Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/06d7ec99
Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/06d7ec99

Branch: refs/heads/master
Commit: 06d7ec99dc2c95f270701f718491973a52fa79e0
Parents: 3b85e86
Author: Paul King <pa...@asert.com.au>
Authored: Thu Nov 12 10:05:35 2015 +1000
Committer: Paul King <pa...@asert.com.au>
Committed: Thu Nov 12 10:05:35 2015 +1000

----------------------------------------------------------------------
 .../groovy/transform/InheritConstructors.java   | 15 ++++++++---
 src/main/groovy/transform/TupleConstructor.java | 14 +++++++++--
 .../TupleConstructorTransformTest.groovy        | 26 ++++++++++++++++++++
 3 files changed, 49 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/06d7ec99/src/main/groovy/transform/InheritConstructors.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/transform/InheritConstructors.java b/src/main/groovy/transform/InheritConstructors.java
index cd22173..fcac101 100644
--- a/src/main/groovy/transform/InheritConstructors.java
+++ b/src/main/groovy/transform/InheritConstructors.java
@@ -27,7 +27,8 @@ import java.lang.annotation.Target;
 
 /**
  * Class annotation to make constructors from a super class available in a sub class.
- * Do not use with {@link TupleConstructor}.
+ * Should be used with care with other annotations which create constructors - see "Known
+ * Limitations" for more details.
  * <p>
  * {@code @InheritConstructors} saves you typing some boilerplate code.
  * <p>
@@ -97,13 +98,19 @@ import java.lang.annotation.Target;
  *     }
  * }
  * </pre>
- *
- * <em>Advanced note:</em>If you create Groovy constructors with optional
+ * Known Limitations:
+ * <ul>
+ * <li>This AST transform creates (potentially) numerous constructors.
+ * You should take care to avoid constructors with duplicate signatures if you are defining your own constructors or
+ * combining with other AST transforms which create constructors (e.g. {@code @TupleConstructor});
+ * the order in which the particular transforms are processed becomes important in that case.</li>
+ * <li>If you create Groovy constructors with optional
  * arguments this leads to multiple constructors created in the byte code.
  * The expansion to multiple constructors occurs in a later phase to
  * this AST transformation. This means that you can't override (i.e. not
  * inherit) the constructors with signatures that Groovy adds later.
- * If you get it wrong you will get a compile-time error about the duplication.
+ * If you get it wrong you will get a compile-time error about the duplication.</li>
+ * </ul>
  *
  * @author Paul King
  * @since 1.7.3

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/06d7ec99/src/main/groovy/transform/TupleConstructor.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/transform/TupleConstructor.java b/src/main/groovy/transform/TupleConstructor.java
index 7090a86..6b239b5 100644
--- a/src/main/groovy/transform/TupleConstructor.java
+++ b/src/main/groovy/transform/TupleConstructor.java
@@ -27,7 +27,8 @@ import java.lang.annotation.Target;
 
 /**
  * Class annotation used to assist in the creation of tuple constructors in classes.
- * Do not use with {@link InheritConstructors}.
+ * Should be used with care with other annotations which create constructors - see "Known
+ * Limitations" for more details.
  * <p>
  * It allows you to write classes in this shortened form:
  * <pre>
@@ -55,8 +56,17 @@ import java.lang.annotation.Target;
  * by the fields of the class (if {@code includeFields} is set). Within each grouping the order
  * is as attributes appear within the respective class.
  * <p>
- * Limitations:
+ * Known Limitations:
  * <ul>
+ * <li>This AST transform might become a no-op if you are defining your own constructors or
+ * combining with other AST transforms which create constructors (e.g. {@code @InheritConstructors});
+ * the order in which the particular transforms are processed becomes important in that case.
+ * See the {@code force} attribute for further details about customizing this behavior.</li>
+ * <li>This AST transform normally uses default parameter values which creates multiple constructors under
+ * the covers. You should use with care if you are defining your own constructors or
+ * combining with other AST transforms which create constructors (e.g. {@code @InheritConstructors});
+ * the order in which the particular transforms are processed becomes important in that case.
+ * See the {@code defaults} attribute for further details about customizing this behavior.</li>
  * <li>Groovy's normal map-style naming conventions will not be available if the first property (or field)
  * has type {@code LinkedHashMap} or if there is a single Map, AbstractMap or HashMap property (or field)</li>
  * </ul>

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/06d7ec99/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 d53ab66..e57ceee 100644
--- a/src/test/org/codehaus/groovy/transform/TupleConstructorTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/TupleConstructorTransformTest.groovy
@@ -111,4 +111,30 @@ class TupleConstructorTransformTest extends GroovyShellTestCase {
         '''
     }
 
+    void testCombiningWithInheritConstructors_groovy7524() {
+        assertScript '''
+            import groovy.transform.*
+
+            @TupleConstructor
+            class NameId {
+                String name
+                Integer id
+            }
+
+            @ToString(includeSuperProperties=true, ignoreNulls=true, includeNames=true)
+            @TupleConstructor(force=true, defaults=false)
+            //@TupleConstructor(force=true, defaults=false, includeSuperProperties=true)
+            @InheritConstructors
+            class Cat extends NameId {
+                Double age
+            }
+
+            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(3.5d).toString() == 'Cat(age:3.5)'
+            assert new Cat().toString() == 'Cat()'
+        '''
+    }
+
 }
\ No newline at end of file