This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How to get method parameter names

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you use a JavaDoc doclet?
 
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DontWakeTheZombies
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic