Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-2503 MOP 2.0 design inflluencing issues
  3. GROOVY-4239

metaClass: if static method with same name and parameters is defined after a instance method, instance method is covert

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.7.1
    • None
    • groovy-jdk
    • None
    • Windows, Groovy 1.7.1

    Description

      If i define methods with same name and parameters on a class, one as static other as instance method, the order becomes important.

      Try this in the groovy console:

      class IDoNothing {
      
      }
      
      IDoNothing.metaClass.sayHello = {
          println "Instance says hello"
      }
      
      IDoNothing.metaClass.static.sayHello = {
          println "Class says hello"
      }
      
      def idn = new IDoNothing()
      
      IDoNothing.sayHello()
      idn.sayHello()
      

      It will print:
      Class says hello
      Class says hello
      although the second sayHello() was called on an instance. If you define the static method first, all works as expected:

      class IDoNothing {
      
      }
      
      IDoNothing.metaClass.static.sayHello = {
          println "Class says hello"
      }
      
      IDoNothing.metaClass.sayHello = {
          println "Instance says hello"
      }
      
      def idn = new IDoNothing()
      
      IDoNothing.sayHello()
      idn.sayHello()
      

      Output:
      Class says hello
      Instance says hello

      Attachments

        Activity

          People

            Unassigned Unassigned
            jkuehn Juri Kuehn
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: