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

map to interface coercion

    XMLWordPrintableJSON

Details

    • Question
    • Status: Closed
    • Major
    • Resolution: Resolved
    • 2.5.0-beta-3
    • None
    • None
    • None

    Description

      As part of GROOVY-8520, I'm trying to implement the CliBuilder.parseFromSpec(Class, String[]) method. The implementation of this method populates a Map with data from the command line, and returns this map, coerced to the specified interface type.

      This should work, but the CliBuilderTest.testParseFromSpec and testParseFromInstanceFlagEdgeCases tests fail with an error:

      groovy.lang.MissingMethodException: No signature of method: java.util.LinkedHashMap.first() is applicable for argument types: () values: []
      Possible solutions: print(java.lang.Object), print(java.io.PrintWriter), find(), sort(), is(java.lang.Object), find(groovy.lang.Closure)
      

      Here is a minimal class that reproduces the issue:

      class CoercionTest extends GroovyTestCase {
      
          void testCoercion() {
              def spec = parseFromSpec(MySpec, [])
              assert spec.foo() == 'firstval'
          }
      
          interface MySpec {
              String foo();
          }
      
          public <T> T parseFromSpec(Class<T> optionsClass, args) {
              def cliOptions = [:]
              cliOptions['foo'] = 'firstval'
              cliOptions as T
          }
      }
      

      This gives:

      groovy.lang.MissingMethodException: No signature of method: java.util.LinkedHashMap.foo() is applicable for argument types: () values: []
      Possible solutions: sort(), any(), find(), get(java.lang.Object), get(java.lang.Object), get(java.lang.Object)
      
       at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:72)
       at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:48)
       at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
       at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
       at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
       at some.pkg.CoercionTest.testCoercion(CoercionTest.groovy:7)
      

      I'm probably doing something wrong, but can't figure out what... Any hints?

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              rpopma Remko Popma
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: