使用javassist对类做字节码增强

业务背景

在日常开发中我们依赖的三方库里的类是无法改动的. 但可以使用javassist 在启动时对类的字节码做改动.达到修改类的作用

实现方式

java 复制代码
@Slf4j
public class JeeSiteJavassistShiroByteCode {

    public static void install() {
        try {
            ClassPool pool = ClassPool.getDefault();
            CtClass cc = pool.get("com.jeesite.common.mybatis.mapper.xmltags.IiIiiiiIiiiI");
            cc.defrost();
            CtMethod m = cc.getDeclaredMethod("doGetAuthenticationInfo");
            CtMethod m2 = cc.getDeclaredMethod("getAuthorizationInfo");
            m.insertAfter("{ System.out.println(\"doGetAuthenticationInfo: \"+com.alibaba.fastjson.JSONObject.toJSONString($_)+\"  :   \"+com.alibaba.fastjson.JSONObject.toJSONString($1)); }");
            m2.insertAfter("{ System.out.println(\"getAuthorizationInfo: \"+com.alibaba.fastjson.JSONObject.toJSONString($_)+\"  :   \"+com.alibaba.fastjson.JSONObject.toJSONString($1)); }");
            cc.writeFile(".");
            cc.toClass();
        } catch (Exception e) {
            log.error("bytecode modify failed.", e);
        }
    }
}

利用javassist 可以将方法的入参和返回值打印出来.

启用javassist

java 复制代码
   public static void main(String[] args) {
        JeeSiteJavassistShiroByteCode.install();
        SpringApplication.run(MesApplication.class, args);
    }

在启动之前对类的字节码做修改

相关推荐
2601_949816223 小时前
Redis 配置日志
java
遇见你...4 小时前
A01-Spring概述
java·后端·spring
Via_Neo6 小时前
JAVA中以2为底的对数表示方式
java·开发语言
野生技术架构师7 小时前
一线大厂Java面试八股文全栈通关手册(含源码级详解)
java·开发语言·面试
廋到被风吹走7 小时前
【AI】Codex 多语言实测:Python/Java/JS/SQL 效果横评
java·人工智能·python
tERS ERTS8 小时前
MySQL中查看表结构
java
坊钰8 小时前
Java 死锁问题及其解决方案
java·开发语言·数据库
于先生吖8 小时前
SpringBoot+MQTT 无人健身房智能管控系统源码实战
java·spring boot·后端
仍然.8 小时前
算法题目---模拟
java·javascript·算法
wefly20178 小时前
纯前端架构深度解析:jsontop.cn,JSON 格式化与全栈开发效率平台
java·前端·python·架构·正则表达式·json·php