解决使用反射引发的NoSuchMethodError异常 [英] Solving NoSuchMethodError exception thrown using Reflection

查看:273
本文介绍了解决使用反射引发的NoSuchMethodError异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Reflection在与我正在研究的项目不同的项目中的类中执行一组方法.这些方法将依次调用该项目中的其他方法.尽管方法调用成功,但引发了由NoSuchMethodError引起的InvocationTargetException.我以为发生这种情况是因为我使用反射调用的方法调用了其他方法.因此,我将其他项目添加到类路径中,但这没有用.

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 InvocationTargetExceptioncaused 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.

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

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.

有人可以帮我吗?

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

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

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

The object MethodSignature stores the method name and the fully qualified name of the class.

堆栈跟踪:

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

修改

这是我尝试调用的方法:

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

该错误似乎正在发生:

   IOUtils.closeQuietly(closeable, null, closeable);

推荐答案

Class

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

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

您应据此通过辩论.希望这会有所帮助.

You shall pass your argument accordingly. Hope this helps.

这篇关于解决使用反射引发的NoSuchMethodError异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆