将所有get *方法的结果映射到地图。
/* * #! * Ontopia Engine * #- * Copyright (C) 2001 - 2014 The Ontopia Project * #- * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * !# *//**时 代 J a v a 公 众 号 - nowjava.com**/ //package com.nowjava; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class Main { /** * Maps all get* methods results to a map. * Replacement for BeanMap to avoid big dependency. * @since %NEXT% */ public static Map<String, String> beanMap(Object bean, boolean sorted) { Map<String, String> map = sorted ? new TreeMap<String, String>( String.CASE_INSENSITIVE_ORDER) : new HashMap<String, String>(); for (Method method : bean.getClass().getMethods()) { if (method.getName().startsWith("get") && !method.getName().equals("getClass") /** from * nowjava.com - 时 代 Java **/ && (method.getReturnType() != null)) { try { Object o = method.invoke(bean); String name = method.getName().substring(3); n