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

Cloning with AutoClone results in Null Pointer Exception

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3.5, 2.3.6, 2.3.7
    • 2.3.8, 2.4.0-beta-4
    • None
    • None
    • Java: Oracle 1.8.0_20
      Linux: Ubuntu 14.04

    Description

      Given: Two groovy classes that uses @AutoClone and loaded in order shown below:

      AutoCloneBug.groovy
      import groovy.transform.AutoClone
      
      @AutoClone
      class B {
        String name='B'
      }
      
      @AutoClone
      class A {
        B b
        String name='A'
      }
      
      
      
      public static void main(String ... args) {
        B b = new B().clone()
      
        //Works
        A a1 = new A(b:b).clone()
      
        //Null Pointer Exception
        A a2 = new A().clone()
      
      }
      

      When: I run this program using Groovy 2.3.5 - 2.3.7

      groovy AutoCloneBug.groovy
      

      Then: It results in Null Pointer Exception.

      Caught: java.lang.NullPointerException: Cannot invoke method clone() on null object
      java.lang.NullPointerException: Cannot invoke method clone() on null object
      	at com.meltmedia.cdp.orch.B$clone.call(Unknown Source)
      	at com.meltmedia.cdp.orch.A.clone(AutoCloneBug.groovy)
      	at com.meltmedia.cdp.orch.A$clone.call(Unknown Source)
      	at AutoCloneBug.run(AutoCloneBug.groovy:25)
      

      Note:
      The issue seems to be in
      https://github.com/groovy/groovy-core/blob/GROOVY_2_3_7/src/main/org/codehaus/groovy/transform/AutoCloneASTTransformation.java#L241

      We possibly need to check for null using ifS .

      Also if we reverse the order of A and B, the issue goes away. Possible because AutoClone transformation thinks at that point that B does not implement Cloneable .

      Attachments

        Activity

          People

            paulk Paul King
            sukrit007 Sukrit Khera
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: