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

asked Feb 8, 2013 at 10:12
9
  • 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? Commented Feb 8, 2013 at 10:14
  • Those are all local variables, you cannot access out of their scope. Commented Feb 8, 2013 at 10:14
  • Perhaps if you explain why you need them there might be a better solution than reflection? Commented Feb 8, 2013 at 10:16
  • possible duplicate of Can I get information about the local variables using Java reflection? Commented Feb 8, 2013 at 10:16
  • 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... Commented Feb 8, 2013 at 10:46

2 Answers 2

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

answered Feb 8, 2013 at 10:45
0
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
3141 silver badge19 bronze badges
answered Feb 8, 2013 at 10:55
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.