Reflection question: int ->Integer->??

T

timjowers

Using reflection I cannot get to the data value of an int member
attribute. Anyone solved this?

Here's the sample code. The issue is reflection works fine if you know
beforehand the type and can cast to an Integer but what if you do not
know? I cannot get the actual int to print! What am I missing?


public class EqualsUtil {

static void printField(Object r, String fieldName) {
Field heightField;
//Integer heightValue;
Object oValue;
Class c = r.getClass();
try {
heightField = c.getField(fieldName);
//heightValue = (Integer) heightField.get(r);
oValue = heightField.get(r);
//System.out.println(fieldName + ": " + heightValue.toString());
System.out.println(fieldName + ": " + oValue);
if( fieldName == "TYPE" && oValue.toString().equals("int")) //
special case in Java
{ ;; // where's the real value? The debugger shows the value "123"
but oValue prints as "int"
}
} catch (NoSuchFieldException e) {
System.out.println(e);
} catch (SecurityException e) {
System.out.println(e);
} catch (IllegalAccessException e) {
System.out.println(e);
}
}
public static void printObject(Object o )
{
Class c = o.getClass();
Field[] publicFields = c.getFields();
for (int i = 0; i < publicFields.length; i++) {
String fieldName = publicFields.getName();
Class typeClass = publicFields.getType();
String fieldType = typeClass.getName();
Object val=null;
System.out.println("Name: " + fieldName + ", Type: " + fieldType);
printField( o, fieldName );
}
}

public static void main(String[] args) {
Integer i1 = new Integer(123);
printObject(i1);
}
}

Note that this code is almost a duplicate of Sun's tutorial. See:
http://java.sun.com/docs/books/tutorial/reflect/object/get.html
I've also tried with getDeclaredFields but no luck. What does work is
creating an isntance of an innerclass with a memeber attribute of
Integer (or else even of int) as these both print the value fine using
reflection.

Thanks,
TimJowers
 
O

Oliver Wong

Using reflection I cannot get to the data value of an int member
attribute. Anyone solved this?

Here's the sample code. The issue is reflection works fine if you know
beforehand the type and can cast to an Integer but what if you do not
know? I cannot get the actual int to print! What am I missing?

I'm not sure I understand the question: Do you want a general way to
print fields which may be of any primitive type or what? Because the code
you're working with seems to half of the time not making any assumption
about the type of the field, and the other half of the time, assumes the
type of the field is an int. Do you want to print ALL objects as if they
were primitive ints? Do you want to print all objects, regardless of whether
they are ints or not? What exactly are you trying to do?

- Oliver
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top