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

metaclass and curry() gives MissingMethodException

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.6-rc-1
    • 1.6-rc-2, 1.5.8, 1.7-beta-1
    • None
    • None
    • Java 1.6; vista sp1

    Description

      Consider the following:

      ===
      def nonCurryClosure =

      {delim, expected -> .... }

      String.metaClass.fastSplit = nonCurryClosure
      def c = nonCurryClosure.curry(",", 4)
      String.metaClass.fastSplitComma4 =

      { c.delegate = delegate; c() }

      ===

      IMHO, the need to reset 'delegate' in fastSplitComma4's closure is wrong.

      As the doco says:

      "When the curry() method is called on a closure instance with one or more arguments, a copy of the
      closure is first made."

      It seems as though the copy is rather inexact in this circumstance.

      There's an associated thread in groovy-user: http://markmail.org/message/n6vn64behmvg4fkq?q=metaclass+and+curry()+gives+MissingMethodException

      I started off with the following:

      ===
      package au.com.transentia.utils.test

      public class FastSplitTest extends GroovyTestCase {

      void setUp() {
      String.metaClass.fastSplit = {delim, expected ->
      def buffer = []
      try

      { StringTokenizer st = new StringTokenizer(delegate, delim); for (int i = 0; i < expected; i++) buffer[i] = st.nextToken(); return buffer.toArray(); }

      catch (NoSuchElementException e)

      { throw new NoSuchElementException("Could not successfully split line: '" + delegate + "' into " + expected + " items delimited by '" + delim + "'"); }

      }

      String.metaClass.fastSplitComma4 = String.metaClass.fastSplit.curry(",", 4)
      }

      void testSplit()

      { assertArrayEquals(['this', 'is', 'a', 'test'].toArray(), "this,is,a,test".fastSplit(",", 4)) }

      void testCurried()

      { assertArrayEquals(['this', 'is', 'a', 'test'].toArray(), "this,is,a,test".fastSplitComma4()) }

      }
      ===

      This gives:
      ===
      "C:\Program Files\Java\jdk1.6.0_11\bin\java" -Didea.launcher.port=7551
      "-Didea.launcher.bin.path=C:\Program Files\JetBrains\IntelliJ IDEA
      8.0.1\bin" -Dfile.encoding=windows-1252 -classpath
      "C:\DEVTOOLS\groovy-1.6-RC-1\embeddable\groovy-all-1.6-RC-1.jar;C:\Program
      Files\Java\jdk1.6.0_11\jre\lib\charsets.jar;C:\Program
      Files\Java\jdk1.6.0_11\jre\lib\deploy.jar;C:\Program
      Files\Java\jdk1.6.0_11\jre\lib\javaws.jar;C:\Program
      Files\Java\jdk1.6.0_11\jre\lib\jce.jar;C:\Program
      Files\Java\jdk1.6.0_11\jre\lib\jsse.jar;C:\Program
      Files\Java\jdk1.6.0_11\jre\lib\management-agent.jar;C:\Program
      Files\Java\jdk1.6.0_11\jre\lib\plugin.jar;C:\Program
      Files\Java\jdk1.6.0_11\jre\lib\resources.jar;C:\Program
      Files\Java\jdk1.6.0_11\jre\lib\rt.jar;C:\Program
      Files\Java\jdk1.6.0_11\jre\lib\ext\dnsns.jar;C:\Program
      Files\Java\jdk1.6.0_11\jre\lib\ext\localedata.jar;C:\Program
      Files\Java\jdk1.6.0_11\jre\lib\ext\sunjce_provider.jar;C:\Program
      Files\Java\jdk1.6.0_11\jre\lib\ext\sunmscapi.jar;C:\Program
      Files\Java\jdk1.6.0_11\jre\lib\ext\sunpkcs11.jar;C:\DEVELOPMENT\IntelliJ\FB\
      out\production\FB;C:\DEVTOOLS\commons-lang-2.4\commons-lang-2.4.jar;C:\DEVTO
      OLS\groovy-1.6-RC-1\lib\junit-3.8.2.jar;C:\Program Files\JetBrains\IntelliJ
      IDEA 8.0.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain
      com.intellij.rt.execution.junit.JUnitStarter -ideVersion5
      au.com.transentia.utils.test.FastSplitTest

      groovy.lang.MissingMethodException: No signature of method:
      groovy.lang.ExpandoMetaClass$ExpandoMetaProperty.curry() is applicable for
      argument types: (java.lang.String, java.lang.Integer) values:

      {,, 4}
      at
      org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapt
      er.java:54)
      at
      org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSit
      e.java:51)
      at
      org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray
      .java:43)
      at
      org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.
      java:116)
      at
      org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.
      java:128)
      at
      au.com.transentia.utils.test.FastSplitTest.setUp(FastSplitTest.groovy:21)
      at
      com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:40)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
      )
      at
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
      .java:25)
      at
      com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)


      groovy.lang.MissingMethodException: No signature of method:
      groovy.lang.ExpandoMetaClass$ExpandoMetaProperty.curry() is applicable for
      argument types: (java.lang.String, java.lang.Integer) values: {,, 4}

      at
      org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapt
      er.java:54)
      at
      org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSit
      e.java:51)
      at
      org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.
      java:128)
      at
      au.com.transentia.utils.test.FastSplitTest.setUp(FastSplitTest.groovy:21)
      at
      com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:40)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
      )
      at
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
      .java:25)
      at
      com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)

      Process finished with exit code -1
      ===

      Attachments

        Activity

          People

            paulk Paul King
            alpheratz alpheratz
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: