Repository: groovy
Updated Branches:
  refs/heads/master 9d2b7fd21 -> 31491e74e


GROOVY-8014: @ToString could output properties in a predefined order when 
'includes' is used


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

Branch: refs/heads/master
Commit: 31491e74e32478a859236d9f7e5b475a90fc74b7
Parents: 9d2b7fd
Author: paulk <pa...@asert.com.au>
Authored: Thu Dec 1 18:18:27 2016 -0500
Committer: paulk <pa...@asert.com.au>
Committed: Thu Dec 1 18:18:27 2016 -0500

----------------------------------------------------------------------
 .../transform/ToStringASTTransformation.java    | 13 +++++++++++-
 .../transform/ToStringTransformTest.groovy      | 21 ++++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/31491e74/src/main/org/codehaus/groovy/transform/ToStringASTTransformation.java
----------------------------------------------------------------------
diff --git 
a/src/main/org/codehaus/groovy/transform/ToStringASTTransformation.java 
b/src/main/org/codehaus/groovy/transform/ToStringASTTransformation.java
index 3cba4a5..01ece7f 100644
--- a/src/main/org/codehaus/groovy/transform/ToStringASTTransformation.java
+++ b/src/main/org/codehaus/groovy/transform/ToStringASTTransformation.java
@@ -42,6 +42,8 @@ import org.codehaus.groovy.runtime.InvokerHelper;
 import org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport;
 
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 
 import static org.codehaus.groovy.ast.ClassHelper.make;
@@ -153,7 +155,7 @@ public class ToStringASTTransformation extends 
AbstractASTTransformation {
         boolean canBeSelf;
     }
 
-    private static Expression calculateToStringStatements(ClassNode cNode, 
boolean includeSuper, boolean includeFields, List<String> excludes, 
List<String> includes, boolean includeNames, boolean ignoreNulls, boolean 
includePackage, boolean includeSuperProperties, boolean allProperties, 
BlockStatement body, boolean allNames) {
+    private static Expression calculateToStringStatements(ClassNode cNode, 
boolean includeSuper, boolean includeFields, List<String> excludes, final 
List<String> includes, boolean includeNames, boolean ignoreNulls, boolean 
includePackage, boolean includeSuperProperties, boolean allProperties, 
BlockStatement body, boolean allNames) {
         // def _result = new StringBuilder()
         final Expression result = varX("_result");
         body.addStatement(declS(result, ctorX(STRINGBUILDER_TYPE)));
@@ -191,6 +193,15 @@ public class ToStringASTTransformation extends 
AbstractASTTransformation {
             elements.add(new ToStringElement(callSuperX("toString"), "super", 
false));
         }
 
+        if (includes != null) {
+            Comparator<ToStringElement> includeComparator = new 
Comparator<ToStringElement>() {
+                public int compare(ToStringElement tse1, ToStringElement tse2) 
{
+                    return new 
Integer(includes.indexOf(tse1.name)).compareTo(includes.indexOf(tse2.name));
+                }
+            };
+            Collections.sort(elements, includeComparator);
+        }
+
         for (ToStringElement el : elements) {
             appendValue(body, result, first, el.value, el.name, includeNames, 
ignoreNulls, el.canBeSelf);
         }

http://git-wip-us.apache.org/repos/asf/groovy/blob/31491e74/src/test/org/codehaus/groovy/transform/ToStringTransformTest.groovy
----------------------------------------------------------------------
diff --git 
a/src/test/org/codehaus/groovy/transform/ToStringTransformTest.groovy 
b/src/test/org/codehaus/groovy/transform/ToStringTransformTest.groovy
index 18f5d10..200ef14 100644
--- a/src/test/org/codehaus/groovy/transform/ToStringTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/ToStringTransformTest.groovy
@@ -469,4 +469,25 @@ class ToStringTransformTest extends GroovyShellTestCase {
         assert toString.contains('super:Foo(baz)')
     }
 
+    void testIncludesOrdering_Groovy8014() {
+        assertScript """
+            import groovy.transform.*
+
+            @ToString
+            class Foo {
+                String baz = 'baz'
+            }
+
+            @ToString(includes='a,c,super,b,d', includeFields=true, 
includeSuper=true)
+            class Bar extends Foo {
+                int a = 1
+                int b = 2
+                private int c = 3
+                public int d = 4
+            }
+
+            assert new Bar().toString() == 'Bar(1, 3, Foo(baz), 2, 4)'
+        """
+    }
+
 }

Reply via email to