• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get parameter name in java method?  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch Ruben sam !
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!