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
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