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

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Language Basics » Java Beans 




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 klassthrows IntrospectionException {
        PropertyDescriptor[] descriptors = (PropertyDescriptor[]) _introspected.get(klass);
        if (descriptors==null) {
            descriptors = getPDs(klass);
            _introspected.put(klass,descriptors);
        }
        return descriptors;
    }

    private static PropertyDescriptor[] getPDs(Class klassthrows 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 settersthrows 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 settersthrows IntrospectionException {
        List props = new ArrayList();
        Set processedProps = new HashSet();
        Iterator gs = getters.keySet().iterator();
        while (gs.hasNext()) {
            String name = (Stringgs.next();
            PropertyDescriptor getter = (PropertyDescriptorgetters.get(name);
            PropertyDescriptor setter = (PropertyDescriptorsetters.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 = (Stringss.next();
            PropertyDescriptor setter = (PropertyDescriptorsetters.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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.