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

Map access is given higher precedence when trying to access fields/properties in classes which implement java.util.Map or extend java.util.HashMap or java.util.Properties

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.8.1, 1.8.2
    • 5.0.0-alpha-1
    • groovy-runtime

    Description

      Using the @Log4j annotation on a groovy class that extends java.util.Properties seems to have odd side effects on closures.

      testscript
      import groovy.util.logging.*
      
      [new Test1(), new Test2(), new Test3()].each {
          runTest(it)
      }
      
      def runTest (obj) {
          try {
               obj.test()
               obj.method()
               println "${obj.getClass()} success"
          }
          catch (e) {
               println "${obj.getClass()} failed $e"
          }
      }
      
      //   classes
      
      @Log4j
      class Test1 {
          def test () {
               println '============================='
               println 'testing plain Groovy class'
               println "testing class [${this.getClass().getSimpleName()}]"
          }
          def method () {
              log.debug 'method called, not in closure'
              ['a', 'b', 'c'].each {
                  log.debug "inside closure [$it]"
              }
          }
      }
      
      @Log4j
      class Test2 extends BaseClass {
          def test () {
               println '============================='
               println 'testing Groovy class that extends another Groovy class'
               println "testing class [${this.getClass().getSimpleName()}]"
          }
          def method () {
              log.debug 'method called, not in closure'
              ['a', 'b', 'c'].each {
                  log.debug "inside closure [$it]"
              }
          }
      }
      
      class BaseClass {
      }
      
      @Log4j
      class Test3 extends java.util.Properties {
          def test () {
               println '============================='
               println 'testing Groovy class that extends java.util.Properties'
               println "testing class [${this.getClass().getSimpleName()}]"
          }
          def method () {
              log.debug 'method called, not in closure'
              ['a', 'b', 'c'].each {
                  log.debug "inside closure [$it]"
              }
          }
      }
      
      log4j.properties
      log4j.rootLogger=DEBUG, CONSOLE
      log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
      log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
      log4j.appender.CONSOLE.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
      
      $ groovy testscript
      =============================
      testing plain Groovy class
      testing class [Test1]
      2011-09-08 15:59:45,757 [main] DEBUG Test1 - method called, not in closure
      2011-09-08 15:59:45,759 [main] DEBUG Test1 - inside closure [a]
      2011-09-08 15:59:45,759 [main] DEBUG Test1 - inside closure [b]
      2011-09-08 15:59:45,759 [main] DEBUG Test1 - inside closure [c]
      class Test1 success
      =============================
      testing Groovy class that extends another Groovy class
      testing class [Test2]
      2011-09-08 15:59:45,768 [main] DEBUG Test2 - method called, not in closure
      2011-09-08 15:59:45,769 [main] DEBUG Test2 - inside closure [a]
      2011-09-08 15:59:45,769 [main] DEBUG Test2 - inside closure [b]
      2011-09-08 15:59:45,769 [main] DEBUG Test2 - inside closure [c]
      class Test2 success
      =============================
      testing Groovy class that extends java.util.Properties
      testing class [Test3]
      2011-09-08 15:59:45,771 [main] DEBUG Test3 - method called, not in closure
      class Test3 failed java.lang.NullPointerException: Cannot invoke method debug() on null object
      
      

      The problem is on Test3 where the log instance var is null inside the closure, but not outside it.
      Only Test 3 has this problem

      Attachments

        1. screenshot-1.png
          27 kB
          Eric Milles

        Issue Links

          Activity

            People

              emilles Eric Milles
              ericksn ericksn
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 2h 40m
                  2h 40m