0

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

请注意,另一个项目是我正在使用的开源项目,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);
4

2 回答 2

3

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

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

你应该相应地通过你的论点。希望这可以帮助。

于 2014-03-01T16:12:37.460 回答
0

从 1.5 开始,所有 Method 反射方法都是参数值/类型的可变参数。

这看起来不对:

method.invoke(obj, null);

如果没有参数,这样调用它:

method.invoke(obj);

同样,这:

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

可以/应该只是

Method method = cls.getDeclaredMethod(test.getName());
于 2014-03-01T16:15:39.337 回答