Created
July 11, 2014 15:47
-
-
Save scottcagno/2c8a539f2eecadd75b14 to your computer and use it in GitHub Desktop.
Java Object To Map via introspection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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