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

The private keyword of an instance variable is causing a behavior change in a map class

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.12
    • 5.0.0-alpha-1
    • None
    • None

    Description

      Problem: Adding the private keyword causes a difference in behavior when using the dot operator

      Consider the following 2 scripts with the same class (1 with the instance variable declared as private with everything else being the same)

      Script 1:

      import java.util.concurrent.ConcurrentHashMap
      import groovy.transform.CompileStatic
      
      class mymap<K,V> extends ConcurrentHashMap<K,V>{
          private String a = 'a';
          def cloned(){
              def clone = new mymap<K,V>()
              clone.putAll(this)
              clone.a = this.a
              return clone
          }
      }
      
      def mm = new mymap<String, String>()
      def fieldA = mymap.class.getDeclaredField("a")
      fieldA.setAccessible(true)
      mm.b = 'b'
      def cloned = mm.cloned()
      println(cloned.a)
      println(fieldA.get(cloned))
      println(cloned)
       

      Output:

      a
      a
      [a:a, b:b]
      

      Script 2:

      import java.util.concurrent.ConcurrentHashMap
      import groovy.transform.CompileStatic
      
      class mymap<K,V> extends ConcurrentHashMap<K,V>{
          String a = 'a';
          def cloned(){
              def clone = new mymap<K,V>()
              clone.putAll(this)
              clone.a = this.a
              return clone
          }
      }
      
      def mm = new mymap<String, String>()
      def fieldA = mymap.class.getDeclaredField("a")
      fieldA.setAccessible(true)
      mm.b = 'b'
      def cloned = mm.cloned()
      println(cloned.a)
      println(fieldA.get(cloned))
      println(cloned)
      

      Output:

      null
      a
      [b:b]
      

      Expectation

      Adding the 'private' keyword in front of the instance variable should not change the output of these 2 scripts

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              Howard Zhang Howard
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: