Getting fields information using java reflection -
i working on program print information related object, information includes:
for each slice (class in object hierarchy) in x:
print class name of slice.
for each non-static field in slice:
- print field modifiers followed field name.
- print value of field follows:
- if field null print null.
- if field primitive type print value
- if field string print value
- if field array:
- loop on elements in array
- print index
- recursively print details of array item
- otherwise, recursively print details of field
i have pretty printed information using code below:
public void print(object obj) { class cl = obj.getclass(); while(cl != null) { system.out.println("class name: " + cl.getname()); field[] fields = cl.getfields(); system.out.println("fields: "); for(int i=0; i<fields.length; i++) { string modifier = modifier.tostring(fields[i].getmodifiers()); string name = fields[i].getname(); system.out.print("modifier: "+modifier+" name: "+name+ " "); try { if(fields[i].get(cl) == null) system.out.print("value: null"); } catch ( illegalargumentexception | illegalaccessexception ex) { logger.getlogger(jcp.class.getname()).log(level.severe, null, ex); } if(fields[i].gettype().isprimitive()) try { system.out.println("value: " + fields[i].get(cl)); } catch ( illegalargumentexception | illegalaccessexception ex) { logger.getlogger(jcp.class.getname()).log(level.severe, null, ex); } if(fields[i].gettype().isarray()) { try { object[] object=(object[])fields[i].get(obj); system.out.print("values: "); for(int j=0;j<object.length;j++){ system.out.print(object[j].tostring() + ","); } } catch ( illegalargumentexception | illegalaccessexception ex) { logger.getlogger(jcp.class.getname()).log(level.severe, null, ex); } } } system.out.println(); cl = cl.getsuperclass(); } }
on testing on following class:
public class hello { private int privatefield1; private long privatefield2; public int publicfield1; public int publicfield2; public hello() { } }
i got following output:
class name: hello fields: mar 10, 2014 2:54:36 pm jcp print severe: null java.lang.illegalargumentexception: can not set int field hello.publicfield1 java.lang.class @ sun.reflect.unsafefieldaccessorimpl.throwsetillegalargumentexception(unsafefieldaccessorimpl.java:164) modifier: public name: publicfield1 modifier: public name: publicfield2 class name: java.lang.object fields: @ sun.reflect.unsafefieldaccessorimpl.throwsetillegalargumentexception(unsafefieldaccessorimpl.java:168) @ sun.reflect.unsafefieldaccessorimpl.ensureobj(unsafefieldaccessorimpl.java:55) @ sun.reflect.unsafeintegerfieldaccessorimpl.getint(unsafeintegerfieldaccessorimpl.java:56) @ sun.reflect.unsafeintegerfieldaccessorimpl.get(unsafeintegerfieldaccessorimpl.java:36) @ java.lang.reflect.field.get(field.java:379) @ jcp.print(jcp.java:211) @ jcp.start(jcp.java:36) @ main.main(main.java:11) mar 10, 2014 2:54:36 pm jcp print severe: null java.lang.illegalargumentexception: can not set int field hello.publicfield1 java.lang.class @ sun.reflect.unsafefieldaccessorimpl.throwsetillegalargumentexception(unsafefieldaccessorimpl.java:164) @ sun.reflect.unsafefieldaccessorimpl.throwsetillegalargumentexception(unsafefieldaccessorimpl.java:168) @ sun.reflect.unsafefieldaccessorimpl.ensureobj(unsafefieldaccessorimpl.java:55) @ sun.reflect.unsafeintegerfieldaccessorimpl.getint(unsafeintegerfieldaccessorimpl.java:56) @ sun.reflect.unsafeintegerfieldaccessorimpl.get(unsafeintegerfieldaccessorimpl.java:36) @ java.lang.reflect.field.get(field.java:379) @ jcp.print(jcp.java:219) @ jcp.start(jcp.java:36) @ main.main(main.java:11) mar 10, 2014 2:54:36 pm jcp print severe: null java.lang.illegalargumentexception: can not set int field hello.publicfield2 java.lang.class @ sun.reflect.unsafefieldaccessorimpl.throwsetillegalargumentexception(unsafefieldaccessorimpl.java:164) @ sun.reflect.unsafefieldaccessorimpl.throwsetillegalargumentexception(unsafefieldaccessorimpl.java:168) @ sun.reflect.unsafefieldaccessorimpl.ensureobj(unsafefieldaccessorimpl.java:55) @ sun.reflect.unsafeintegerfieldaccessorimpl.getint(unsafeintegerfieldaccessorimpl.java:56) @ sun.reflect.unsafeintegerfieldaccessorimpl.get(unsafeintegerfieldaccessorimpl.java:36) @ java.lang.reflect.field.get(field.java:379) @ jcp.print(jcp.java:211) @ jcp.start(jcp.java:36) @ main.main(main.java:11) mar 10, 2014 2:54:36 pm jcp print severe: null java.lang.illegalargumentexception: can not set int field hello.publicfield2 java.lang.class @ sun.reflect.unsafefieldaccessorimpl.throwsetillegalargumentexception(unsafefieldaccessorimpl.java:164) @ sun.reflect.unsafefieldaccessorimpl.throwsetillegalargumentexception(unsafefieldaccessorimpl.java:168) @ sun.reflect.unsafefieldaccessorimpl.ensureobj(unsafefieldaccessorimpl.java:55) @ sun.reflect.unsafeintegerfieldaccessorimpl.getint(unsafeintegerfieldaccessorimpl.java:56) @ sun.reflect.unsafeintegerfieldaccessorimpl.get(unsafeintegerfieldaccessorimpl.java:36) @ java.lang.reflect.field.get(field.java:379) @ jcp.print(jcp.java:219) @ jcp.start(jcp.java:36) @ main.main(main.java:11)
mainly throwing exceptions @ "fields[i].get(cl)", kindly me figuring out problem in getting field values.
you have use:
fields[i].get(obj)
instead of:
fields[i].get(cl)
because trying value of particular instance of class, not class itself.
Comments
Post a Comment