字节码编程,Javassist篇五《使用Bytecode指令码生成含有自定义注解的类和方法》

简介: 到本章为止已经写了四篇关于字节码编程的内容,涉及了大部分的API方法。整体来说对 Javassist 已经有一个基本的使用认知。那么在 Javassist 中不仅提供了高级 API 用于创建和修改类、方法,还提供了低级 API 控制字节码指令的方式进行操作类、方法。

目录


  • 一、前言
  • 二、开发环境
  • 三、案例目标
  • 四、技术实现
  • 1. 读取类自定义注解
  • 2. 读取方法的自定义注解
  • 3. 读取方法指令码
  • 4. 通过指令修改方法
  • 5. 使用指令码生成方法
  • 五、总结


一、前言

到本章为止已经写了四篇关于字节码编程的内容,涉及了大部分的API方法。整体来说对 Javassist 已经有一个基本的使用认知。那么在 Javassist 中不仅提供了高级 API 用于创建和修改类、方法,还提供了低级 API 控制字节码指令的方式进行操作类、方法。

有了这样的 javassist API 在一些特殊场景下就可以使用字节码指令控制方法。

接下来我们通过字节码指令模拟一段含有自定义注解的方法修改和生成。在修改的过程中会将原有方法计算息费的返回值替换成 0,最后我们使用这样的技术去生成一段计算息费的方法。通过这样的练习学会字节码操作。

二、开发环境

  1. JDK 1.8.0
  2. javassist 3.12.1.GA
  3. 本章涉及源码在:itstack-demo-bytecode-1-05,可以关注公众号:bugstack虫洞栈,回复源码下载获取。你会获得一个下载链接列表,打开后里面的第17个「因为我有好多开源代码」,记得给个Star!

三、案例目标

  1. 使用指令码修改原有方法返回值
  2. 使用指令码生成一样的方法

「测试方法」

@RpcGatewayClazz(clazzDesc = "用户信息查询服务", alias = "api", timeOut = 500)
public class ApiTest {
    @RpcGatewayMethod(methodDesc = "查询息费", methodName = "interestFee")
    public double queryInterestFee(String uId){
        return BigDecimal.TEN.doubleValue();  // 模拟息费计算返回
    }
}
  • 这里使用的注解是测试中自定义的,模拟一个相当于网关接口的暴漏。

四、技术实现

1. 读取类自定义注解

ClassPool pool = ClassPool.getDefault();
// 类、注解
CtClass ctClass = pool.get(ApiTest.class.getName());
// 通过集合获取自定义注解
Object[] clazzAnnotations = ctClass.getAnnotations();
RpcGatewayClazz rpcGatewayClazz = (RpcGatewayClazz) clazzAnnotations[0];
System.out.println("RpcGatewayClazz.clazzDesc:" + rpcGatewayClazz.clazzDesc());
System.out.println("RpcGatewayClazz.alias:" + rpcGatewayClazz.alias());
System.out.println("RpcGatewayClazz.timeOut:" + rpcGatewayClazz.timeOut());
  • ctClass.getAnnotations(),可以获取所有的注解,进行操作

「输出结果:」

RpcGatewayClazz.clazzDesc:用户信息查询服务
RpcGatewayClazz.alias:api
RpcGatewayClazz.timeOut:500

2. 读取方法的自定义注解

CtMethod ctMethod = ctClass.getDeclaredMethod("queryInterestFee");
RpcGatewayMethod rpcGatewayMethod = (RpcGatewayMethod) ctMethod.getAnnotation(RpcGatewayMethod.class);
System.out.println("RpcGatewayMethod.methodName:" + rpcGatewayMethod.methodName());
System.out.println("RpcGatewayMethod.methodDesc:" + rpcGatewayMethod.methodDesc());
  • 在读取方法自定义注解时,通过的是注解的 class 获取的,这样按照名称可以只获取最需要的注解名称。

「输出结果:」

RpcGatewayMethod.methodName:interestFee
RpcGatewayMethod.methodDesc:查询息费

3. 读取方法指令码

MethodInfo methodInfo = ctMethod.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
CodeIterator iterator = codeAttribute.iterator();
while (iterator.hasNext()) {
    int idx = iterator.next();
    int code = iterator.byteAt(idx);
    System.out.println("指令码:" + idx + " > " + Mnemonic.OPCODE[code]);
}
  • 这里的指令码就是一个方法编译后在 JVM 执行的操作流程。

「输出结果:」

指令码:0 > getstatic
指令码:3 > invokevirtual
指令码:6 > dreturn

4. 通过指令修改方法

ConstPool cp = methodInfo.getConstPool();
Bytecode bytecode = new Bytecode(cp);
bytecode.addDconst(0);
bytecode.addReturn(CtClass.doubleType);
methodInfo.setCodeAttribute(bytecode.toCodeAttribute());
  • addDconst,将 double 型0推送至栈顶
  • addReturn,返回 double 类型的结果

此时的方法的返回值已经被修改,下面的是新的 class 类;

@RpcGatewayClazz(
    clazzDesc = "用户信息查询服务",
    alias = "api",
    timeOut = 500L
)
public class ApiTest {
    public ApiTest() {
    }
    @RpcGatewayMethod(
        methodDesc = "查询息费",
        methodName = "interestFee"
    )
    public double queryInterestFee(String var1) {
        return 0.0D;
    }
}
  • 可以看到查询息费的返回结果已经是 0.0D。如果你的程序被这样操作,那么还是很危险的。所以有时候会进行一些混淆编译,降低破解风险。

5. 使用指令码生成方法

5.1 创建基础方法信息

ClassPool pool = ClassPool.getDefault();
// 创建类信息
CtClass ctClass = pool.makeClass("org.itstack.demo.javassist.HelloWorld");
// 添加方法
CtMethod mainMethod = new CtMethod(CtClass.doubleType, "queryInterestFee", new CtClass[]{pool.get(String.class.getName())}, ctClass);
mainMethod.setModifiers(Modifier.PUBLIC);
MethodInfo methodInfo = mainMethod.getMethodInfo();
ConstPool cp = methodInfo.getConstPool();
  • 创建类和方法的信息在我们几个章节中也经常使用,主要是创建方法的时候需要传递;返回类型、方法名称、入参类型,以及最终标记方法的可访问量。

5.2 创建类使用注解

// 类添加注解
AnnotationsAttribute clazzAnnotationsAttribute = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
Annotation clazzAnnotation = new Annotation("org/itstack/demo/javassist/RpcGatewayClazz", cp);
clazzAnnotation.addMemberValue("clazzDesc", new StringMemberValue("用户信息查询服务", cp));
clazzAnnotation.addMemberValue("alias", new StringMemberValue("api", cp));
clazzAnnotation.addMemberValue("timeOut", new LongMemberValue(500L, cp));
clazzAnnotationsAttribute.setAnnotation(clazzAnnotation);
ctClass.getClassFile().addAttribute(clazzAnnotationsAttribute);
  • AnnotationsAttribute,创建自定义注解标签
  • Annotation,创建实际需要的自定义注解,这里需要传递自定义注解的类路径
  • addMemberValue,用于添加自定义注解中的值。需要注意不同类型的值 XxxMemberValue 前缀不一样;StringMemberValue、LongMemberValue
  • setAnnotation,最终设置自定义注解。如果不设置,是不能生效的。

5.3 创建方法注解

// 方法添加注解
AnnotationsAttribute methodAnnotationsAttribute = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
Annotation methodAnnotation = new Annotation("org/itstack/demo/javassist/RpcGatewayMethod", cp);
methodAnnotation.addMemberValue("methodName", new StringMemberValue("查询息费", cp));
methodAnnotation.addMemberValue("methodDesc", new StringMemberValue("interestFee", cp));
methodAnnotationsAttribute.setAnnotation(methodAnnotation);
methodInfo.addAttribute(methodAnnotationsAttribute);
  • 设置类的注解与设置方法的注解,前面的内容都是一样的。唯独需要注意的是方法的注解,需要设置到方法的;addAttribute 上。

5.4 字节码编写方法快

// 指令控制
Bytecode bytecode = new Bytecode(cp);
bytecode.addGetstatic("java/math/BigDecimal", "TEN", "Ljava/math/BigDecimal;");
bytecode.addInvokevirtual("java/math/BigDecimal", "doubleValue", "()D");
bytecode.addReturn(CtClass.doubleType);
methodInfo.setCodeAttribute(bytecode.toCodeAttribute());
  • Javassist 中的指令码通过,Bytecode 的方式进行添加。基本所有的指令你都可以在这里使用,它有非常强大的 API。
  • addGetstatic,获取指定类的静态域, 并将其压入栈顶
  • addInvokevirtual,调用实例方法
  • addReturn,从当前方法返回double
  • 最终讲字节码添加到方法中,也就是会变成方法体。

5.5 添加方法信息并输出

// 添加方法
ctClass.addMethod(mainMethod);
// 输出类信息到文件夹下
ctClass.writeFile();
  • 这部分内容就比较简单了,也是我们做 Javassist 字节码开发常用的内容。添加方法和输出字节码编程后的类信息。

5.6 最终创建的类方法

@RpcGatewayClazz(
    clazzDesc = "用户信息查询服务",
    alias = "api",
    timeOut = 500L
)
public class HelloWorld {
    @RpcGatewayMethod(
        methodName = "查询息费",
        methodDesc = "interestFee"
    )
    public double queryInterestFee(String var1) {
        return BigDecimal.TEN.doubleValue();
    }
    public HelloWorld() {
    }
}

3.jpg

字节码生成含有注解的类和方法

五、总结

  • 本章节我们看到字节码编程不只可以像以前使用强大的api去直接编写代码,还可以向方法中添加指令,控制方法。这样就可以非常方便的处理一些特殊场景。例如 TryCatch 中的开始位置。
  • 关于 javassist 字节码编程本身常用的方法基本已经覆盖完成,后续会集合 JavaAgent 做一些案例汇总,将知识点与实际场景进行串联。
  • 学习终究还是要成体系的系统化深入学习,只言片语有的内容不能很好的形成一个技术栈的闭环,也不利于在项目中实战。


目录
相关文章
|
3天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8096 2
|
12天前
|
域名解析 人工智能 Linux
|
20天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10643 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
6天前
|
人工智能 安全 网络安全
|
11天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1604 5
|
10天前
|
云安全 数据采集 人工智能
|
17天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2757 8
|
10天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2726 0