java 变量获取 type,从Java程序获取局部变量的名称和类型

这篇博客探讨了如何使用Java Compiler API编译Java类,并通过反射获取函数名称、参数和返回类型。作者遇到挑战在于无法直接获取函数的局部变量名和返回类型。解决方案是利用编译选项-g:vars,该选项会为调试器填充本地变量表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This is the code which I am trying out:

JavaCompiler compilerA = ToolProvider.getSystemJavaCompiler();

int resultA = compilerA.run(null,null,null,"/Users/a/Documents/Java/a.java");

System.out.println("Compile result code = " + resultA);

File fileA = new File("/Users/a/Documents/Java/a.class");

JavaCompiler compilerB = ToolProvider.getSystemJavaCompiler();

int resultB = compilerB.run(null,null,null,"/Users/a/Documents/Java/b.java");

System.out.println("Compile result code = " + resultB);

File fileB = new File("/Users/a/Documents/Java/b.class");

Here I am compiling a Java class and then using reflection to get all the function names, parameters and return types. However, I have no clue how to get the names and return types of the local variables of the functions. I tried looking up bytecode outline but it says it isn't available for netbeans. Please help me out.

解决方案

Look at the javac compiler options. Especially -g:vars.

This fills the local variable table for debuggers.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值