Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to modify code dynamically at run time in javassist

Shulou Source: shulou.com Published: 2022-06-02 10:34:20 10月13日 Update

Javassist how to dynamically modify code at runtime, I believe many inexperienced people are helpless about this, this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Javassist is a Java class handler commonly used to implement dynamic proxies. Used to implement proxy functionality in various frameworks.

Pros:

0 Ability to operate class

1 can modify class files at runtime

2 widely dependent (hibernate,mybatis will apply its jar package, no need to refer again)

Related code://Listen to port 8000, set//java -agentlib in startup parameters:jdwp=transport=dt_socket,server=y,suspend=n,address=8000HotSwapper hs = new HotSwapper(8000);// ClassPool cp = ClassPool.getDefault();//Get class String clsName = C2.C8.class.getName();CtClass cc = cp.get(clsName);//Get method CtMethod m = cc.getDeclaredMethod ("getSerial");//Rewrite method m.setBody("{ return getM(); }");//Call System.out.println(C2.C8.getM()) once;hs.reload(clsName, cc.toBytecode());//Call System.out.println(C2.C8.getM()) once more; Note:

0 JVM does not allow multiple versions of a class. Changing class requires attention.

1 Need to enable JPDA support Load class java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000

2 Open JPDA will affect debug

After reading the above, do you know how to modify code dynamically at runtime in javassist? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

Tags: Method code dynamic run content more problem agent different helpless for this advantage again function reason parameter multiple for this tool skill Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi OPPO Reno Xiaomi Apple MySQL