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:


edit

it might worth clarifying types java.lang.character.type, etc., same thing class literals. is,

  • java.lang.character.type == char.class
  • java.lang.byte.type == byte.class
  • java.lang.short.type == short.class
  • java.lang.integer.type == int.class
  • java.lang.long.type == long.class
  • java.lang.float.type == float.class
  • java.lang.double.type == double.class

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -