[ 
https://issues.apache.org/jira/browse/GROOVY-8555?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Paul King closed GROOVY-8555.
-----------------------------

> The private keyword of an instance variable is causing a behavior change in a 
> map class
> ---------------------------------------------------------------------------------------
>
>                 Key: GROOVY-8555
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8555
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.4.12
>            Reporter: Howard
>            Priority: Major
>             Fix For: 5.0.0-alpha-1
>
>
> h1. 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)
> h2. Script 1:
> {code:java}
> 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)
>  {code}
> h2. Output:
> {code}
> a
> a
> [a:a, b:b]
> {code}
> h2. Script 2:
> {code}
> 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)
> {code}
> h2. Output:
> {code}
> null
> a
> [b:b]
> {code}
> h1. Expectation
> Adding the 'private' keyword in front of the instance variable should not 
> change the output of these 2 scripts



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to