1

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

for eg:

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)

  }
}

How can i do that?

Thanks in advance..

user1919581
  • 481
  • 2
  • 14
  • 32
  • 1
    So you want to get `i` and `z`? I don't think that's possible, but what do you plan to do with them anyway? – assylias Feb 08 '13 at 10:14
  • Those are all local variables, you cannot access out of their scope. – Pradeep Simha Feb 08 '13 at 10:14
  • Perhaps if you explain why you need them there might be a better solution than reflection? – Ben Thurley Feb 08 '13 at 10:16
  • possible duplicate of [Can I get information about the local variables using Java reflection?](http://stackoverflow.com/questions/6816951/can-i-get-information-about-the-local-variables-using-java-reflection) – Rohit Jain Feb 08 '13 at 10:16
  • @assylias yes, i just want to list the local variables declared in class and within methods and their access modifiers etc. – user1919581 Feb 08 '13 at 10:18
  • @Ben Thurley, i just want to list all method names, variable names and their scope in a class. – user1919581 Feb 08 '13 at 10:21
  • That's the what not the why. Variables defined within a method are all locally scoped to that method. It doesn't really make any sense to retrieve them with relection because they only exist while that method is being run and as they are encapsulated they don't impact anything outside of the method. – Ben Thurley Feb 08 '13 at 10:27
  • 1
    @Ben Thurley, I thought for big programs it will be useful to understand and analyze it if we can come to know the variables their types and method names and their parameter list etc so only... – user1919581 Feb 08 '13 at 10:46
  • @user1919581: If you like your answers, you should upvote them (click on the gray upward triangle on answers you like). – Ira Baxter Feb 08 '13 at 16:37

2 Answers2

2

There is no simple way to do this.

If those were fields, you could get their names using reflection. However, local variable and parameter names are not loaded into the JVM. So you would need to resort to reading the "A.class" file and extracting the debug information for that method. And the bad news is that if the class wasn't compiled with debug information, then even that wouldn't work.

There are libraries around for reading ".class" files for various purposes, but I can't give a specific recommendation.


But the $64,000 question is "But why ...?". What is the point of listing the local variable names for a method from Java? Can't you just look at the source code? Can't you dump the ".class" file with "javap" or decompile it with some 3rd party decompiler?


I thought for big programs it will be useful to understand and analyze it if we can come to know the variables their types and method names and their parameter list etc so only...

I think you just need a decent IDE ...

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

To paraphrase another answer, There's no simple way to do this with reflection.

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

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).

max
  • 166
  • 13
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341