File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Getting Method Parameter Names using Reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting Method Parameter Names using Reflection" Watch "Getting Method Parameter Names using Reflection" New topic
Author

Getting Method Parameter Names using Reflection

Andy Peter
Greenhorn

Joined: Feb 12, 2002
Posts: 12
I was wondering how I could find the method parameter names (not just types) for the various methods of a class using reflection or some other technique if I have the Java Class.
It would be great if you could provide me some insight into it.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Not possible. This info isn't even present in the .class files that are generated by the compiler, so there is no way for the JVM to know what the name in the original source file was. Which is fine, usually, as the JVM has no reason to care what the name was. Other classes using the API don't need to know the name of a parameter - they just need to know the name of the method, and the types and order of the parameters. Make up any names you like.


"I'm not back." - Bill Harding, Twister
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
If you really, really want these names, I believe you can achieve it by modifying .class files and including attribute_info. You wil also need to write a custom class loader and extensions to Reflection API to get your information, because standard Java system, as Jim said, doesn't provide any support for this task. An example of how this can be implemented is here.
Of course, you need *serious* reasons to go into all this trouble. :roll:
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Good lord, if you write code that depends on all these extra hoops you better have total and permanent control of your deployment platform.


Rob
SCJP 1.4
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
There isn't probably any sensible reason to actually do it, I just wanted to give trouble to Jim. Cannot see when he is saying "not possible"...
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, that's as good a reason as any for me to say it. But in this case, I was mistaken - the parameter names are present in the class files. Of course, we know that there is absolutely no way™ that this info can be obtained through any of the classes in the JDK.
...
[waiting patiently...]
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Sorry, Jim.
It was my way of saying that I am reading Stuart Halloway's "Component Development for the Java™ Platform" and it's really book. It is published in the same DevelopMentor series as "Java Rules" and is almost as good as "Java Rules". Not quite as thorough (for example, they have a chapter on Reflection, but didn't say how to instantiate an inner class via Reflection! Well, I guess it's kinda tangenial to their subject matter), but more practical, so maybe some readers will like it even more.
Just wanted to make sure you read the book.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

I would prefer Jim's first answer to be correct, even though Map's citation proves her point (and that Jim's software is rapidly exceeding the boundaries of its programmer). The last thing a programmer wants, I would think, is knowledge that every little thing he puts in his code gets exposed in compiled form (or bytecode, in this case). I can't even think of a good (i.e., productive or useful) reason for it, other than proving it's possible.
So Andy, why do you want this ability? Whatcha goona use it for?


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Originally posted by Michael Ernest:
I would prefer Jim's first answer to be correct

But it *is* correct!
-------------------------
Mapraputa Is
Sun Certified Programmer for the Java™2 Platform.
IBM Certified Developer - XML and Related Technologies.
Co-author of Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting Method Parameter Names using Reflection
 
Similar Threads
Annotation in Java
How to get method parameter names
"STRUTS-like action"
instantiating a parameterized type
JSP Vs (XML and XSL)