Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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<>();

    /**
     * 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(JComponent component) {
        Map<Object, Object> retVal = new HashMap<>();
        Class<?> clazz = component.getClass();
        Method[] methods = clazz.getMethods();
        Object value = null;
        for (Method method : methods) {
            if (method.getName().matches("^(is|get).*") && //$NON-NLS-1$
                    method.getParameterTypes().length == 0) {
                try {
                    Class<?> returnType = method.getReturnType();
                    if (returnType != void.class && !returnType.getName().startsWith("[") && //$NON-NLS-1$
                            !setExclude.contains(method.getName())) {
                        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 (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
                    //no catch
                }
            }
        }
        return retVal;
    }
}