Solving NoSuchMethodError exception created using Reflection

I am currently using Reflection to execute a set of methods in classes that are in a different project than the one I'm working on. These methods, in turn, will call other methods in the framework of this project. Although the method call succeeds, an InvocationTargetException is NoSuchMethodError by NoSuchMethodError . I suppose this happened because the methods that I call using reflection invoke other methods. For this reason, I added to the path to another project, but it did not work.

Please note that another project is an open source project, which I use from GitHub and use it exclusively for analysis, so I don’t want to manipulate it.

Can anybody help me?

Edit:

Below 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 MethodSignature object stores the method name and fully qualified class name.

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'm 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); 
0
source share
2 answers

The org.apache.commons.io.IOUtils class does not have any closeQuietly method that takes java.io.Closeable as a parameter. It has the following methods:

  closeQuietly(InputStream input) closeQuietly(OutputStream output) closeQuietly(Reader reader) closeQuietly(Writer writer) 

You must pass your arguments accordingly. Hope this helps.

+3
source

Starting from 1.5, all method reflection methods are varargs for parameter values ​​/ types.

This looks wrong:

 method.invoke(obj, null); 

If there are no parameters, call them like this:

 method.invoke(obj); 

Nice, this is:

 Method method = cls.getDeclaredMethod(test.getName(), noparams); 

can / should just be

 Method method = cls.getDeclaredMethod(test.getName()); 
0
source

All Articles