覆盖标准JVM异常的fillInStackTrace [英] Overriding fillInStackTrace for a standard JVM Exceptions
问题描述
如果我正在使用反射,并且我想查找是否实现了方法,我可以使用getMethod()方法。此方法抛出NoSuchMethodException。
If I'm using reflection and I want to find if a method is implemented or not, i can use the getMethod() method. This method throws a NoSuchMethodException.
有没有办法重载此Exception的fillInStackTrace以优化性能?现在,大约40%的时间花在了这个方法上。
Is there a way to overload the fillInStackTrace of this Exception to optimize the performance ? Right now, about 40% of the time is spent in this Method.
我正在使用一个框架,使用异常作为执行某种控制流的方法。
I'm using a framework using exceptions as a way to perform a certain kind of control flow.
所以我不想过于侵略。如果我正在创建一个扩展Throwable的类并使用这个新类而不是NoSuchMethodException,我有类似的东西:
So I don't want to be too invasive. If i'm creating a class extending Throwable and using this new class instead of NoSuchMethodException, I'm having something like:
NewException is never thrown in body of corresponding trystatement
谢谢
推荐答案
不,因为 getMethod()
直接和你一起调用 new
无法替换 NoSuchMethodException
的代码,因为该类已签名且 fillInStackTrace()
是原生
。
No, since getMethod()
calls new
directly and you can't replace the code for NoSuchMethodException
since the class is signed and fillInStackTrace()
is native
.
你最好的办法就是将呼叫缓存到 getMethod()
中心位置:只需创建一个两级地图: Map< Class,Map< String,Method>>
并使用快速查找而不会抛出异常。
Your best bet is to cache calls to getMethod()
in a central place: Just create a two level map: Map<Class, Map<String, Method>>
and use a quick lookup without any exception throwing.
这篇关于覆盖标准JVM异常的fillInStackTrace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!