【Arthas】單元測試使用Arthas輸出動態代理增強類


1. 單元測試方法新增代碼,阻止程序終止

System.in.read();

2. 啟動Arthas

選擇項目 com.intellij.rt.junit.JUnitStarter 代表是單元測試進程


3. 查詢指定類的增強類

sc *ControllerName*

輸出

com.controller.ControllerName
com.controller.ControllerName$$EnhancerBySpringCGLIB$$1
com.controller.ControllerName$$EnhancerBySpringCGLIB$$1$$FastClassBySpringCGLIB$$2
com.controller.ControllerName$$FastClassBySpringCGLIB$$3

4. 代碼輸出到文件中

jad com.controller.ControllerName > C:\\ControllerName1.java
jad com.controller.ControllerName$$EnhancerBySpringCGLIB$$1 > C:\\ControllerName2.java
jad com.controller.ControllerName$$EnhancerBySpringCGLIB$$1$$FastClassBySpringCGLIB$$2 > C:\\ControllerName3.java
jad com.controller.ControllerName$$FastClassBySpringCGLIB$$3 > C:\\ControllerName4.java

僅輸出指定方法的源碼

jad com.controller.ControllerName$$EnhancerBySpringCGLIB$$1 methodName > C:\\ControllerNameMethod.java

jad --help
jad --source-only

5. 增強類源碼

CGLib方式增強,使用攔截器(MethodInterceptor)執行AOP相關代碼
com.controller.ControllerName$$EnhancerBySpringCGLIB$$1

public class ControllerName$$EnhancerBySpringCGLIB$$1
extends ControllerName
implements SpringProxy,
Advised,
Factory {
    
    public final Result TestMethod(String string) {
    try {
        MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
        if (methodInterceptor == null) {
            ControllerName$$EnhancerBySpringCGLIB$$1.CGLIB$BIND_CALLBACKS(this);
            methodInterceptor = this.CGLIB$CALLBACK_0;
        }
        if (methodInterceptor != null) {
            return (Result)methodInterceptor.intercept(this, CGLIB$TestMethod$0$Method, new Object[]{string}, CGLIB$TestMethod$0$Proxy);
        }
        return super.TestMethod(string);
    }
    catch (Error | RuntimeException throwable) {
        throw throwable;
    }
    catch (Throwable throwable) {
        throw new UndeclaredThrowableException(throwable);
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM