Getting name and type of local variables from a Java program

我的梦境 提交于 2019-12-08 01:48:31

问题


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.


回答1:


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

This fills the local variable table for debuggers.

See same qustion Java Reflection: How to get the name of a variable?



来源:https://stackoverflow.com/questions/11287933/getting-name-and-type-of-local-variables-from-a-java-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!