简体   繁体   English

Java中Reflected方法的异常stacktrace

[英]Exception stacktrace of Reflected method in Java

I am calling a method via reflection , which throws an exception. 我通过反射调用方法,该方法引发异常。

However when I get the stacktrace via 但是当我通过

StackTraceElement[] elements = Thread.currentThread().getStackTrace();

i don't get the entire stack trace upto the reflected method. 我没有整个堆栈跟踪到反射的方法。 It gets the stack trace only uptil my method which implements reflection. 它仅在实现反射的我的方法之前获取堆栈跟踪。

The reflected class is in a different project altogether. 反映的类完全在另一个项目中。

EDIT : Adding code snippet 编辑:添加代码段

Project A 项目A

Class ReflectionImpl {

   public void callMethodsviaReflection{
     try{
              // Reflection code to call the method

         }catch(InvocationTargetException iTE){

           StringBuilder errorLogBuilder = new StringBuilder();
            errorLogBuilder.append(ex.toString());
            errorLogBuilder.append("\n");

            StackTraceElement[] elements = Thread.currentThread().getStackTrace();

             for (int i=0;i<elements.length;i++) {

                 errorLogBuilder.append((elements[i].toString()));
                 errorLogBuilder.append("\n");
             }

            System.out.println(errorLogBuilder.toString());
        }
    }
}

Project B --> References Project A in Eclipse 项目B- > 在Eclipse中引用项目A

Class ReflectedClass{

 List<String> nameList = null;

 public void reflectedMethod(){

     nameList.add("Hello");
    }      
}

At the particular point where you are calling: 在您致电的特定时间点:

StackTraceElement[] elements = Thread.currentThread().getStackTrace();

your 'reflective invocation' has already been completed, which is reflected in the call stack you get back from the call. 您的“反射调用”已经完成,这反映在您从调用返回的调用堆栈中。 Sounds more like you want: 听起来更像您想要的:

iTE.getCause().getStackTrace();

Which will give you the call stack up to and including the method that actually threw the exception. 这将使您的调用堆栈达到并包括实际引发异常的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM