I am trying to invoke a method using reflection
Method mi = TestInterface.class.getMethod("TestMethod", java.lang.String.class,java.lang.String.class,java.lang.String.class,java.lang.Object[].class);
this method has 3 mandatory string arguments, the last argument, which is the variable argument is optional.
However when I invoke this method as below.
mi.invoke(new TestImplementation(), new Object[]{"arg1", "arg2","arg3"});
then it gives me an error java.lang.IllegalArgumentException: wrong number of arguments
but the last arguement should be optional right? or this doesn't work in case of invoking methods using reflection??
Code:
public interface TestInterface {
public void TestMethod(String str, String str1, String str2, Object... objects);
}
public class TestImplementation implements TestInterface {
public void TestMethod(String str1, String str2, String str3, Object... objects) {
// ....
}
}
public static void main(String[] args) throws Exception {
// works perfectly
TestInterface obj = new TestImplementation();
obj.TestMethod("str", "str1", "str2");
// doesn't work
Method mi = TestInterface.class.getMethod("TestMethod", java.lang.String.class, java.lang.String.class,
java.lang.String.class);
mi.invoke(new TestImplementation(), new Object[] { "arg1", "arg2", "arg3" });
}
Thanks in advance