Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Reflection & primitive method parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reflection & primitive method parameters" Watch "Reflection & primitive method parameters" New topic

Reflection & primitive method parameters

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 776

In trying to invoke a method via reflection; I discovered
that it's not possible to invoke a method that accepts
primitive parameters.

Method.invoke() accepts two params:
Object & Object[]
so the Method object returned will always match the 2nd burp().
Please confirm!


David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Been a while but I think it should be possible.
Why not do it the other way around:
use reflection to get a list of the Method objects, then look at each Method and find out it getParameterTypes? You'll get two methods, you can get the array of classes they accept then you should be able to reverse engineer your own answer...
If you do get a solution, post some code for us all to see how to do it...
(if this doesn't work, it may allow you to get a particular 'instance' of a mthod so you can explicitly call that one)
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 776

I finally found out a way to get hold of the method with
the primitive param.

To get a reference to the first burp() method,

The trick is that Integer.TYPE is of type Class which
represents primitive variables.
However this doesn't solve my problem, as I still can't
Method.invoke( .. );
[This message has been edited by Pho Tek (edited August 15, 2001).]
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 776

Surprisingly it works when I call the invoke method
with an Integer[] as the 2nd parameter.

I agree. Here's the link:
subject: Reflection & primitive method parameters
It's not a secret anymore!