You can start with Class#getDeclaredFields()
to get an array of the fields in your class. Then, iterate over each Field
in the array and filter as needed.
Something like this:
public static List<Field> getPrimitiveFields(Class<?> clazz)
{
List<Field> toReturn = new ArrayList<Field>();
Field[] allFields = clazz.getDeclaredFields();
for (Field f : allFields)
{
Class<?> type = f.getType();
if (type.isPrimitive())
{
toReturn.add(f);
}
}
return toReturn;
}
More info:
Edit
It might be worth clarifying that the types java.lang.Character.TYPE
, etc., are the same thing as the class literals. That 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