groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Blake McBride <blake1...@gmail.com>
Subject Reflection problem with varargs
Date Tue, 14 Jul 2020 17:55:38 GMT
Greetings,

I am using Groovy 2.4.15-indy.  I am having a problem executing a Groovy
function through reflection.  Here is the Groovy code:

class Example {

    static Object myMethod(Object ... args) {
        System.out.println("Hello from groovy!")
        int r = (Integer) args[0] + (Integer) args[1];
        return r
    }
}


And here is the Java code:


static void test() throws IOException, NoSuchMethodException,
InvocationTargetException, IllegalAccessException {
    String fname =
"/home/blake/intellijProjects/Arahant/src/java/com/arahant/custom/Example.groovy";
    GroovyClassLoader groovyLoader = new
GroovyClassLoader(GroovyClass.class.getClassLoader());
    Class<?> groovyClass = groovyLoader.parseClass(new
GroovyCodeSource(new File(fname)), false);
    Class<?> [] ca = {
            Object[].class
    };
    Method methp = groovyClass.getMethod("myMethod", ca);
    Object r = methp.invoke(null, 4, 5);  // Here is where the error occurs
    int x = 1;  //  place for a breakpoint
}


At the indicated location, I am getting the error:

java.lang.IllegalArgumentException: wrong number of arguments

I have tried a veriety of alternatives with no success.  Any help
would sure be appreciated.

Thanks!

Blake McBride

Mime
View raw message