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

toString() unsupported on proxy

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.7.10, 1.8.0
    • 1.8.3, 1.9-beta-4, 1.7.11
    • None
    • None
    • Patch

    Description

      toString() and other methods that are expected to be on Object should not generate an UnsupportedOperationException when using the Proxy pattern in groovy. Many methods expect non-null objects to allow methods that exist on Object to be called without issue. The example below should throw a MissingMethodException with a nice message that includes the method name and the arguments that were called, but since MissingMethodException.getMessage() calls the proxy's toString() method we get an UnsupportedOperationException on toString() instead, as well as a stack trace that originates from the location that the MissingMethodException's getMessage() is being called, obscuring the actual problem entirely.

      interface A {
        def getValue()
      }
      
      class B { }
      
      def test = [
        getValue: {
          'getValue() called'
        }
      ] as A
      
      def b = new B()
      
      b.call(test)
      

      Stack trace from 1.8.0:

      java.lang.reflect.InvocationTargetException
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      	at java.lang.reflect.Method.invoke(Method.java:597)
      	at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:108)
      	at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)
      Caused by: java.lang.UnsupportedOperationException
      	at org.codehaus.groovy.runtime.ConvertedMap.invokeCustom(ConvertedMap.java:46)
      	at org.codehaus.groovy.runtime.ConversionHandler.invoke(ConversionHandler.java:82)
      	at $Proxy4.toString(Unknown Source)
      	at org.codehaus.groovy.runtime.InvokerHelper.format(InvokerHelper.java:550)
      	at org.codehaus.groovy.runtime.InvokerHelper.format(InvokerHelper.java:504)
      	at org.codehaus.groovy.runtime.InvokerHelper.toArrayString(InvokerHelper.java:689)
      	at org.codehaus.groovy.runtime.InvokerHelper.toString(InvokerHelper.java:109)
      	at groovy.lang.MissingMethodException.getMessage(MissingMethodException.java:55)
      	at java.lang.Throwable.getLocalizedMessage(Throwable.java:267)
      	at java.lang.Throwable.toString(Throwable.java:343)
      	at java.lang.String.valueOf(String.java:2826)
      	at java.lang.StringBuilder.append(StringBuilder.java:115)
      	at groovy.ui.GroovyMain.run(GroovyMain.java:340)
      	at groovy.ui.GroovyMain.process(GroovyMain.java:315)
      	at groovy.ui.GroovyMain.processArgs(GroovyMain.java:112)
      	at groovy.ui.GroovyMain.main(GroovyMain.java:93)
      	... 6 more
      

      Attachments

        1. Groovy4857Bug.groovy
          1 kB
          Tim Yates

        Activity

          People

            guillaume Guillaume Sauthier
            olandere Eric Olander
            Votes:
            7 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: