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

Calling a method overwritten via metaClass from another method uses the original (non-overwritten) method if the overridden class extends something

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.8.2
    • 1.8.3, 1.9-beta-4
    • primtive opts
    • None

    Description

      Hi Everyone

      I'm using Groovy 1.8.2, and there's a variant of http://jira.codehaus.org/browse/GROOVY-4884. This code fails for me:

      import org.junit.Test;
      
      public class BreakingExample_NoMetaclassOverride {
         @Test
         void testNotOverriden() {
            def list = []
            ClassUnderTest cut = new ClassUnderTest()
            cut.metaClass.getRemoteObject = { ->
               return [method: {obj -> 
                  list << obj
               }] as RemoteObject
            }
            
            String val = "Value" 
            cut.someMethod(val)
            assert list == [val]
         }
      }
      
      public class ClassUnderTest extends RemoteObject {
         public def someMethod(String someValue) {
            RemoteObject object = getRemoteObject()
            object.method(someValue)
         }
         protected RemoteObject getRemoteObject() {
            return new RemoteObject()
         }
      }
      
      public class RemoteObject {
         public void method(obj) { /* Something */ }
      }
      

      The only difference between this and #4884 is the "extends RemoteObject" added to ClassUnderTest. With it in, the test fails. Remove it, the test passes.

      Please let me know if you need any more info!

      Thanks
      Jason Griffith

      Attachments

        Activity

          People

            roshandawrani Roshan Dawrani
            jason@bobberinteractive.com Jason Griffith
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: