提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
Java Bean映射
//package com.nowjava; /**来自 时 代 J a v a - nowjava.com**/ 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 = "nowjava.com"; System.out.println(Bean2Map(javaBean)); } @SuppressWarnings("unchecked")//时代Java 提 供 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(javaBe