Tyler Wright
Tyler Wright

Reputation: 825

Java Reflection with Object... as a parameter

I'm wanting to use Java reflection to call a method on a class of mine that has the following signature:

public Object execute(Object...params)

In my loader class, I have the class loaded, but I'm not sure how to setup my getMethod call. Currently, I have something like this:

Method classEntry = _loadedClass.getMethod("execute", new Class[]{Object[].class});

I then try to invoke this method after creating a newInstance of my class by calling:

Object classObj = _loadedClass.newInstance();
classEntry.invoke(classObj, params); // params comes in from the method as Object...params

This is giving me a java.lang.NoSuchMethodException Exception. I know that my issue lies in my getMethod call. How should I set that up to accept a params object?

Upvotes: 0

Views: 858

Answers (1)

Mikhail Vladimirov
Mikhail Vladimirov

Reputation: 13900

If params is of type Object [] then you need to call invoke like this:

classEntry.invoke(classObj, new Object [] {params});

But this does not explain NoSuchMethodException

Upvotes: 2

Related Questions