It's not a secret anymore!
The moose likes Java in General and the fly likes How to get method parameter names Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to get method parameter names" Watch "How to get method parameter names" New topic

How to get method parameter names

Rahul.v Joshi

Joined: Sep 20, 2007
Posts: 2
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

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Can you use a JavaDoc doclet?

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Pat Farrell

Joined: Aug 11, 2007
Posts: 4659

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

Joined: Jul 08, 2003
Posts: 24189

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.

[Jess in Action][AskingGoodQuestions]
Ratnakar Rahul

Joined: Jan 29, 2008
Posts: 2

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

Joined: Oct 30, 2001
Posts: 1970
avihai marchiano
Ranch Hand

Joined: Jan 10, 2007
Posts: 342
Can you please explain how you did it for a class?
Did you found any answer?

Thank you
Ratnakar Rahul

Joined: Jan 29, 2008
Posts: 2
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.
I agree. Here's the link:
subject: How to get method parameter names