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

propertyMissing / methodMissing called on outer instead of inner class

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.7.2, 1.7.10, 1.8.0
    • None
    • groovy-runtime
    • None
    • Win7 64, JDK 1.6.0_17

    Description

      I have an outer FactoryClass creating instances of an InnerClass:

      class MyFactory {
      
          static def createMyInner(String name) {
            return new MyInnerClass(name)
          }
      
          static class MyInnerClass {
      //      def prop = "InnerProperty"
            String name
      
            MyInnerClass(String name) {
              this.name = name
            }
          }
        }
      

      Now I create an Instance of MyInnerClass and try to access a non-existent property or method on it:

          def myImpl = MyFactory.createMyInner("InnerClass")
          def prop = myImpl.prop
      

      The following error occurs:

        groovy.lang.MissingFieldException: No such field: prop for class: MyFactory
      

      When I uncomment the line

        def prop = "InnerProperty"
      

      it is correctly resolved.
      But if I try to add propertyMissing() to my InnerClass:

          static class MyInnerClass {
            ...
            def propertyMissing(name) {
              new MyInnerClass(name)
            }
            ...
          }
      

      the same error persists.

      I read (here: http://groovy.329449.n5.nabble.com/methodMissing-message-td4288343.html#a4305333) that inner classes will get an automatic propertyMissing() from the compiler, delegating to their outer class. Is this the root of the problem here?

      In my Opinion, several things should be changed here:
      1) Existing propertyMissing()-implementations should not be disregarded (this is critical to me!)
      2) The error message should state correctly that InnerClass does not have the specified property
      3) The same goes for methodMissing() (This is going to be critical for me verry soon, too!)

      The problem occurs regardless of MyFactory being a stand-alone class or an inner class of the executing code (f.e. test)

      I'm not good with jUnit, I hope Spock is fine, too.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              myles-g Markus Günther
              Votes:
              5 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated: