Hi,
I'm trying to call a bunch of methods during run-time using reflection, but I'm getting an exception saying "IllegalArgumentException: wrong number of arguments".
Here's some information on the variables used.
- All elements of mailTestClass[] are classes that extend AbstractTestCase. (FirstTestCase extends AbstractTestCase and FirstTestCase.class is the first element of mailTestClass)
- These classes have constructors which take a String as an argument.
- After instantiating a class, init(Object[][] args) method is called using reflection.
The part of the code which creates the objects using reflection works fine. The code blows up when the method init(Object[][] args) is called.
private void runAll(Class[] mailTestClass, Object[][] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
AbstractTestCase mailTest[] = new AbstractTestCase[mailTestClass.length];
Method methodInit;
Constructor mailTestConstructor;
Class[] constructorParamsClass = new Class[] {String.class};
Integer count[] = new Integer[mailTestClass.length];
Object[] constructorParams = new Object[] {"DummyString"};
int i;
for (i=0; i<mailTestClass.length; i++)
{
try
{
mailTestConstructor = mailTestClass[i].getConstructor(constructorParamsClass);
mailTest[i] = (AbstractTestCase) mailTestConstructor.newInstance(constructorParams);
methodInit = mailTestClass[i].getDeclaredMethod("init", new Class[] {Object[][].class});
methodInit.invoke(mailTest[i], args);//throws the exception here
The code till line 14 creates an instance of FirstTestCase passing a String "DummyString" to it's constructor.
In line 15, please check if the way the method is invoked is correct. That's the line I'm not sure of.
In FirstTestCase.java:
public void init ()
{
.
.
.
}
public void init (Object[][] args) // Overloaded methods
{
.
.
.
}
Thanks. Let me know if additional information is needed.