• Post Reply Bookmark Topic Watch Topic
  • New Topic

java. lang .reflection .Method .invoke()  RSS feed

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the invoke mathod requires all the underlying method parameters are passed in using an object array as in its prototype:

public Object invoke(Object obj,
Object[] args)
throws IllegalAccessException,
IllegalArgumentException,
InvocationTargetException


but what if the underlying method have primitive parameters? Must the method have to accept reference typa params only? if not, then back to my first question, how do we pass in these primitive param values?
(And also, if the return type is of primitive type, it will automatically wrap it and if i want to use the returned primitive value i have to call xxxValue() on the returned object....seems a lot of overheads, isn't it?)
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a method takes an argument of primitive type, you have to pass in a wrapper for that type -- i.e., int -> Integer, boolean -> Boolean.
Regarding the "overhead" question: no, not really, not so much. Compared to the amount of computation that calling a method reflectively involves, creating an Integer object is pretty trivial.
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
If a method takes an argument of primitive type, you have to pass in a wrapper for that type -- i.e., int -> Integer, boolean -> Boolean.

just to clarify my understanding, for the statement above, does it mean that:
If a method is possibly to be invoked by reflection, it must accepts reference type parameters only.
By my experiment, it turned out to be so.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that's not what I said at all. If you have a method
void foo(int i, boolean b);
then the argument array you pass to Method.invoke() should contain a java.lang.Integer and a java.lang.Boolean. The JVM will then automatically "unwrap" the arguments to invoke the method.
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, then how do i retrive the Method in the first place?
let's say that there is a static method test in class T
public static int test(int i, int j){return (i + j);}
the getMethod or getDeclaredMethod have a similar signature as below:
public Method getMethod(String name,
Class[] parameterTypes)
throws NoSuchMethodException,
SecurityException
how do i specify "parameterTypes"....
Class c = Class.forName("T");
Method m = c.getDeclaredMethod("test", new Class[]{what's here?});
m.invoke(null, new Integer[]{new Integer(2),new Integer(3)});
[ August 29, 2003: Message edited by: Yi Meng ]
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got answer myself. now the whole testing program is like
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's really amazing that there is something called int.class .....
Thanks Ernest Friedman-Hill
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!