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

@Lazy(soft=true) not quite right

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.1.1
    • Fix Version/s: 2.1.7, 2.2.0-beta-2
    • Component/s: groovy-jdk
    • Labels:
      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 
                      }
                  }
              }
          }
      

        Attachments

          Activity

            People

            • Assignee:
              tim_yates Tim Yates
              Reporter:
              tim_yates Tim Yates
            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: