Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-5989 Inner Class Issues
  3. GROOVY-4737

propertyMissing not called in anonymous or inner classes

    XMLWordPrintableJSON

    Details

    • Type: Sub-task
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: groovy-runtime
    • Labels:
      None

      Description

      The next code snippet illustrates the problem:

      class Foo {
          def propertyMissing(String name) { "missing $name" }
          def getBar() { "bar" }
      }
      
      def f = new Foo()
      assert f.bar == "bar"
      assert f.baz == "missing baz" // porpertyMissing is called.
      
      def f2 = new Foo() {
          def getFoobar() { "foobar" }
      }
      
      assert f2.foobar == "foobar" // This works ok.
      assert f2.bar == "bar" // This works too.
      assert f2.baz == "missing baz" // This breaks :(
      

      The last line throws a MissingFieldException.

      If instead of using an anonymous class for the f2 variable a normal class that extends Foo is used, the propertyMissing method is called correctly.

      No inheritance is required. If an anonymous class is defined as:

      def obj = new Object() {
          def propertyMissing(String name) { "this is also ignored" }
      }
      obj.foo
      

      The MissingFieldException is also thrown.

      This also happens with inner classes. I think this behavior is unexpected, because one would think that the only difference between an inner class and a normal one is that an instance on the first one can access the fields and methods of it's enclosing object.

        Attachments

        1. Groovy4737.groovy
          0.7 kB
          Frank Tolstrup
        2. propertyMissing.groovy
          0.4 kB
          Demian Ferreiro

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                epidemian Demian Ferreiro
              • Votes:
                1 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated: