Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify code dynamically at run time in javassist

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report