java reflection to create field/value hashmap
Field[] fields = entity.getClass().getFields();
Map<String, String> map = new HashMap<String, String>();
for(Field f : fields)
map.put(f.getName(),(String) f.get(entity));
O, and your entity should be an object of your class, not your class itself. If your fields are private and you have getters for them, you should use getMethods() and check if method name starts with "get" prefix.Like this:
Method[] methods = entity.getClass().getMethods();
Map<String, String> map = new HashMap<String, String>();
for(Method m : methods)
{
if(m.getName().startsWith("get"))
{
String value = (String) m.invoke(entity);
map.put(m.getName().substring(3), value);
}
}
If all you want to get out of this is a map, why not use a literary like Jackson, then you can simply convert it like this
Map map = new ObjectMapper().convertValue(object, Map.class);