使用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);
    }

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

相关推荐
LSL666_11 分钟前
SpringBoot自动配置类
java·spring boot·后端·自动配置类
甜鲸鱼1 小时前
Java与MySQL中的枚举(Enum)
java·mysql
xxxxxxllllllshi1 小时前
【LeetCode Hot100----14-贪心算法(01-05),包含多种方法,详细思路与代码,让你一篇文章看懂所有!】
java·数据结构·算法·leetcode·贪心算法
pengzhuofan1 小时前
Sentinel 服务保护
java·微服务·sentinel
6***37941 小时前
Java安全
java·开发语言·安全
豐儀麟阁贵1 小时前
8.1 异常概述
java·开发语言
qq_12498707532 小时前
基于springboot的疾病预防系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·毕业设计
麦烤楽鸡翅2 小时前
简单迭代法求单根的近似值
java·c++·python·数据分析·c·数值分析
火星数据-Tina2 小时前
低成本搭建体育数据中台:一套 API 如何同时支撑比分网与 App?
java·前端·websocket
lcu1113 小时前
Java 学习38:ArrayList 类
java