0

현재 작업하고있는 것과 다른 프로젝트에있는 클래스에서 메서드 집합을 실행하려면 Reflection을 사용하고 있습니다. 이 메서드는이 프로젝트 내에서 다른 메서드를 차례로 호출합니다. 메서드 호출이 성공하더라도 NoSuchMethodError에 의해 발생하는 InvocationTargetException이 발생합니다. 리플렉션을 사용하여 호출하는 메서드가 다른 메서드를 호출하기 때문에이 문제가 발생했다고 추정합니다. 이런 이유로 나는 클래스 경로에 다른 프로젝트를 추가했지만 이것이 작동하지 않았습니다.Reflection을 사용하여 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 저장 방법 이름과 클래스의 완전한 이름 :

다음은 반성 내 현재 코드입니다.

스택 추적 :

:

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

오류가 줄 것으로 보인다 :

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 

편집

이 내가 전화하려고하는 방법입니다

IOUtils.closeQuietly(closeable, null, closeable); 
+1

실험 작업의 일부 코드로 질문을 수정할 수 있습니까? 이것은 우리가 어떤 답을 제공하는 데 도움이 될 수 있습니다. – sakura

+0

방금 ​​리플렉션을 수행 할 제 코드를 제공했습니다. – gracey

+0

전체 스택 추적을 게시하십시오. 어떤 라인이 예외를 던집니까? 'method.invoke (obj, null);'? – m0skit0

답변

3

클래스 org.apache.commons.io.IOUtils에는 closeQuietly 메서드가 없으므로 매개 변수로 java.io.Closeable이 필요합니다. 그것은 다음과 같은 방법이 있습니다

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

당신은 그에 따라 인수를 전달하여야한다. 희망이 도움이됩니다.

+0

GitHub에서 다운로드 한 소스 코드가 있습니다. 나는 거기에 메서드로 closeQuietly java.io.Closeable 매개 변수로 확실 해요. IOUtils.java 클래스 – gracey

+0

코드가 올바른 소스/jar를 참조하는지 확인하는 것보다 – Sanjeev

+0

클래스 경로에 올바른 폴더를 지정하지 않은 것으로 의심됩니다. – gracey

0

1.5 이후 모든 메소드 반영 메소드는 매개 변수 값/유형에 대한 가변 인수입니다.

이 잘못 같습니다 매개 변수가없는 경우

method.invoke(obj, null); 

, 따라서 전화 :

method.invoke(obj); 

Simlarly를,이 :

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

는/단지

해야 할 수 있습니다
Method method = cls.getDeclaredMethod(test.getName()); 
+0

차이를 만들지 않았습니까? – gracey

관련 문제