java - 解决使用Reflection抛出的NoSuchMethodError异常

标签 java reflection nosuchmethoderror invocationtargetexception

我目前正在使用Reflection 来执行类中的一组方法,这些类位于与我正在处理的项目不同的项目中。这些方法将依次调用该项目中的其他方法。虽然方法调用成功,但会抛出由 NoSuchMethodError 引起的 InvocableTargetException。我推测发生这种情况是因为我使用反射调用的方法调用了其他方法。因此,我已将其他项目添加到类路径中,但这不起作用。

请注意,另一个项目是我从 GitHub 使用的开源项目,并且仅将其用于分析,因此我不想对其进行操作。

有人可以帮我吗?

编辑:

以下是我当前的反射代码:

   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());
    }
}

对象 MethodSignature 存储类的方法名称和完全限定名称。

堆栈跟踪:

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

编辑

这是我尝试调用的方法:

public void testCloseQuietly_AllCloseableIOException() {
    final Closeable closeable = new Closeable() {
        public void close() throws IOException {
            throw new IOException();
        }
    };
    IOUtils.closeQuietly(closeable, null, closeable);
}

错误似乎就行了:

   IOUtils.closeQuietly(closeable, null, closeable);

最佳答案

org.apache.commons.io.IOUtils没有任何将 java.io.Closeable 作为参数的 closeQuietly 方法。它有以下方法:

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

你应该相应地提出你的论点。希望这会有所帮助。

关于java - 解决使用Reflection抛出的NoSuchMethodError异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22116299/

相关文章:

java - 更改 TreeView 的字体和大小

java - 当数组中的参数时调用 Method.invoke()

java - Android:是否可以下载Android自定义View的类(它是LinearLayout的扩展)并在运行时实例化它?

java - 使用哪些集合?

java - 在 Java 中每个线程只能有一个 hibernate session 吗?

java - Spring MVC : Testing with selenium failing with commons-io not found(it exists)

c# - 反射将 css 类添加到 body 标记 c#

java - 这个 "NoSuchMethod"异常中缺少哪个方法?

gradle - Kotlin:测试中的 java.lang.NoSuchMethodError

java - 虚假的 NoSuchMethodError