java - find fields of type String, Boolean, Integer using reflection -
is there way find fields in class of type
java.lang.character.type java.lang.byte.type java.lang.short.type java.lang.integer.type java.lang.long.type java.lang.float.type java.lang.double.type there isprimitive method char, byte, short etc.
you can start class#getdeclaredfields() array of fields in class. then, iterate on each field in array , filter needed.
something this:
public static list<field> getprimitivefields(class<?> clazz) { list<field> toreturn = new arraylist<field>(); field[] allfields = clazz.getdeclaredfields(); (field f : allfields) { class<?> type = f.gettype(); if (type.isprimitive()) { toreturn.add(f); } } return toreturn; } more info:
field#gettype()class#isprimitive()(though sounds know one)
edit
it might worth clarifying types java.lang.character.type, etc., same thing class literals. is,
java.lang.character.type == char.classjava.lang.byte.type == byte.classjava.lang.short.type == short.classjava.lang.integer.type == int.classjava.lang.long.type == long.classjava.lang.float.type == float.classjava.lang.double.type == double.class
Comments
Post a Comment