Groovy
  1. Groovy
  2. GROOVY-3867

Allow methodMissing/propertyMissing to be defined through category

    Details

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

      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.

        Activity

        Kohsuke Kawaguchi created issue -
        Guillaume Delcroix made changes -
        Field Original Value New Value
        Fix Version/s 1.8-beta-1 [ 15750 ]
        Hide
        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"
                    }
                """
            }
        }
        
        Show
        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" } """ } }
        curious.attempt.bunny@gmail.com made changes -
        Attachment categoryAndMissingMethodFailingTest.patch [ 46620 ]
        curious.attempt.bunny@gmail.com made changes -
        Attachment categoryAndMissingMethodFailingTest.patch [ 46620 ]
        Hide
        malbery added a comment -

        Corrected the attached test.

        Show
        malbery added a comment - Corrected the attached test.
        curious.attempt.bunny@gmail.com made changes -
        Attachment categoryAndMissingMethodFailingTest.patch [ 46675 ]
        Hide
        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: []
        
        Show
        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: []
        Pascal Schumacher made changes -
        Fix Version/s 1.8.x [ 15750 ]
        Hide
        Pascal Schumacher added a comment -

        Is this something for Groovy 3?

        Show
        Pascal Schumacher added a comment - Is this something for Groovy 3?
        Jochen Theodorou made changes -
        Component/s groovy-runtime [ 16250 ]
        Mark Thomas made changes -
        Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
        Mark Thomas made changes -
        Workflow jira [ 12732834 ] Default workflow, editable Closed status [ 12744669 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12970572 ] Default workflow, editable Closed status [ 12978348 ]

          People

          • Assignee:
            Unassigned
            Reporter:
            Kohsuke Kawaguchi
          • Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:

              Development