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

AutoClone throws ClassCastException when used with @ToString or @EqualsAndHashCode

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical
    • Resolution: Fixed
    • Affects Version/s: 2.5.5
    • Fix Version/s: 3.0.0-beta-1, 2.5.7
    • Component/s: None
    • Labels:
      None

      Description

      Let’s say I have the following class:

      @ToString(includes = 'someId')
      @CompileStatic
      @AutoClone
      class SomeClass {
        String someId
      }
      

      When I call clone, I get the following exception:

      org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.some.package.SomeClass(null)' with class 'com.some.package.SomeClass' to class 'java.lang.StringBuilder'
      

      When I look at the decompiled .class file in Intellij, I see the following clone method has been generated:

      @Generated
      public SomeClass clone() throws CloneNotSupportedException {
        StringBuilder _result = (StringBuilder)ScriptBytecodeAdapter.castToType((SomeClass)ScriptBytecodeAdapter.castToType(super.clone(), SomeClass.class), StringBuilder.class);
        return (SomeClass)ScriptBytecodeAdapter.castToType(_result, SomeClass.class);
      }
      

      The issue is not reproducible if I remove the @ToString on the class.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                paulk Paul King
                Reporter:
                moamzia Amin Ziaei
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 20m
                  20m