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

ExpandoMetaClass append method does not throw an exception as per docs

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.4.7
    • Component/s: None
    • Labels:
      None

      Description

      I was reading the docs when I came across "Note that the left shift operator is used to append a new method. If the method already exists an exception will be thrown."

      I decided to try it via the below program. There was no exception. I am using groovy 2.3.8

      class A {
      }
      A.metaClass.hello = {
        "hello superclass"
      }
      
      class B extends A {
      }
      B.metaClass.hello << {
        "hello subclass"
      }
      
      B.metaClass.hello << {
        "hello subclass"
      }
      
      new B().hello()
      

        Activity

        Hide
        jwagenleitner John Wagenleitner added a comment -

        Thanks for reporting. I have updated the docs to try to clarify that only existing declared methods on the class/interface will cause an exception and not methods added at runtime.

        Updated wording is:

        Note that the left shift operator is used to append a new method. If a public method with the same name and parameter types is declared by the class or interface, including those inherited from superclasses and superinterfaces but excluding those added to the metaClass at runtime, an exception will be thrown. If you want to replace a method declared by the class or interface you can use the = operator.

        Commit https://github.com/apache/groovy/commit/1065954aafcb93da8258bbac6a0c9ce2397d99d9

        Show
        jwagenleitner John Wagenleitner added a comment - Thanks for reporting. I have updated the docs to try to clarify that only existing declared methods on the class/interface will cause an exception and not methods added at runtime. Updated wording is: Note that the left shift operator is used to append a new method. If a public method with the same name and parameter types is declared by the class or interface, including those inherited from superclasses and superinterfaces but excluding those added to the metaClass at runtime, an exception will be thrown. If you want to replace a method declared by the class or interface you can use the = operator. Commit https://github.com/apache/groovy/commit/1065954aafcb93da8258bbac6a0c9ce2397d99d9

          People

          • Assignee:
            Unassigned
            Reporter:
            anshbansal Aseem Bansal
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development