| Author |
How to use Class.getMethod() by primitive type parameter?
|
fei peng
Ranch Hand
Joined: Sep 15, 2003
Posts: 40
|
|
|
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
Joined: Aug 20, 2001
Posts: 1815
|
|
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.
|
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
|
 |
fei peng
Ranch Hand
Joined: Sep 15, 2003
Posts: 40
|
|
|
thanks,it work.
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
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 ]
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
 |
|
|
subject: How to use Class.getMethod() by primitive type parameter?
|
|
|