我试图使用instrumentation调试java应用程序.当前系统的问题是
这使得很难追溯功能损坏的根本原因.
为了处理这种情况,我开发了工具,使用Instrumentation
API的java代理,我能够注入日志语句,并解决了一半的问题.
但下一个问题是记录异常.我想扩展我的工具记录在应用程序执行期间抛出的每个异常.我尝试使用注入"的try-catch"块javaassist
为方法API(使用addCatch
,insertBefore
和insertAfter
),并且它是有效的一定程度.
public byte[] transform(ClassLoader loader,
String className,
Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer)
throws IllegalClassFormatException {
if (className.startsWith("com/alu/")) {
return insertLog(className, classBeingRedefined, classfileBuffer);
}
if(className.endsWith("Exception")){
System.out.println("============= exception occured "+className);
}
Run Code Online (Sandbox Code Playgroud)
这里 inserLog(..)
方法将注入必要的日志语句并且工作正常,但是当有任何异常时它不会转换为变换器.
但问题是一些方法处理异常内部(即使没有log/sysout).
例如:
try {
if(search.equals("Category")){
//do operation
}
} catch (Exception e) {
}
Run Code Online (Sandbox Code Playgroud)
这个代码NullPointerException
在值为search
null 时吃,我从来不知道这个异常和应用程序失败了.
最终我想要的是记录应用程序抛出的任何异常的机制.以下详细信息将被捕获
我知道有API Thread.setDefaultUncaughtExceptionHandler
,但不确定它如何与java检测一起使用.我没有任何源访问该应用程序.
[更新1]
我发现下面链接告诉使用retransformation
,我会试一试并更新 …