如何通过检索Java中的异常堆栈来获取批注 [英] How to get annotation by retrieving exception stack in Java

查看:66
本文介绍了如何通过检索Java中的异常堆栈来获取批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想从引发的异常中获取注释.我的示例代码如下:

Hi,
I want to get the annotation from the thrown exception. My example code is like this:

public class P {
    @MyAnnotation(stringValue = "FirstType", intValue = 999)
    public void test() throws Exception {
        // ...
        throw new NullPointerException();
   }
    @MyAnnotation(stringValue = "SecondType", intValue = 111)
    public void test(int a) throws Exception {
        // ...
       throw new NullPointerException();
    }
}


上面的类包含2个方法,并且它们都抛出NullPointerException.
现在,我可以使用callerElement检索异常,并知道引起异常的程序包名称,类名称,方法名称和行号.这是我的代码:


The above class contain 2 methods, and both of them throw NullPointerException.
Now I can use callerElement to retreive the exception and know the package name, class name, method name and line number which cause the exception. Here is my code:

public class CallP {
    public static void main(String[] argv){
        P class_p = new P();
        StackTraceElement[] callerElement = null;
        try{
            class_p.test();
        }catch(Exception e){
            callerElement = e.getStackTrace();
            System.out.println(callerElement[0].toString());
        }
   }
}


在控制台窗口中,我可以看到消息
StackoverflowQuestion.P.test(P.java:9)
我的问题是

1.如何在catch子句中获取注释? (附言:我们所能知道的就是异常堆栈)

2.方法重载时(在P类中,我有两个方法称为test),如何获取确切的注释?(请注意,每个方法都有不同的注释)
那是我所有的问题.感谢您花时间阅读.


In my console window, I can see the message
StackoverflowQuestion.P.test(P.java:9)
My questions are

1. How to get the annotation in catch clause? (p.s. All that we can know is exception stack)

2. When method overloading (in class P, I have two methods called test), how to get the exactly annotation?(p.s. Each method has different annotations)
That''s all my questions. Thanks for spending time on reading.

推荐答案

您不能.

如果您根本没有任何重载的方法,则可以通过以下方式进行操作:

You can''t.

If you haven''t got any overloaded methods at all, then you can do it by something like this:

try {
   foo();
}
catch(Throwable t) {
   StackTraceElement element = t.getStackTrace()[0];
   Class clazz = Class.forName(element.getClassName());
   Method[] methods = clazz.getMethods();
   for(Method method : methods) {
      if (method.getName().equals(element.getMethodName())) {
         for(Annotation annotation : method.getAnnotations()) {
            System.out.println(annotation);
         }
         break;
      }
   }
}



但是,既然您提出了第二点,我想您已经知道了.

要解决此问题,您可以将数据从注释移动到抛出的Exception,实质上是创建一个新的Exception子类,该子类保存您需要发送的所有数据.

这有点丑陋,并且可能导致奇怪的数据重复(以防您在批注中仍需要它).

希望这会有所帮助,
弗雷德里克·博纳德(Fredrik Bornander)



But since you brought up your second point I guess you''ve already figured that out.

To work around it you can move the data from the annotation to the Exception being thrown, essentially creating a new Exception subclass that holds whatever data you need to send across.

That is kind of ugly and might lead to weird duplication of data (in case you still need it on the annotations).

Hope this helps,
Fredrik Bornander


这篇关于如何通过检索Java中的异常堆栈来获取批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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