从Java程序获取局部变量的名称和类型 [英] Getting name and type of local variables from a Java program

查看:441
本文介绍了从Java程序获取局部变量的名称和类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在尝试的代码:

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");

这里我正在编译Java类,然后使用反射来获取所有函数名称,参数并返回类型。但是,我不知道如何获取函数的局部变量的名称和返回类型。我尝试查找字节码大纲,但说它不适用于netbeans。请帮帮我。

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.

推荐答案

查看 javac编译器选项。尤其是-g:vars。

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

这将为调试器填充局部变量表。

This fills the local variable table for debuggers.

请参见相同的问题 Java反射:如何获取变量的名称?

这篇关于从Java程序获取局部变量的名称和类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆