Skip to content

Instantly share code, notes, and snippets.

@scottcagno
Created July 11, 2014 15:47
Show Gist options
  • Select an option

  • Save scottcagno/2c8a539f2eecadd75b14 to your computer and use it in GitHub Desktop.

Select an option

Save scottcagno/2c8a539f2eecadd75b14 to your computer and use it in GitHub Desktop.
Java Object To Map via introspection
public static Map<String, Object> introspect(Object obj) throws Exception {
Map<String, Object> result = new HashMap<String, Object>();
BeanInfo info = Introspector.getBeanInfo(obj.getClass());
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
Method reader = pd.getReadMethod();
if (reader != null)
result.put(pd.getName(), reader.invoke(obj));
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment