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

AutoClone does not check for existing copy constructor

    XMLWordPrintableJSON

Details

    Description

      @AutoClone with COPY_CONSTRUCTOR style does not check if a copy constructor already exists. There are times when the author may want to write their own copy constructor.

      Please see example below:

      import groovy.transform.AutoClone
      import groovy.transform.AutoCloneStyle
      
      @AutoClone(style = AutoCloneStyle.COPY_CONSTRUCTOR)
      class MyClass {
        MyClass(MyClass other) {
          println 'custom copy constructor'
        }
      }
      

      If we compile and examine the generated code, we see duplication:

      > groovyc MyClass.groovy 
      > javap -c MyClass | grep "MyClass("
        protected MyClass(MyClass);
        protected MyClass(MyClass);
      > 
      

      This leads to compilation error later when MyClass.class is used in another code, like so,

      Caught: java.lang.ClassFormatError: Duplicate method name&signature in class file MyClass
      java.lang.ClassFormatError: Duplicate method name&signature in class file MyClass
      

      Attachments

        Activity

          People

            paulk Paul King
            venkats Venkat Srinivasan
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: