Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7394

@ToString could support non-field properties

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.4.3
    • 2.5.0-alpha-1
    • xforms

    Description

      Cloned from the @EqualsAndHashCode issue to handle the same problem with @ToString as an independent issue.

      I have the following class representing a location in Amazon S3. Depending on some inner business logic, I often need to split the object key into a prefix and a file name, so I access them all through the getKey() method; with S3, the only thing that matters is the final concatenated string.

      The generated toString method doesn't display properties following the JavaBean conventions, e.g. with explicit getXxx() or isYyy() methods but no field as per normal Groovy conventions for properties with auto getters/setters.

      import groovy.transform.*
      
      @ToString(includes = ['bucket', 'key'], includeNames = true)
      class S3ImageLocation {
          final String bucket
          final String prefix
          final String subKey
      
          S3ImageLocation(String bucket, String prefix, String subKey) {
              this.bucket = bucket
              this.prefix = prefix
              this.subKey = subKey
          }
      
          S3ImageLocation(String bucket, String subKey) {
              this(bucket, null, subKey)
          }
      
          String getKey() { prefix ? "$prefix/$subKey" : subKey }
      }
      
      // expected below (current is 'S3ImageLocation(bucket:foo)')
      assert new S3ImageLocation('foo', 'bar', 'baz').toString() == 'S3ImageLocation(bucket:foo, key:bar/baz)'
      

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              chrylis Christopher Smith
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: