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

@Lazy(soft=true) not quite right

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.1.1
    • 2.1.7, 2.2.0-beta-2
    • groovy-jdk
    • 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

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

            Dates

              Created:
              Updated:
              Resolved: