Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
2.1.1
-
None
Description
As discussed in this SO question:
http://stackoverflow.com/questions/15429315/how-to-build-a-reinitializable-lazy-property-in-groovy
If you have the following class:
class MyObject { @Lazy(soft=true) volatile String test = { //initalize with network access println 'init' Thread.sleep(1000) 'test' }() }
The generated getter is:
public java.lang.String getTest() { java.lang.String res = $test?.get() if ( res != null) { return res } else { synchronized ( this ) { if ( res != null) { return res } else { res = { this.println('init') java.lang.Thread.sleep(1000) return 'test' }.call() $test = new java.lang.ref.SoftReference( res ) return res } } } }
This is incorrect as I believe it is missing a $test?.get() inside the synchronized block like so:
public java.lang.String getTest() { java.lang.String res = $test?.get() if ( res != null) { return res } else { synchronized ( this ) { res = $test?.get() if ( res != null) { return res } else { res = { this.println('init') java.lang.Thread.sleep(1000) return 'test' }.call() $test = new java.lang.ref.SoftReference( res ) return res } } } }