groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Remko Popma (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (GROOVY-8530) map to interface coercion
Date Thu, 05 Apr 2018 09:54:00 GMT

    [ https://issues.apache.org/jira/browse/GROOVY-8530?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16426687#comment-16426687
] 

Remko Popma edited comment on GROOVY-8530 at 4/5/18 9:53 AM:
-------------------------------------------------------------

A small variation in the call site gives a different error:
{code}
package builder

class ExplicitCoercionTest extends GroovyTestCase {

    void testCoercion() {
        MySpec spec = parseFromSpec(MySpec, []) as MySpec // explicit coercion
        def val = spec.foo()
        assert val == 'value'
    }

    static interface MySpec {
        String foo();
    }

    public <T> T parseFromSpec(Class<T> optionsClass, args) {
        def cliOptions = [:]
        cliOptions['foo'] = 'value'
        cliOptions as T
    }
}
{code}
Gives the following error:
{code:java}
java.lang.ClassCastException: java.lang.String cannot be cast to groovy.lang.Closure

	at org.codehaus.groovy.runtime.ConvertedMap.invokeCustom(ConvertedMap.java:49)
	at org.codehaus.groovy.runtime.ConversionHandler.invoke(ConversionHandler.java:124)
	at com.sun.proxy.$Proxy4.foo(Unknown Source)
	at builder.ExplicitCoercionTest$MySpec$foo.call(Unknown Source)
	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 builder.ExplicitCoercionTest.testCoercion(ExplicitCoercionTest.groovy:7)
...
{code}
It seems that with this syntax the map was successfully coerced, but the method invocation
still fails. What am I doing wrong? Also, why is the map not coerced in the first example
{{CoercionTest}}?


was (Author: remkop@yahoo.com):
A small variation in the call site gives a different error:
{code}
package builder

class ExplicitCoercionTest extends GroovyTestCase {

    void testCoercion() {
        MySpec spec = parseFromSpec(MySpec, []) as MySpec // explicit coercion
        def val = spec.foo()
        assert val == 'value'
    }

    static interface MySpec {
        String foo();
    }

    public <T> T parseFromSpec(Class<T> optionsClass, args) {
        def cliOptions = [:]
        cliOptions['foo'] = 'value'
        cliOptions as T
    }
}
{code}
Gives the following error:
{code:java}
java.lang.ClassCastException: java.lang.String cannot be cast to groovy.lang.Closure

	at org.codehaus.groovy.runtime.ConvertedMap.invokeCustom(ConvertedMap.java:49)
	at org.codehaus.groovy.runtime.ConversionHandler.invoke(ConversionHandler.java:124)
	at com.sun.proxy.$Proxy4.foo(Unknown Source)
	at builder.ExplicitCoercionTest$MySpec$foo.call(Unknown Source)
	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 builder.ExplicitCoercionTest.testCoercion(CoercionTest.groovy:7)
...
{code}
It seems that with this syntax the map was successfully coerced, but the method invocation
still fails. What am I doing wrong? Also, why is the map not coerced in the first example
{{CoercionTest}}?

> map to interface coercion
> -------------------------
>
>                 Key: GROOVY-8530
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8530
>             Project: Groovy
>          Issue Type: Question
>    Affects Versions: 2.5.0-beta-3
>            Reporter: Remko Popma
>            Priority: Major
>
> 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:
> {code}
> 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)
> {code}
> Here is a minimal class that reproduces the issue:
> {code}
> 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
>     }
> }
> {code}
> This gives:
> {code}
> 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)
> {code}
> I'm probably doing something wrong, but can't figure out what... Any hints?



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message