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

trait's groovyProxy class doesn't dispatch String#toString()

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.3.0-beta-1
    • Fix Version/s: 2.3.0-rc-1
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      Description

      following code(attached):

      class DynamicTraitTest extends GroovyTestCase {
        @groovy.transform.TypeChecked
        void testTraitTest() {
            def x = new String("hello") as Helloable
            x.hello()
            assert !(x instanceof String)
            assert x.toUpperCase() == "HELLO" // exptected
            assert x.toString() == "hello" // fails
        }
      }
      
      trait Helloable {
          void hello() { println "hello" }
      }
      

      generate following exception:

      % groovy dynamictrait.groovy
      groovy dynamictrait.groovy
      .hello
      E
      Time: 0.13
      There was 1 error:
      1) testTraitTest(DynamicTraitTest)Assertion failed:

      assert x.toString() == "hello" // fails
      \ | | |
      \ | | false
      \ | String1_groovyProxy@24269709
      \ String1_groovyProxy@24269709

      at org.codehaus.groovy.runtime.InvokerHelper.assertFailed(InvokerHelper.java:398)
      at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.assertFailed(ScriptBytecodeAdapter.java:646)
      at DynamicTraitTest.testTraitTest(dynamictrait.groovy:8)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
      at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
      at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1318)
      at org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:927)
      at org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:77)
      at groovy.lang.GroovyShell.runJUnit3Test(GroovyShell.java:353)
      at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:273)
      at groovy.lang.GroovyShell.run(GroovyShell.java:502)
      at groovy.lang.GroovyShell.run(GroovyShell.java:491)
      at groovy.ui.GroovyMain.processOnce(GroovyMain.java:627)
      at groovy.ui.GroovyMain.run(GroovyMain.java:379)
      at groovy.ui.GroovyMain.process(GroovyMain.java:365)
      at groovy.ui.GroovyMain.processArgs(GroovyMain.java:124)
      at groovy.ui.GroovyMain.main(GroovyMain.java:104)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:106)
      at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:128)

      FAILURES!!!
      Tests run: 1, Failures: 0, Errors: 1

      I don't know precise behavior of above case, but metaClass.mixin works with this code:

      class DynamicMixinTest extends GroovyTestCase {
        void testTraitTest() {
            String.metaClass.mixin Helloable
            def x = "hello".asType(Helloable)
            x.hello()
            assert !(x instanceof String)
            assert x.toUpperCase() == "HELLO" // exptected
            assert x.toString() == "hello" // fails
        }
      }
      
      class Helloable {
          void hello() { println "hello" }
      }
      
      

        Attachments

        1. dynamictrait.groovy
          0.4 kB
          UEHARA Junji

          Activity

            People

            • Assignee:
              melix C├ędric Champeau
              Reporter:
              uehaj UEHARA Junji
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: