Handling Primitive Types when invoking a method

G

garfield

Here is my code (condensed):


import java.lang.reflect.*;

public class TestMethodInvocation { // 1

public static final int TESTINT = 4; // 2

public static void main(String args[]) { // 3

try {
TestMethodInvocation tmi = new TestMethodInvocation(); // 4

Object objList[] = new Object[] { tmi.getClass()
.getDeclaredField("TESTINT").get(null) }; // 5

Class clsList[] = new Class[objList.length]; // 6
for ( int i=0; i<objList.length; i++ ) {
clsList = objList.getClass(); // 7
}

Method method = tmi.getClass().getMethod("methodToInvoke",
clsList); // 8
System.out.println(method.invoke(tmi, objList)); // 9

} catch (Exception e) {
System.out.println("Nope!");
}
}

public String methodToInvoke(int i) {
return "Worked!";
}
}

What is happening is this, when line 5 executes, the class for the
integer is being converted from int to Integer. When I get the class
for that value at line 7, I get class java.lang.Integer. Subsequently
when I use it to get the Method object at line 8, I get a
MethodNotFound (or close to it) exception.

My question: using this algorhythm, how do I convert the class type in
clsList[0] from Integer.class to int.class so that I can find the
method?

Note: I am doing this because I do not know what the object types are
in my larger implementation, I have to derive them from other sources
and this seemed to be a practical way to do it until I ran into this
problem.

Thank you for any response.
 
B

Brad BARCLAY

garfield said:
My question: using this algorhythm, how do I convert the class type in
clsList[0] from Integer.class to int.class so that I can find the
method?

"int" is a primitive type -- thus there is no such thing as
"int.class". Indeed, you can't hold an "int" on it's own in either an
Object array or in Class array. It's simply not possible.
Note: I am doing this because I do not know what the object types are
in my larger implementation, I have to derive them from other sources
and this seemed to be a practical way to do it until I ran into this
problem.

I suggest changing your code to either allow the desired method to
accept Integer, or to create an overloaded version that does, ala:

public String methodToInvoke(Integer i) {
return methodToInvoke(i.intValue());
}

Note that this is pretty much a requirement when

HTH!

Brad BARCLAY
 
C

Chris Uppal

garfield said:
My question: using this algorhythm, how do I convert the class type in
clsList[0] from Integer.class to int.class so that I can find the
method?

You don't have enough information at this point to know whether the field was
an int or an Integer; if you need that info then you have to change your
approach to call Field.getType() as well as Field.get().

-- chris
 
J

John C. Bollinger

Chris said:
garfield wrote:

My question: using this algorhythm, how do I convert the class type in
clsList[0] from Integer.class to int.class so that I can find the
method?


You don't have enough information at this point to know whether the field was
an int or an Integer; if you need that info then you have to change your
approach to call Field.getType() as well as Field.get().

To expand on that a little, both int (primitive) and Integer (object)
_values_ are represented by Integer objects in the reflection API. The
_types_, however, must be represented by different classes to
disambiguate. The class that represents the primitive int type is
Integer.TYPE. Similar applies to the other primitive types. Yes, it is
quirky that primitives' types and values are represented by different
classes.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top