aspose file tools
The moose likes Java in General and the fly likes Reflections:  Method.invoke + complex and primitive args Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Reflections:  Method.invoke + complex and primitive args" Watch "Reflections:  Method.invoke + complex and primitive args" New topic
Author

Reflections: Method.invoke + complex and primitive args

Michael Herrmann
Ranch Hand

Joined: Dec 06, 2003
Posts: 60
Method.invoke(Object obj, Object[] args) allows to dynamically invoke a method on an object. But what if the method to be invoked looks like, for instance

public void myMethod(Object arg1, int i) {}

? Is it really true that, because int is not a subclass of Object, you cannot dynamically call methods that use complex as well as primitive datatypes for their parameters?

Thanks in advance.
[ September 12, 2004: Message edited by: Michael Herrmann ]
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
? Is it really true that, because int is not a subclass of Object, you cannot dynamically call methods that use complex as well as primitive datatypes for their parameters?


No, not at all. If a method has a primitive as an argument, you pass the wrapped version to method.invoke().

So, for the method: the call would look something like this.


See the documentation for method.invoke()
[ September 12, 2004: Message edited by: Sheldon Fernandes ]
Michael Herrmann
Ranch Hand

Joined: Dec 06, 2003
Posts: 60

I should have read the docu more carefully. Thank you.

Is there a similar option for Class.getMethod?(I mean this complex-primitive conversion). What I want to do is to add an assertThrows functionality to JUnit's Assert class. My current, not working approach looks like this:



Because the primitives are also represented as Class objects, Class.getMethod seems not to provide the conversion(complex->primitive) functionality. The problem is that the args above are passed in as Objects, which can be handled(=converted) by Method.invoke, but not by Class.getMethod.
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
For objects you can call the getClass() method to get the runtime class of an object. For primitives you can use the literal xxx.class

Class[] argClasses = new Class[] {obj.getClass(), int.class, Integer.class}

You will probably need to change the signature to accept Class[]. If you try to obtain it from Object[], you will not know which wrappers were actually primitives.

I hope that makes sense.

[Edit] More on the class Class
[ September 12, 2004: Message edited by: Sheldon Fernandes ]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Reflections: Method.invoke + complex and primitive args
 
Similar Threads
calling a metohd name usingn string
Reflection & primitive method parameters
Invoke a Method with primitive arguments through Reflection
Accessing methods with primitive argument via reflection
How to access set of methods in a loop