Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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: 24204
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.
 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic