John,

as for similar problems, you might want to check the thread

“field preferred to getter? Shouldn't it be the other way?”

in the list archives. It should be available at

http://groovy.329449.n5.nabble.com/field-preferred-to-getter-Shouldn-t-it-be-the-other-way-td5713987.html

but at the moment, I cannot access it -- always I get a “503: too many active 
connections” report. With some luck, it'll get better over time.

Am not sure whether it is the same problem or not, but at the quick look, seems 
to be related. If it is, well, myself, I would consider it a pretty grave bug, 
but triple alas, it is the intended behaviour, as shown in that thread :(

All the best,
OC

On 25. 8. 2016, at 17:54, John Smiljanic <[email protected]> wrote:

> Groovy 2.4.6
> 
> I am trying to override static property access/mutation in a groovy class.  
> My objective is to read/write static properties from a well defined scope 
> rather than depending upon the Class/ClassLoader scope.
> 
> My current strategy is to use an AST transform to rewrite all static, mutable 
> fields in my classes to use a scope service.  I was planning to use the 
> ExpandoMetaClass to accomplish this as follows:
> 
> Before transformation:
> 
> class Test
> {   
>    static def test = "foo"
>    
>    static def lookupTest() {
>       test
>    }
> }
> 
> After transformation:
> 
> class Test
> {
>    static
>    {
>       Test.metaClass.static.getTest = { -> 
>          Scope.getValue("test")
>       }
>       
>       Test.metaClass.static.setTest = { Object v ->
>          Scope.setValue("test", v)
>       }
>       
>       Test.test = "foo"
>    }
>    
>    static def test = "foo"
>    
>    static def lookupTest() {
>       Test.test
>    }
> }
> 
> where Scope is an abstraction of a Scope service that I already have 
> implemented.  For testing with groovy console I implemented the following 
> simple Scope service:
> 
> class Scope {
>    // this is a simplification of an abstract scope service
>    static Map<String, Object> properties = [:]
>    
>    static def getValue(String k) {
>       properties[k]
>    }
>    static void setValue(String k, Object v) {
>       properties[k] = v
>    }
> }
> 
> One gotcha that I did notice is that if I don't rewrite all access to the 
> property (see lookupTest) to include an explicit reference to the static 
> target then groovy resolves to the actual field rather than using the getter. 
>  For example, if the field is defined as:
> 
> static def test = "foo"
> 
> but I set a different value in the static constructor:
> 
> Test.test = "bar"
> 
> then lookupTest() { test } will print "foo"
> 
> and lookupTest() { Test.test } will print "bar".
> 
> 
> Is this a bug?
> 
> Has anyone attempted anything similar?  Does anyone have other ideas about 
> how one might accomplish this with groovy?
> 
> JR

Reply via email to