1

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

Taky
  • 5,284
  • 1
  • 20
  • 29
Hemal
  • 265
  • 1
  • 7
  • 17
  • What is optional argument in Java? – Taky Feb 12 '13 at 10:20
  • 1
    What is the prototype of your method? – fge Feb 12 '13 at 10:21
  • 1
    I think, you have to pass an empty `Object[]` as last argument. – R Kaja Mohideen Feb 12 '13 at 10:22
  • without using reflection, if i simply invoke this method as below : TestInterface obj = new TestImplementation(); obj.TestMethod("str", "str1", "str2"); then it works fine...but if i do the same using reflection...then it gives error.. – Hemal Feb 12 '13 at 10:22
  • What I mean is...the last variable argument normally is optional, but not in case when using reflection. or is there some work around? – Hemal Feb 12 '13 at 10:26
  • @Hernal In Java no optional parameters. There is only overriding mechanism. – Taky Feb 12 '13 at 10:30
  • if not using reflection..i try this code..then it works perfectly fine...TestInterface obj = new TestImplementation(); obj.TestMethod("str", "str1", "str2"); here the last Object[] is optional..rite? – Hemal Feb 12 '13 at 10:32
  • @Hemal Could you please show TestInterface and TestImplementation code? – Taky Feb 12 '13 at 10:40
  • Interface-------------------------------------------------------------public interface TestInterface { public void TestMethod(String str, String str1, String str2, Object ...objects); } – Hemal Feb 12 '13 at 10:43
  • Implementation--------------------------------------------------------public class TestImplementation implements TestInterface{ public void TestMethod(String str1, String str2, String str3, Object ...objects) { .... } } – Hemal Feb 12 '13 at 10:43
  • Main Method-----------------------------------------------------------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"}); } – Hemal Feb 12 '13 at 10:44
  • So I was confuced by lexic you are used. In Java it called Varargs( http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html ) – Taky Feb 12 '13 at 10:46
  • glad we are clear now...so how can we make this work..? – Hemal Feb 12 '13 at 10:50

1 Answers1

2

In Java don't exists optional parameters. You can only override methods or use varargs.

In your case of varargs you are explicitly request Method object with paramters: String, String, String, Object[].

So you must invoke method with same parameters:

mi.invoke(new TestImplementation(), new Object[]{"arg1", "arg2","arg3", new Object[0]);

To understand your problem in general way see this topic.

Community
  • 1
  • 1
Taky
  • 5,284
  • 1
  • 20
  • 29
  • Taky, incase if i try to find method with String, String, String, then it gives java.lang.NoSuchMethodException:...does that mean, variable argument is not optional when using reflection? – Hemal Feb 12 '13 at 10:29
  • It means TestImplementation hasn't TestMethod(String, String, String). Mayby some base class has this method. Review this link: http://stackoverflow.com/questions/4737388/problem-using-model-getclass-getmethod-in-case-of-inheritance or pass correct class into the getMethod(...) – Taky Feb 12 '13 at 10:33
  • there is no base class which has TestMethod(String, String, String), but still if not using reflection we are able to call this method simple by TestInterface obj = new TestImplementation(); obj.TestMethod("str", "str1", "str2"); , but the same cannot be done using reflection, thats wht my point is... – Hemal Feb 12 '13 at 10:37
  • In short the varargs is not optional using reflection...as I mentioned earlier?? – Hemal Feb 12 '13 at 10:55
  • No. It is implicit array of Object passing into the method. In Java level on runtime Object[] and Object... are the same. – Taky Feb 12 '13 at 10:57