当前位置: 首页 > news >正文

javassist

使用javassist获取参数名

1,添加依赖

需要在pom.xml文件中添加下面的依赖:

<dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>3.28.0-GA</version>
</dependency>

2,示例代码及详解

// UserController.java
package com.example;public class UserController {public void saveUser(String username, int age) {// 方法实现}
}

下面是使用 javassist 获取 saveUser 方法参数名的代码:

import javassist.*;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute;
import javassist.bytecode.MethodInfo;import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;public class ParameterNameExtractor {public static void main(String[] args) throws Exception {// 反射获取目标类的方法Class<?> targetClass = Class.forName("com.example.UserController");Method method = targetClass.getMethod("saveUser", String.class, int.class);// 获取 ClassPool 实例,它是 javassist 的类池,用于管理类的字节码ClassPool pool = ClassPool.getDefault();// 获取目标类的 CtClass 对象,CtClass 表示类的字节码表示CtClass ctClass = pool.get(targetClass.getName());// 获取方法的参数类型数组Class<?>[] parameterTypes = method.getParameterTypes();//参数类型数组CtClass[] ctParams = new CtClass[parameterTypes.length];for (int i = 0; i < parameterTypes.length; i++) {ctParams[i] = pool.getCtClass(parameterTypes[i].getName());}// 获取目标方法的 CtMethod 对象CtMethod ctMethod = ctClass.getDeclaredMethod(method.getName(), ctParams);// 获取方法的字节码信息MethodInfo methodInfo = ctMethod.getMethodInfo();// 获取方法的代码属性CodeAttribute codeAttribute = methodInfo.getCodeAttribute();// 获取方法的局部变量属性,其中包含参数名信息LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);if (attr == null) {System.out.println("未找到局部变量属性,可能编译时未保留参数名信息。");return;}// 确定参数名的起始`在这里插入代码片`位置int pos = java.lang.reflect.Modifier.isStatic(method.getModifiers()) ? 0 : 1;List<String> parameterNames = new ArrayList<>();int parameterCount = method.getParameterCount();for (int i = 0; i < parameterCount; i++) {// 获取参数名String parameterName = attr.variableName(i + pos);parameterNames.add(parameterName);}// 输出参数名System.out.println("方法 " + method.getName() + " 的参数名:");for (String name : parameterNames) {System.out.println(name);}}
}
http://www.xdnf.cn/news/31879.html

相关文章:

  • Python concurrent.futures模块的ProcessPoolExecutor, ThreadPoolExecutor类介绍
  • 在 Node.js 中使用原生 `http` 模块,获取请求的各个部分:**请求行、请求头、请求体、请求路径、查询字符串** 等内容
  • Python爬虫实战:获取网易新闻数据
  • Windows系统安装`face_recognition`
  • 2. ubuntu20.04 和VS Code实现 ros的输出 (C++,Python)
  • DeepSeek与Napkin:信息可视化领域的创新利器
  • [matlab]南海地形眩晕图代码
  • Github 2025-04-19Rust开源项目日报 Top10
  • Prompt-Tuning 提示词微调
  • 机器学习核心算法全解析:从基础到进阶的 18 大算法模型
  • MySQL运维三部曲初级篇:从零开始打造稳定高效的数据库环境
  • 10软件测试需求分析案例-查询学习信息
  • 详讲Linux下进程等待
  • Go-zero框架修改模版进行handler统一响应封装
  • 手撕 简易HashMap
  • YOLO11改进-Backbone-使用MobileMamba替换YOLO backbone 提高检测精度
  • 在服务器上部署MinIO Server
  • JMeter实现UI自动化测试的完整方案
  • 配置管理与系统文档
  • MyImgConverter:图片批量处理工具
  • 智能提示语全周期优化系统:云原生架构设计与工程实践
  • LPDDR中读操作不存在Additive Latency(AL)的技术思考
  • opencv 最近邻插值法的原理
  • 集合框架(详解)
  • 手机投屏到电视方法
  • 从UDS协议学习ISO网络七层架构:汽车诊断网络协议的分层逻辑剖析
  • vue3专题1------父组件中更改子组件的属性
  • 应急响应篇近源攻击Docker镜像容器分析Dockfile路径定位基线扫描
  • MPTCP 的吞吐困局
  • 数据库基础-B+树