• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use Class.getMethod() by primitive type parameter?  RSS feed

 
fei peng
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to use Class.getMethod('methodname',paramA) to load a 'methodname(float)' method,paramA={new Float(13.0)},but when run it, throw a nosuchmethod exception,how JVM distinguish the primitive type parameter and its wrapper?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have to use the float.class (not Float.class) when looking up primitive methods that take floats.
Other primitives follow the same format:
int.class, double.class, boolean.class, etc. The only problem with this is that I don't think that it's possible to dynamically determine these class names (i.e., you can't pass in a String and do a Class.forName() on it); instead, you have to have a bunch of if/elses to test the string.
 
fei peng
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks,it work.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an exercise I did in reflection, inspired by a magazine article with a great idea and code that made me wince. You might step through ThingTester with a debugger and see how it handles int and Integer parameters. I guess I should post a ZIP of the source as well as the HTML version. Something to do tomorrow night. (Kill Bill tonite!) (Next day: Kill Bill rocks!)
Interpreter
[ October 14, 2003: Message edited by: Stan James ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!