Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

GROOVY-4654: groovy.transform.ToString annotation does not handle cyc…

…les (handle easy case of self-reference)
  • Loading branch information...
commit d9d4feb923443ef90f03887cdca79a5696bc8181 1 parent 5cbe57d
Paul King paulk-asert authored
2  src/main/org/codehaus/groovy/transform/AbstractASTTransformUtil.java
@@ -210,7 +210,7 @@ private static BooleanExpression notEqualsPropertyExpr(PropertyNode pNode, Expre
210 210 return new BooleanExpression(new BinaryExpression(selfGetter, COMPARE_NOT_EQUAL, otherGetter));
211 211 }
212 212
213   - private static BooleanExpression identicalExpr(Expression self, Expression other) {
  213 + public static BooleanExpression identicalExpr(Expression self, Expression other) {
214 214 return new BooleanExpression(new MethodCallExpression(self, "is", new ArgumentListExpression(other)));
215 215 }
216 216
11 src/main/org/codehaus/groovy/transform/ToStringASTTransformation.java
@@ -40,12 +40,7 @@
40 40 import java.util.ArrayList;
41 41 import java.util.List;
42 42
43   -import static org.codehaus.groovy.transform.AbstractASTTransformUtil.assignStatement;
44   -import static org.codehaus.groovy.transform.AbstractASTTransformUtil.declStatement;
45   -import static org.codehaus.groovy.transform.AbstractASTTransformUtil.getInstanceNonPropertyFields;
46   -import static org.codehaus.groovy.transform.AbstractASTTransformUtil.getInstanceProperties;
47   -import static org.codehaus.groovy.transform.AbstractASTTransformUtil.hasDeclaredMethod;
48   -import static org.codehaus.groovy.transform.AbstractASTTransformUtil.notNullExpr;
  43 +import static org.codehaus.groovy.transform.AbstractASTTransformUtil.*;
49 44
50 45 /**
51 46 * Handles generation of code for the @ToString annotation.
@@ -157,7 +152,9 @@ private static void appendValue(BlockStatement body, Expression result, Variable
157 152 final Statement appendValue = ignoreNulls ? new IfStatement(notNullExpr(value), thenBlock, EmptyStatement.INSTANCE) : thenBlock;
158 153 appendCommaIfNotFirst(thenBlock, result, first);
159 154 appendPrefix(thenBlock, result, name, includeNames);
160   - thenBlock.addStatement(append(result, new StaticMethodCallExpression(INVOKER_TYPE, "toString", value)));
  155 + thenBlock.addStatement(new IfStatement(identicalExpr(value, VariableExpression.THIS_EXPRESSION),
  156 + append(result, new ConstantExpression("(this)")),
  157 + append(result, new StaticMethodCallExpression(INVOKER_TYPE, "toString", value))));
161 158 body.addStatement(appendValue);
162 159 }
163 160
20 src/test/org/codehaus/groovy/transform/ToStringTransformTest.groovy
@@ -220,4 +220,24 @@ class ToStringTransformTest extends GroovyShellTestCase {
220 220
221 221 assertEquals("Person()", toString)
222 222 }
  223 +
  224 + void testSelfReference() {
  225 +
  226 + def toString = evaluate("""
  227 + import groovy.transform.*
  228 +
  229 + @ToString(includeFields=true, includeNames=true) class Tree {
  230 + String val
  231 + Tree left
  232 + private Tree right
  233 + }
  234 +
  235 + def self = new Tree(val:'foo', left:null, right:null)
  236 + self.left = self
  237 + self.right = self
  238 + self.toString()
  239 + """)
  240 +
  241 + assert toString == 'Tree(val:foo, left:(this), right:(this))'
  242 + }
223 243 }

0 comments on commit d9d4feb

Please sign in to comment.
Something went wrong with that request. Please try again.