Java examples for Reflection:Java Bean
Java Bean to Map
//package com.java2s; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] argv) throws Exception { Object javaBean = "java2s.com"; System.out.println(Bean2Map(javaBean)); }/*w w w .java2s . co m*/ @SuppressWarnings("unchecked") public static <K, V> Map<K, V> Bean2Map(Object javaBean) { Map<K, V> ret = new HashMap<K, V>(); try { Method[] methods = javaBean.getClass().getDeclaredMethods(); for (Method method : methods) { if (method.getName().startsWith("get")) { String field = method.getName(); field = field.substring(field.indexOf("get") + 3); field = field.toLowerCase().charAt(0) + field.substring(1); Object value = method.invoke(javaBean, (Object[]) null); ret.put((K) field, (V) (null == value ? "" : value)); } } } catch (Exception e) { } return ret; } }