0

I am using paranamer.jar to get the parameter names of a method. But it does not get any parameter names.

    Class c = Class.forName("com.soa1.MyClass");
    Class[] argTypes = { java.lang.String.class };
    Method method=ABC.class.getMethod("getData",argTypes);
Paranamer paranamer = new CachingParanamer();
    String[] parameterNames = paranamer.lookupParameterNames(method,false);
Parag Phadtare
  • 117
  • 1
  • 3
  • 9
  • Is your code compiled with `varnames` on? – Craig Jun 20 '13 at 01:39
  • no, How do I do that. Please let me know – Parag Phadtare Jun 20 '13 at 01:41
  • Have a look at [this question](http://stackoverflow.com/questions/939194/preserving-parameter-argument-names-in-compiled-java-classes), but it will depend on how you are compiling your code; are you using an IDE? are you compiling from the command line? – Craig Jun 20 '13 at 01:42
  • I am using eclipse. Are there any settings that I can change? – Parag Phadtare Jun 20 '13 at 01:48
  • 2
    To preserve names in the class file for debugging purposes try: "project properties" -> "Java compiler" -> "Add variable attributes to generated class files". But also read @Chris' answer for other possible solutions – Craig Jun 20 '13 at 01:50
  • I already have the settings you mentioned. – Parag Phadtare Jun 20 '13 at 16:31

3 Answers3

3

Did the parameter names come from somewhere? According to ParaNamer's documentation, it uses several ways to obtain parameter names:

  1. Via a __PARANAMER_DATA field. Obviously, this only works if your class has such a field.
  2. Via debug information in your .class file. Obviously, this only works if your class was compiled with debugging information.
  3. Via a @Named annotation. Obviously, this only works if your method parameters contain such an annotation.

If none of these apply to your class, then sorry, parameter names will not be available.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • I am following option 2. But it still does not work. I have the following settings in my eclipse. "project properties" -> "Java compiler" -> "Add variable attributes to generated class files" – Parag Phadtare Jun 20 '13 at 18:14
0

Use this:

Paranamer paranamer = new AdaptiveParanamer();

Instead this:

Paranamer paranamer = new CachingParanamer();

If are not working yet, tell me, cause I have trouble with paranamer too - after help of a brazilian friend I've learned how it works. See ya!

0

keep the debug information when compiling

if you are compiling with eclipse so check the option keep debug information in project-properties-java compiler

if you are compiling with javac add -g option

and in your code use Paranamer paranamer = new AdaptiveParanamer();

Bilel Boulifa
  • 216
  • 2
  • 5