Obtains all property descriptors from a bean (interface or implementation). : Java Beans « Language Basics « Java






Obtains all property descriptors from a bean (interface or implementation).

        
//package com.witframework.util;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Obtains all property descriptors from a bean (interface or implementation).
 * <p>
 * The java.beans.Introspector does not process the interfaces hierarchy chain, this one does.
 * <p>
 * @author Alejandro Abdelnur
 *
 */
public class BeanIntrospector {

    private static final Map _introspected = new HashMap();

    public static synchronized PropertyDescriptor[] getPropertyDescriptors(Class klass) throws IntrospectionException {
        PropertyDescriptor[] descriptors = (PropertyDescriptor[]) _introspected.get(klass);
        if (descriptors==null) {
            descriptors = getPDs(klass);
            _introspected.put(klass,descriptors);
        }
        return descriptors;
    }

    private static PropertyDescriptor[] getPDs(Class klass) throws IntrospectionException {
        Method[] methods = klass.getMethods();
        Map getters = getPDs(methods,false);
        Map setters = getPDs(methods,true);
        List pds     = merge(getters,setters);
        PropertyDescriptor[] array = new PropertyDescriptor[pds.size()];
        pds.toArray(array);
        return array;
    }

    private static final String SETTER = "set";
    private static final String GETTER = "get";
    private static final String BOOLEAN_GETTER = "is";

    private static Map getPDs(Method[] methods,boolean setters) throws IntrospectionException {
        Map pds = new HashMap();
        for (int i=0;i<methods.length;i++) {
            String pName = null;
            PropertyDescriptor pDescriptor = null;
            if ((methods[i].getModifiers()&Modifier.PUBLIC)!=0) {
                if (setters) {
                    if (methods[i].getName().startsWith(SETTER) &&
                        methods[i].getReturnType()==void.class && methods[i].getParameterTypes().length==1) {
                        pName = Introspector.decapitalize(methods[i].getName().substring(3));
                        pDescriptor = new PropertyDescriptor(pName,null,methods[i]);
                    }
                }
                else {
                    if (methods[i].getName().startsWith(GETTER) &&
                        methods[i].getReturnType()!=void.class && methods[i].getParameterTypes().length==0) {
                        pName = Introspector.decapitalize(methods[i].getName().substring(3));
                        pDescriptor = new PropertyDescriptor(pName,methods[i],null);
                    }
                    else
                    if (methods[i].getName().startsWith(BOOLEAN_GETTER) &&
                        methods[i].getReturnType()==boolean.class && methods[i].getParameterTypes().length==0) {
                        pName = Introspector.decapitalize(methods[i].getName().substring(2));
                        pDescriptor = new PropertyDescriptor(pName,methods[i],null);
                    }
                }
            }
            if (pName!=null) {
                pds.put(pName,pDescriptor);
            }
        }
        return pds;
    }

    private static List merge(Map getters,Map setters) throws IntrospectionException {
        List props = new ArrayList();
        Set processedProps = new HashSet();
        Iterator gs = getters.keySet().iterator();
        while (gs.hasNext()) {
            String name = (String) gs.next();
            PropertyDescriptor getter = (PropertyDescriptor) getters.get(name);
            PropertyDescriptor setter = (PropertyDescriptor) setters.get(name);
            if (setter!=null) {
                processedProps.add(name);
                PropertyDescriptor prop = new PropertyDescriptor(name,getter.getReadMethod(),setter.getWriteMethod());
                props.add(prop);
            }
            else {
                props.add(getter);
            }
        }
        Set writeOnlyProps = new HashSet(setters.keySet());
        writeOnlyProps.removeAll(processedProps);
        Iterator ss = writeOnlyProps.iterator();
        while (ss.hasNext()) {
            String name = (String) ss.next();
            PropertyDescriptor setter = (PropertyDescriptor) setters.get(name);
            props.add(setter);
        }
        return props;
    }

}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.How to create Java bean componentHow to create Java bean component
2.Simple Java bean containerSimple Java bean container
3.Use assert the verify value
4.BeanContext SupportBeanContext Support
5.BeanContext Child SupportBeanContext Child Support
6.how to use the instantiateChild() convenience method to create a bean automatically nested into a bean contexthow to use the instantiateChild() convenience method to create a bean automatically nested into a bean context
7.illustrate delivery of the BeanContextMembershipEventillustrate delivery of the BeanContextMembershipEvent
8.Creates all of the objects, a tests the service capabilitiesCreates all of the objects, a tests the service capabilities
9.A JTable subclass that displays a table of the JavaBeans properties of any specified classA JTable subclass that displays a table of the JavaBeans properties of any specified class
10.Demonstration of set functionality in beans