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

AutoClone throws ClassCastException when used with @ToString or @EqualsAndHashCode

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 2.5.5
    • 3.0.0-beta-1, 2.5.7
    • None
    • 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

              paulk Paul King
              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