jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • groovy
  • GROOVY-2458

Differences between StringBuffer.append und String +

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

The Script

class Test {
public String toString() { super.toString() + properties }
}
println new Test()

produces:
Test@2a5330["metaClass":groovy.lang.MetaClassImpl@1b383e9[class Test], "class":class Test]

but the Script

class Test {
public String toString() { new StringBuffer(super.toString()).append(properties) }
}
println new Test()

produces:
Test@13c1b02{metaClass=groovy.lang.MetaClassImpl@91cee[class Test], class=class Test}

Is that a bug? I would think this should produce the same result. If the first class would be compile it should generate something like the second class. So this could also be a difference between scripts and compiled code? (I will try this if I got the time.)

Issue Links

relates to

Bug - A problem which impairs or prevents the functions of the product. GROOVY-2331 Println behavior for collections, strings and gstrings [minor breaking change]

  • Major - Major loss of function.
  • Closed - The issue is considered finished, the resolution is correct. Issues which are not closed can be reopened.

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
Hide
Permalink
Jörg Gottschling added a comment - 30/Dec/07 11:49 AM

Hm,... so other versions of the script:

class Test {
public String toString() { super.toString() + properties.toString() }
}
println new Test()

and

class Test {
public String toString() { super.toString() << properties }
}
println new Test()

produce the second result again:
Test@13c1b02{metaClass=groovy.lang.MetaClassImpl@91cee[class Test], class=class Test}

So, is something wrong with the + operator for Strings in scripts?

PS: I used Groovy 1.5.0

Show
Jörg Gottschling added a comment - 30/Dec/07 11:49 AM Hm,... so other versions of the script: class Test { public String toString() { super.toString() + properties.toString() } } println new Test() and class Test { public String toString() { super.toString() << properties } } println new Test() produce the second result again: Test@13c1b02{metaClass=groovy.lang.MetaClassImpl@91cee[class Test], class=class Test} So, is something wrong with the + operator for Strings in scripts? PS: I used Groovy 1.5.0
Hide
Permalink
Paul King added a comment - 30/Dec/07 11:19 PM

Just summarising, this code:

class Test1 {
  public String toString() { super.toString() + properties.toString() }
}
println new Test1()

class Test2 {
  public String toString() { super.toString() << properties }
}
println new Test2()

class Test3 {
  public String toString() { super.toString() + properties }
}
println new Test3()

class Test4 {
  public String toString() { new StringBuffer(super.toString()).append(properties) }
}
println new Test4()

class Test5 {
    public String toString() { "${super.toString()}${properties}" }
}
println new Test5()

produces these results:

Test1@1acd47{metaClass=groovy.lang.MetaClassImpl@ff2413[class Test1], class=class Test1}
Test2@104c575{metaClass=groovy.lang.MetaClassImpl@3fa5ac[class Test2], class=class Test2}
Test3@1e58cb8[metaClass:groovy.lang.MetaClassImpl@179935d[class Test3], class:class Test3]
Test4@67064{metaClass=groovy.lang.MetaClassImpl@c5495e[class Test4], class=class Test4}
Test5@914f6a[metaClass:groovy.lang.MetaClassImpl@16d2633[class Test5], class:class Test5]

This is using 1.5.2-SNAPSHOT and is slightly different to what 1.5.0 gives you. The issue isn't
anything to do with the + operator per se or whether the script is compiled. It is simply whether
the artifact is being "pretty printed" by Java or Groovy. Groovy tries to produce a prettier looking
format for maps but sometimes Java is calling Java and bypassing Groovy's mechanics for
swapping in the prettier version.

See also the related GROOVY-2331.

Show
Paul King added a comment - 30/Dec/07 11:19 PM Just summarising, this code:
class Test1 {
  public String toString() { super.toString() + properties.toString() }
}
println new Test1()

class Test2 {
  public String toString() { super.toString() << properties }
}
println new Test2()

class Test3 {
  public String toString() { super.toString() + properties }
}
println new Test3()

class Test4 {
  public String toString() { new StringBuffer(super.toString()).append(properties) }
}
println new Test4()

class Test5 {
    public String toString() { "${super.toString()}${properties}" }
}
println new Test5()
produces these results:
Test1@1acd47{metaClass=groovy.lang.MetaClassImpl@ff2413[class Test1], class=class Test1}
Test2@104c575{metaClass=groovy.lang.MetaClassImpl@3fa5ac[class Test2], class=class Test2}
Test3@1e58cb8[metaClass:groovy.lang.MetaClassImpl@179935d[class Test3], class:class Test3]
Test4@67064{metaClass=groovy.lang.MetaClassImpl@c5495e[class Test4], class=class Test4}
Test5@914f6a[metaClass:groovy.lang.MetaClassImpl@16d2633[class Test5], class:class Test5]
This is using 1.5.2-SNAPSHOT and is slightly different to what 1.5.0 gives you. The issue isn't anything to do with the + operator per se or whether the script is compiled. It is simply whether the artifact is being "pretty printed" by Java or Groovy. Groovy tries to produce a prettier looking format for maps but sometimes Java is calling Java and bypassing Groovy's mechanics for swapping in the prettier version. See also the related GROOVY-2331.
Hide
Permalink
blackdrag blackdrag added a comment - 06/Apr/09 9:03 AM

as Paul said the issue is not related to the "+" operator

Show
blackdrag blackdrag added a comment - 06/Apr/09 9:03 AM as Paul said the issue is not related to the "+" operator

People

  • Assignee:
    blackdrag blackdrag
    Reporter:
    Jörg Gottschling
Vote (0)
Watch (0)

Dates

  • Created:
    30/Dec/07 11:43 AM
    Updated:
    08/Apr/09 12:13 AM
    Resolved:
    06/Apr/09 9:02 AM
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.