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 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.
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.
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.
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