0

私は現在Reflection、作業中のプロジェクトとは異なるプロジェクトにあるクラスで一連のメソッドを実行するために使用しています。これらのメソッドは、このプロジェクト内の他のメソッドを呼び出します。メソッドの呼び出しは成功していますが、InvocationTargetExceptioncause byNoSuchMethodErrorがスローされています。これは、リフレクションを使用して呼び出しているメソッドが他のメソッドを呼び出しているために発生したと推測しています。このため、クラスパスに他のプロジェクトを追加しましたが、これは機能しませんでした。

他のプロジェクトは、私が使用しているオープン ソース プロジェクト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.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 に答える