调用以“ get”或“ is”开头的对象的每个方法
/* from n o w j a v a . c o m */ //package com.nowjava; import java.lang.reflect.Method; import java.util.LinkedHashMap; import java.util.Map; public class Main { /** * Call every method on object that begins with "get" or "is" * * @return map of parameter to value. */ public static Map<String, String> callEveryBeanMethodOnObject(Object o) { Map<String, String> data = new LinkedHashMap<String, String>(); for (Method m : o.getClass().getMethods()) { final String methodName = m.getName(); if ((methodName.startsWith("get") || methodName /* from n o w j a v a . c o m */ .startsWith("is")) && m.getParameterTypes().length == 0 && methodName != "getClass") { try { Object value = m.invoke(o, (Object[]) null); if (value instanceof byte[]) { // byte arrays are "special" System.err.println("\n\nI was called\n\n"); data.put(methodName, "byte[" + ((byte[]) value).length + "]"); // cast to String continue; // Skip for now. Is this causing gnome-terminal display issues? }//else