Monday, 15 August 2011

java - determine type of class member via reflection -



java - determine type of class member via reflection -

i using next code determine class fellow member type ,i've seen introspection methods haven't quite figured out how type such string ,bigdecimal ,date etc. in code can see check primitive , phone call method handle it. 1. want same string ,bigdecimal ,fieldtypename,java.util.date etc how should ?

how type called?

for generic type such joblist utilize generictype handler...

object newinstance = clsobj.getclass().newinstance(); (field field : declaredfields) { // fellow member name & types class<?> fieldtype = field.gettype(); type generictype = field.getgenerictype(); string fieldtypename = fieldtype.getname(); string membername = field.getname(); boolean primitive = field.gettype().isprimitive(); handleprimtive();

if (generictype instanceof parameterizedtype) {

this class

@entity public class person { @id @generatedvalue(strategy = generationtype.table) private int id; private string firstname; private bigdecimal bigdecimal; private java.util.date mydate; private list<job> joblist = new arraylist<job>();

class <?> type = field.gettype (); if (string.class.equals (type)) handlestring (); else if (date.class.equals (type)) handledate (); ...

java reflection types type-conversion

No comments:

Post a Comment