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

When working with ClassNodes, sometimes need to call getFields() before getField() will work (lazy init)

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.8.0
    • 1.8.1, 1.9-beta-1
    • Compiler
    • None

    Description

      Here is a test that I've been running in the LazyInitOnClassNodeTest class.

      void testLazyInit2() {
      		ClassNode listType = ClassHelper.make(RetentionPolicy.class)
      //		listType.getFields()
      		FieldNode fn = listType.getField "SOURCE"
      		assert fn!=null		
      	}
      

      Like that, it will fail. If the call to getFields() is uncommented, it will pass. This is due to lazy class initialization not running if getFields() isnt run (or some other method that triggers the lazy init). I fixed it by adding:

      if (!redirect().lazyInitDone) redirect().lazyClassInit();

      to getDeclaredField() (used from getField) - but that may not be the best way.

      I haven't checked whether the same problem also affects methods/etc.

      Attachments

        Activity

          People

            paulk Paul King
            aclement Andy Clement
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: