Primitive vs Object return Type (using Reflection)

Hi

I have a Method object and I call getReturnType() to give me the class of what it returns. I then need to see if the value returned is equal to another value or not, using .equals() preferably.

The trouble is, a lot of the time the return type is a primitive, say 'int' and so I need to be able to distinguish when to use '==' and when to use .equals(). Is there any way of making sure I always end up with an Object that I can use with .equals().

Please help!

[500 byte] By [rgunder3] at [2007-9-26 2:47:03]
# 1
When using reflection to invoke a method then returned type is already wrapped in an object, so can you please expand on the problem
scottcoleman1973 at 2007-6-29 10:31:15 > top of Java-index,Core,Core APIs...
# 2
Depends on what you are talking about.You could use Class to get the function signature of the method and parse it.You could just insist that methods return objects rather than primitive (Integer rather than int.)
jschell at 2007-6-29 10:31:15 > top of Java-index,Core,Core APIs...
# 3
getReturnType() returns a "java.lang.Class" object - so if a method returns an int then the getReturnType() will return the class object defined by bothInteger.TYPE;int.class;
oxbow_lakes at 2007-6-29 10:31:15 > top of Java-index,Core,Core APIs...
# 4

Thanks scottcoleman1973 for pointing that out - I was assuming that it would return an int so a piece of code such as

int x;

Integer y;

if (x.equals(y)) ....

would not work, but if x really comes back as an Integer that's great. I've just accidentally stumbled on the same point myself and in doing so think I've solved another problem I was having (dynamic casting) by just treating everything as an Object. Not sure if this is a good way to do it but (fingers crossed!) it seems to be holding together.

Thanks for all your replies everyone!

rgunder3 at 2007-6-29 10:31:15 > top of Java-index,Core,Core APIs...