alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.xml.core.settings;

import java.beans.BeanDescriptor;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.ClassLoader;
import java.lang.reflect.Method;
import java.util.TreeMap;
import org.openide.options.ContextSystemOption;
import org.openide.options.SystemOption;
import org.openide.util.Lookup;
import org.openide.util.io.NbMarshalledObject;

/**
 * SystemOption (settings) test looking for changed options.
 * Create it, setIO then perform testSystemOption().
 *
 * @author  mryzl, Petr Kuzel
 * @version
 */
public class SystemOptionTest {
    
    private String result = "";
    
    /** Get a system option by name.
     * @param className     name of the class
     * @return              a system option
     */
    public String testSystemOption(String className) {
        result = "";
        refSystemOption(className);
        return result;
    }
    
    private void ref(String s) {
        result += s + "\n";
    }
    
    private void refSystemOption(String className) {
        // introduction
        ref("========================================");
        try {
            // get class
            ClassLoader loader = (ClassLoader) Lookup.getDefault().lookup(ClassLoader.class);
            Class clazz = loader.loadClass(className);
            ref("  className = " + clazz.getName());
            
            // get option
            SystemOption so = (SystemOption) SystemOption.findObject(clazz, true);
            
            // serializability
            NbMarshalledObject mo = new NbMarshalledObject(so);
            mo.get();
            ref("  serializable = true");
            
            // bean info
            ref("bean info:");
            BeanInfo bi = Introspector.getBeanInfo(clazz);
            ref("  class = " + bi.getClass().getName());
            
            // bean descriptor
            ref("bean descriptor:");
            BeanDescriptor bd = bi.getBeanDescriptor();
            ref("  name = " + bd.getName());
            ref("  displayName = " + bd.getDisplayName());
            ref("  shortDescription = " + bd.getShortDescription());
            ref("  hidden = " + bd.isHidden());
            ref("  customizer = " + getClassName(bd.getClass()));
            
            // properties
            PropertyDescriptor[] pds = bi.getPropertyDescriptors();
            
            // sort properties
            TreeMap smap = new TreeMap();
            for(int i = 0; i < pds.length; i++) {
                smap.put(pds[i].getName(), pds[i]);
            }
            pds = (PropertyDescriptor[]) smap.values().toArray(pds);
            
            for(int i = 0; i < pds.length; i++) {
                ref("property:");
                ref("  name = " + pds[i].getName());
                ref("  type = " + getClassName(pds[i].getPropertyType()));
                ref("  getter = " + getMethodName(pds[i].getReadMethod()));
                ref("  setter = " + getMethodName(pds[i].getWriteMethod()));
                ref("  displayName = " + pds[i].getDisplayName());
                ref("  shortDescription = " + pds[i].getShortDescription());
                ref("  hidden = " + pds[i].isHidden());
                ref("  editor = " + getClassName(pds[i].getPropertyEditorClass()));
            }
            
            ref("additional bean infos:");
            BeanInfo[] bis = bi.getAdditionalBeanInfo();
            if (bis != null) {
                for(int i = 0; i < bis.length; i++) {
                    ref("  class = " + getClassName(bis[i].getClass()));
                }
            }
            
            ref("========================================");
            
            if (so instanceof ContextSystemOption) {
                ref("context:");
                SystemOption[] sos = ((ContextSystemOption) so).getOptions();
                for(int i = 0; i < sos.length; i++) {
                    refSystemOption(getClassName(sos[i].getClass()));
                }
            }
            
            
        } catch (Exception ex) {
            StringWriter sw = new StringWriter();
            ex.printStackTrace(new PrintWriter(sw, true));
            ref("----------------------------------------");
            ref(sw.toString());
        }
    }
    
    /** Get name of the class.
     * @param clazz     class
     * @return          name of the class or null
     */
    private String getClassName(Class clazz) {
        if (clazz != null) return clazz.getName();
        return null;
    }
    
    /** Get name of the method.
     * @param clazz     class
     * @return          name of the class or null
     */
    private String getMethodName(Method method) {
        if (method != null) return method.getName();
        return null;
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.