I am currently using Reflection
to execute a set of methods in classes which reside in a different project than the one I am working on. These methods will in turn call other methods within this project. Although the method calling is succeeding, an InvocationTargetException
caused by NoSuchMethodError
is being thrown. I am presuming that this occurred because the methods I am calling using reflection call other methods. For this reason I have added to the class path the other project but this did not work.
Please note that the other project is an open source project I'm using from GitHub
and am solely using it for analysis thus I do not want to manipulate with it.
Can anyone help me please?
Edit:
The following is my current code for reflection:
public void runSelectedTests(MethodSignature test) throws Exception{
//no paramater
Class<?> noparams[] = {};
try{
//load the test at runtime
//get the class
Class<?> cls = Class.forName(test.getClassName());
Constructor<?>[] cons = cls.getDeclaredConstructors();
//can use the first constructor if there are multiple
//if we instantiate with all constructors you end up calling the test methods depending on
//how many constructors you have
Constructor<?> cons1 = cons[0];
Object params[] = null;
if(cons1.getParameterTypes().length > 0){
params = new Object[cons1.getParameterTypes().length];
}
for(int i = 0; i < cons1.getParameterTypes().length; i++){
String type = cons1.getParameterTypes()[i].toString();
if(type.equals("byte") || type.equals("short") || type.equals("int")){
params[i] = 0;
}else if(type.equals("long")){
params[i] = (long)0.0;
}else if(type.equals("float")){
params[i] = (float)0.0;
}else if(type.equals("double")){
params[i] = (double)0.0;
}else if(type.equals("char")){
params[i] = (char)0;
}else if(type.equals("boolean")){
params[i] = false;
}else{
params[i] = null;
}
}
Object obj = cons1.newInstance(params);
//call the test method
Method method = cls.getDeclaredMethod(test.getName(), noparams);
method.invoke(obj, null);
}catch(Exception e){
System.out.println("exception "+e.getMessage());
}
}
The object MethodSignature stores the method name and the fully qualified name of the class.
Stack Trace:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.Evaluation.TestRunner.runSelectedTests(TestRunner.java:72)
at com.Main.AnalyserFactory.main(AnalyserFactory.java:41)
Caused by: java.lang.NoSuchMethodError: org.apache.commons.io.IOUtils.closeQuietly([Ljava/io/Closeable;)V
at org.apache.commons.io.IOUtilsTestCase.testCloseQuietly_AllCloseableIOException(IOUtilsTestCase.java:134)
... 6 more
Edit
This is the method I am trying to call:
public void testCloseQuietly_AllCloseableIOException() {
final Closeable closeable = new Closeable() {
public void close() throws IOException {
throw new IOException();
}
};
IOUtils.closeQuietly(closeable, null, closeable);
}
The error seems to be on the line:
IOUtils.closeQuietly(closeable, null, closeable);
Since 1.5, all Method reflection methods are varargs for the parameter values/types.
This looks wrong:
If there are no parameters, call it thus:
Simlarly, this:
can/should be just
Class org.apache.commons.io.IOUtils does not have any method
closeQuietly
that takesjava.io.Closeable
as a parameter. It has following methods:You shall pass your argument accordingly. Hope this helps.