• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get parameter name in java method?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am having method inside a class.

public GetVersionResponse getVersion(GetVersionRequest versionRequest, long data, int value, String Request) {

return versionRequest;
}


I can able to get the parametertype, return type etc in the method from my own class using reflection like this

Method m[] = interfaceClass.getDeclaredMethods();
Class parameters[] = m[i].getParameterTypes();
String returnClass=m[i].getReturnType().getName();

But my requirement is I need to get the parameters passed name such as versionRequest, data, value, request etc

how to get it in java?





 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From JVM specification:


For every parameter declared in a method declaration, a new parameter variable is created each time that method is invoked. The new variable is initialized with the corresponding argument value from the method invocation



So, compiler will put different variable name in generated class file as its convenience. so As far as i know it is not possible.

[not sure]
however, some where I heard that you can ask compiler to use same variable name what you have used in your source file by passing -g:vargs option.
in eclipse go to project properties-->java compiler--> check the Add variable attribute to generated class file.

and you can get the parameter name in runtime .. I dont know how it can be done
[/not sure]
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch Ruben sam !
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find this : https://coderanch.com/t/329211/java/java/method-parameter-names
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic