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

Allow methodMissing/propertyMissing to be defined through category

Details

    Description

      When integrating a library defined by a 3rd party into Groovy, it's often convenient to be able to define missing method/property receiver. Since I can't modify the library directly, it would be great if this can be achieved via category.

      In the current MetaClassImpl implementation, category isn't searched for a missing method/property receiver.

      Note that the generic "Object get(String name)" serves as a replacement for propertyMissing, but there's nothing like that for methods. Plus the get method isn't invoked for "foo.someMethod(...)" even though it works for "(foo.someMethod)(...)" due to the difference in the property look up and method invocation handling.

      Attachments

        Issue Links

          Activity

            malbery malbery added a comment - - edited

            Attached is a test that fails:

            class MetaClassImplTest extends GroovyTestCase {
                void testExtensionByCategory() {
                    assertScript """
                        @Category(String)
                        class StringCategory {
                            def methodMissing(String name, args) {
                                return "methodMissing $name"
                            }
                            
                            def bar() {
                            	return "normal method"
                            }
                        }
                        
                        use(StringCategory) {
                            assert "aString".bar() == "normal method"
                            assert "aString".foo() == "methodMissing foo"
                        }
                    """
                }
            }
            
            malbery malbery added a comment - - edited Attached is a test that fails: class MetaClassImplTest extends GroovyTestCase { void testExtensionByCategory() { assertScript """ @Category( String ) class StringCategory { def methodMissing( String name, args) { return "methodMissing $name" } def bar() { return "normal method" } } use(StringCategory) { assert "aString" .bar() == "normal method" assert "aString" .foo() == "methodMissing foo" } """ } }
            malbery malbery added a comment -

            Corrected the attached test.

            malbery malbery added a comment - Corrected the attached test.
            malbery malbery added a comment -

            Test fails on the second assertion:

                [junit] groovy.lang.MissingMethodException: No signature of method: java.lang.String.foo() is applicable for argument types: () values: []
            
            malbery malbery added a comment - Test fails on the second assertion: [junit] groovy.lang.MissingMethodException: No signature of method: java.lang. String .foo() is applicable for argument types: () values: []

            Is this something for Groovy 3?

            pschumacher Pascal Schumacher added a comment - Is this something for Groovy 3?
            ecerulm Ruben Laguna added a comment - - edited

            I've opened a new issue GROOVY-8408 from a conversation with blackdrag in the mailing list which is related to this issue. (invokeMethod instead of missingMethod)

            ecerulm Ruben Laguna added a comment - - edited I've opened a new issue GROOVY-8408 from a conversation with blackdrag in the mailing list which is related to this issue. (invokeMethod instead of missingMethod)
            githubbot ASF GitHub Bot added a comment -

            GitHub user ecerulm opened a pull request:

            https://github.com/apache/groovy/pull/693

            Allow methodMissing/propertyMissing to be defined through category

            Resolves GROOVY-3867

            You can merge this pull request into a Git repository by running:

            $ git pull https://github.com/ecerulm/groovy GROOVY-3867-pq

            Alternatively you can review and apply these changes as the patch at:

            https://github.com/apache/groovy/pull/693.patch

            To close this pull request, make a commit to your master/trunk branch
            with (at least) the following in the commit message:

            This closes #693


            commit 8d6af73c53d0f0ad8c2e7b72d214ba732b14e8ee
            Author: Ruben Laguna <ruben.laguna@...>
            Date: 2018-05-01T10:07:54Z

            Allow methodMissing/propertyMissing to be defined through category

            Resolves GROOVY-3867


            githubbot ASF GitHub Bot added a comment - GitHub user ecerulm opened a pull request: https://github.com/apache/groovy/pull/693 Allow methodMissing/propertyMissing to be defined through category Resolves GROOVY-3867 You can merge this pull request into a Git repository by running: $ git pull https://github.com/ecerulm/groovy GROOVY-3867 -pq Alternatively you can review and apply these changes as the patch at: https://github.com/apache/groovy/pull/693.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #693 commit 8d6af73c53d0f0ad8c2e7b72d214ba732b14e8ee Author: Ruben Laguna <ruben.laguna@...> Date: 2018-05-01T10:07:54Z Allow methodMissing/propertyMissing to be defined through category Resolves GROOVY-3867
            githubbot ASF GitHub Bot added a comment -

            Github user asfgit closed the pull request at:

            https://github.com/apache/groovy/pull/693

            githubbot ASF GitHub Bot added a comment - Github user asfgit closed the pull request at: https://github.com/apache/groovy/pull/693
            daniel_sun Daniel Sun added a comment - The proposed PR is merged. Thanks! https://github.com/apache/groovy/commit/ad664b181249c4b8e8821003d7e750632943ce36
            ecerulm Ruben Laguna added a comment -

            You're welcome. Can I backport this to 2.6 and 2.5 or are those versions closed for this type of change?

            ecerulm Ruben Laguna added a comment - You're welcome. Can I backport this to 2.6 and 2.5 or are those versions closed for this type of change?
            daniel_sun Daniel Sun added a comment -

            I've merged the PR into 2.6 and 2.5 too

            daniel_sun Daniel Sun added a comment - I've merged the PR into 2.6 and 2.5 too

            People

              daniel_sun Daniel Sun
              kohsuke Kohsuke Kawaguchi
              Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: