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

@Lazy does not work when used in a Trait

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.4.6
    • 4.0.0-alpha-1, 3.0.6
    • groovy-runtime
    • None

    Description

      @Lazy annotation is not honored when used in a trait.
      Here is an example script demonstrating the issue:

          class MyBase{
            @Lazy String test = { 'test' }()
          }
          class TestBaseClass extends MyBase{}
          def tb = new TestBaseClass();
      
          //Dump the current state to output an assert test hasn't been initialized
          println "Testing @Lazy on a Class.."
          println tb.dump()
          assert tb.dump().contains('test=null') //FOR A CLASS, THIS WILL SUCCEED
      
          //Access the test property causing initialization
          println "Accessing the test property."
          assert tb.test
      
          //Dump the current state to output an assert test has now been initialized
          println tb.dump()
          assert tb.test == 'test'
      
          trait MyTrait{
            @Lazy String test = { 'test' }()
          }
          class TestClass implements MyTrait{}
          def t = new TestClass();
      
          //Dump the current state to output an assert test hasn't been initialized
          println "\nTesting @Lazy on a Trait.."
          println t.dump()
          assert t.dump().contains('test=null') //FOR A TRAIT, THIS WILL FAIL THE ASSERTION - A BUG?
      
          //Access the test property causing initialization
          println "Accessing the test property."
          assert t.test
      
          //Dump the current state to output an assert test has now been initialized
          println t.dump()
          assert t.test == 'test'
      

      Attachments

        Issue Links

          Activity

            emilles Eric Milles added a comment -
            trait T {
              @Lazy String x = { -> 'works' }()
            }
            class C implements T {
            }
            print new C().dump() // outputs"<C@52f9aa7f T__x=works>"
            
            emilles Eric Milles added a comment - trait T { @Lazy String x = { -> 'works' }() } class C implements T { } print new C().dump() // outputs "<C@52f9aa7f T__x=works>"

            People

              emilles Eric Milles
              lewisdavidcole David Cole
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: