Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Java Method with Suppliers from Groovy

Tags:

groovy

How can I call a Java method that accepts suppliers as varargs from a Groovy application?

My Java method:

public class Framework {

    public static void run(Supplier<?>... suppliers) {
        System.out.println(Arrays.stream(suppliers).map(supplier -> supplier.get()).collect(Collectors.toList()));
    }

}

My Groovy application:

class Application {

    static void main(String[] arguments) {
        Framework.run { 42 }
    }

}

IntelliJ doesn't show any errors. However, when I try to execute my Groovy application, I get an exception:

Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: static Framework.run() is applicable for argument types: (Application$_main_closure1) values: [Application$_main_closure1@aecb35a]
Possible solutions: run([Ljava.util.function.Supplier;), dump(), find(), any(), grep(), grep(java.lang.Object)
    at groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1525)
    at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1511)
    at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:50)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127)
    at Application.main(Application.groovy:6)
like image 855
Martin Avatar asked Oct 16 '22 05:10

Martin


1 Answers

Currently, I suppose the only solution would be:

Framework.run({ 42 } as Supplier)
Framework.run({ 42 } as Supplier, { 43 } as Supplier)
Framework.run([{ 42 }, { 43 }] as Supplier[])

I've also tried Groovy 3.0.0-alpha-4. It supports lambda syntax but not in the vararg case. Quick search through their jira didn't show any issues on this matter. You can raise the issue as explained at http://groovy-lang.org/support.html

like image 73
Dmitry Khamitov Avatar answered Oct 21 '22 04:10

Dmitry Khamitov