当前位置: 首页> 科技> 能源 > 郑州今日新闻_批发市场网上进货渠道_如何线上推广自己产品_360优化大师最新版的功能

郑州今日新闻_批发市场网上进货渠道_如何线上推广自己产品_360优化大师最新版的功能

时间:2025/7/11 15:25:10来源:https://blog.csdn.net/yf3241610146/article/details/147357733 浏览次数:0次
郑州今日新闻_批发市场网上进货渠道_如何线上推广自己产品_360优化大师最新版的功能

使用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);}}
}
关键字:郑州今日新闻_批发市场网上进货渠道_如何线上推广自己产品_360优化大师最新版的功能

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: