groovy
  1. groovy
  2. GROOVY-3493

Cannot override methods via metaclass that are part of an interface implementation

    Details

    • Type: Bug Bug
    • Status: Open Open
    • Priority: Critical Critical
    • Resolution: Unresolved
    • Affects Version/s: 1.6.2
    • Fix Version/s: None
    • Component/s: groovy-runtime
    • Labels:
      None
    • Number of attachments :
      0

      Description

      The following works...

      class T {
             def doIt() { true }
      }
      
      def t = new T()
      
      assert t.doIt()
      t.metaClass.doIt = { -> false }
      assert !t.doIt()
      

      But this fails...

      interface I {
             def doIt()
      }
      
      class T implements I {
             def doIt() { true }
      }
      
      def t = new T()
      
      assert t.doIt()
      t.metaClass.doIt = { -> false }
      assert !t.doIt()
      

        Issue Links

          Activity

          Hide
          David Dawson added a comment -

          It'd also have to handle ClosureStaticMetaMethod.

          Show
          David Dawson added a comment - It'd also have to handle ClosureStaticMetaMethod.
          Hide
          thurston n added a comment -

          What is the status of this bug?
          Is still present in 1.8.4

          Show
          thurston n added a comment - What is the status of this bug? Is still present in 1.8.4
          Hide
          cow like added a comment -

          Bug still present in 2.0.1

          This passes in the console:

          interface IFoo

          { int foo(); }

          class IFooImpl implements IFoo { int foo()

          { 1 }

          }
          IFoo.metaClass.foo =

          { -> 2 }

          // changing to IFooImpl.metaClass fails
          assert new IFooImpl().foo() == 2

          Show
          cow like added a comment - Bug still present in 2.0.1 This passes in the console: interface IFoo { int foo(); } class IFooImpl implements IFoo { int foo() { 1 } } IFoo.metaClass.foo = { -> 2 } // changing to IFooImpl.metaClass fails assert new IFooImpl().foo() == 2
          Hide
          jorge fiallega added a comment - - edited

          In the meantime, what is the recommended way to mock methods in classes that implement and interface.
          Like in the given example how do we mock doIt so that we may test another methods that uses it ?
          I naively thought that using MockFor would get rid of the problem, but I get the same behavior.

          Show
          jorge fiallega added a comment - - edited In the meantime, what is the recommended way to mock methods in classes that implement and interface. Like in the given example how do we mock doIt so that we may test another methods that uses it ? I naively thought that using MockFor would get rid of the problem, but I get the same behavior.
          Hide
          Alex Anderson added a comment -

          Would also appreciate a recommended workaround.

          Show
          Alex Anderson added a comment - Would also appreciate a recommended workaround.

            People

            • Assignee:
              Unassigned
              Reporter:
              Luke Daley
            • Votes:
              26 Vote for this issue
              Watchers:
              24 Start watching this issue

              Dates

              • Created:
                Updated: