Java tutorial
//package com.java2s; import java.awt.Component; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import javax.swing.JComponent; public class Main { /** * Exclude methods that return values that are meaningless to the user */ static Set<String> setExclude = new HashSet<String>(); /** * Convenience method for obtaining most non-null human readable properties * of a JComponent. Array properties are not included. * <P> * Implementation note: The returned value is a HashMap. This is subject to * change, so callers should code against the interface Map. * * @param component * the component whose proerties are to be determined * @return the class and value of the properties */ public static Map<Object, Object> getProperties(final JComponent component) { final Map<Object, Object> retVal = new HashMap<Object, Object>(); final Class<?> clazz = component.getClass(); final Method[] methods = clazz.getMethods(); Object value = null; for (final Method method : methods) { if (method.getName().matches("^(is|get).*") && method.getParameterTypes().length == 0) { try { final Class returnType = method.getReturnType(); if (returnType != void.class && !returnType.getName().startsWith("[") && !setExclude.contains(method.getName())) { final String key = method.getName(); value = method.invoke(component); if (value != null && !(value instanceof Component)) { retVal.put(key, value); } } // ignore exceptions that arise if the property could not be // accessed } catch (final IllegalAccessException ex) { } catch (final IllegalArgumentException ex) { } catch (final InvocationTargetException ex) { } } } return retVal; } }