如何获取在java中的方法中声明的变量名称 [英] How to get the variables name declared within a method in java

查看:36
本文介绍了如何获取在java中的方法中声明的变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取在java类的方法中声明的变量名?

How to get the variables name declared within a method in java class?

例如:

public class A {
  String x;
  public void xyz(){
     int i;
     String z = null;
     //some code here
  }
  Method[] methods = A.class.getDeclaredMethods();
  for (Method q = methods){

      //some code here to get the variables declared inside method (i.e q)

  }
}

我该怎么做?

提前致谢..

推荐答案

套用另一个答案,没有简单的方法可以通过反射来做到这一点.

有一种方法可以做到.您需要一个完整的 Java 源代码解析器和名称/类型解析器(符号表").

There is a way to do it. You need a full Java source code parser and name/type resolver ("symbol tables").

Java 编译器提供内部 API 来获取该信息.Eclipse JDT 提供了类似的东西.我们的 DMS 软件再工程工具包提供了一个完整的解析器,可以轻松访问这些信息,并提供大量额外的帮助来构建利用这些额外信息的分析器和/或代码生成器.(您可以在我网站的示例 Java 源代码浏览器中看到 DMS 提取的此信息,请参阅生物).

The Java compiler offers internal APIs to get at that information. Eclipse JDT offers something similar. Our DMS Software Reengineering Toolkit offers a full parser with this information easily accessible, and considerable additional help to build analyzers and/or code generators that take advantage of this extra information. (You can see this information extracted by DMS in the example Java Source Code Browser at my site, see bio).

这篇关于如何获取在java中的方法中声明的变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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