[ 
https://issues.apache.org/jira/browse/GROOVY-3250?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149814#comment-18149814
 ] 

ASF GitHub Bot commented on GROOVY-3250:
----------------------------------------

daniellansun commented on PR #3330:
URL: https://github.com/apache/groovy/pull/3330#issuecomment-7102396027

   Thanks Jochen. You are right: once we detect cycles deeper than a direct 
self-reference, `(this Collection)` is unclear. In your example the last marker 
in `[3, (this Collection)]` looks like `nestedList3` contains itself, but it 
actually points at `nestedList4`.
   
   I would like to keep `(this Collection)`, `(this Map)`, and `(this array)` 
only for a cycle back to the container that is currently being printed, which 
is the same case Java's `AbstractCollection.toString()` marks, and the same 
meaning these markers had before. When the cycle points at a container further 
up the render path, the marker should say so: `(ancestor Collection)`, 
`(ancestor Map)`, or `(ancestor array)`.
   
   Your example would then look like this:
   
   ```groovy
   assert nestedList4.toString() == '[[1, [(ancestor Collection)]], [2, 
[(ancestor Collection)]], [3]]'
   nestedList3 << nestedList4
   assert nestedList4.toString() == '[[1, [(ancestor Collection)]], [2, 
[(ancestor Collection)]], [3, (ancestor Collection)]]'
   ```
   
   A direct self-reference stays as it is today, for example `[1, (this 
Collection)]` for `list << list`. I have updated the tests, including this 
case, and pushed the change to this PR.
   




> StackOverflowError in List and Map formatting
> ---------------------------------------------
>
>                 Key: GROOVY-3250
>                 URL: https://issues.apache.org/jira/browse/GROOVY-3250
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-jdk
>    Affects Versions: 1.5.7
>         Environment: Groovy 1.5.7 and 1.6-RC1
>            Reporter: Alexander Veit
>            Assignee: Daniel Sun
>            Priority: Major
>              Labels: StackOverflowError
>
> If a List or Map that contains itself is formatted for output (print, 
> inspect,...) a StackOverflowError occurs.
> {code}
> import org.codehaus.groovy.runtime.InvokerHelper
> def x
> // List (simple case)
> x = []
> x << x
> assert "[(this Collection)]" == x.toString()
> InvokerHelper.toListString(x)
> // List (nested case)
> x = []
> x << [x]
> assert InvokerHelper.toListString(x)
> // Map (simple case)
> x = [:]
> x.put("self", x)
> assert "[self:this Map_]" == InvokerHelper.toMapString(x) // JDK uses "(this 
> Map)"
> // Map (nested case)
> x = [:]
> x.put("nested", ["self":x])
> assert InvokerHelper.toMapString(x)
> // there are also List/Map mixed cases
> // ...
> {code}
> See also GROOVY-1106.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to