This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get method parameter names

 
Rahul.v Joshi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know (at lease till java 5.0) compiler doesnt store method parameter names in class file and we cant retrieve it using reflection.
But I want to get method parameter names can any one tell me any way to do it..or point to any utility (open source) to do the same may be by parsing java file.

what I want is like...

String[] getMethodParams(java.reflection.Method method)

it should return me all the parameter names for this method.

Thanks in advance

~Rahul.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you use a JavaDoc doclet?
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "names"? The parameters are declared as a class/type and a dummy parameter. The dummy parameter is used within the method, no external object/method/anything should know or care about the dummy parameter names.

Do you really mean class/type of the parameters?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The names are stored in the LocalVariableTable attribute of the method, if a class is compiled with debug symbols on (i.e., javac -g). Otherwise, as you say, they're not in the class file, so of course there's no way to get them.

The LocalVariableTable attribute is not accessible using reflection, but it can be read using a bytecode parsing library like ASM.
 
Ratnakar Rahul
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have used the paraname package to read the method parameter names, it works fine for me for a class, however am always getting null as the parameter name in case of an interface and unfortunately i just need to scan interfaces in my code base. Any one faced similar issues, any idea if i am missing something.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DontWakeTheZombies
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please explain how you did it for a class?
Did you found any answer?

Thank you
 
Ratnakar Rahul
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest was kind enough to inform me that interface class files do not have any attribute tables and do not store the method names as such, which seems logical, though am not sure how the various IDEs are able to get around this.

As for my problem, I solved it by scanning all interfaces I was interested in and then loading the classes which implemented these interfaces. Simple matching the method signatures ensured I was looking at the interface method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic