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

Change In Behavior For MetaMethods Replacing Private Methods

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.4.3
    • None
    • groovy-runtime
    • None

    Description

      The attached privatemethods.zip contains the following:

      src/main/groovy/demo/Widget.groovy
      package demo
      
      class Widget {
      
          def somePublicMethod() {
              'real value'
          }
      
          private somePrivateMethod() {
              'real value'
          }
      }
      
      src/test/groovy/demo/WidgetSpec.groovy
      package demo
      
      import spock.lang.Specification
      
      class WidgetSpec extends Specification {
      
          void 'test meta methods'() {
              when: 'a public method is replaced at runtime'
              Widget.metaClass.somePublicMethod = { ->
                  'test value'
              }
      
              then: 'the meta method is used'
              'test value' == new Widget().somePublicMethod()
      
              when: 'a private method is replaced at runtime'
              Widget.metaClass.somePrivateMethod = { ->
                  'test value'
              }
      
              then: 'the meta method is used'
              // This one passes with Groovy 2.3.11 and fails with 2.4.3
              'test value' == new Widget().somePrivateMethod()
          }
      }
      
      build.gradle
      apply plugin: 'groovy'
      
      repositories {
          jcenter()
      }
      
      dependencies {
          boolean useGroovy24 = true
      
          if(useGroovy24) {
              compile 'org.codehaus.groovy:groovy-all:2.4.3'
              compile 'org.spockframework:spock-core:1.0-groovy-2.4'
          } else {
              compile 'org.codehaus.groovy:groovy-all:2.3.11'
              compile 'org.spockframework:spock-core:1.0-groovy-2.3'
          }
      }
      

      The tests can be run with the following:

      ./gradlew test
      

      Is the change in behavior with respect to private methods here intentional?

      Attachments

        1. privatemethods.zip
          50 kB
          Jeff Brown

        Activity

          People

            Unassigned Unassigned
            brownj Jeff Brown
            Votes:
            5 Vote for this issue
            Watchers:
            10 Start watching this issue

            Dates

              Created:
              Updated: