I don't know whether the attached is of any interest, but it dynamically generates an XMLClassDescriptor from a BeanInfo. Feel free to do whatever you wish with it. The code worked well enough for me to test some beans, but I haven't tested it beyond that. In case there can be any doubt - I accept no responsibility for any consequence of any utilization of the code.

If you add the following code snippet into:
Introspector.generateClassDescriptor(..);
just before the default introspector, then it will use the info from the BeanInfo instead of a raw introspection.


//-- handle BeanInfos
XMLClassDescriptor fromBeanInfoDesc = getDescriptorFromBeanInfo(c);
if (fromBeanInfoDesc != null ) {
 return fromBeanInfoDesc;
}


Jim


-- Jim Redman (505) 662 5156 x85 http://www.ergotech.com
    /** This is a hashtable of classes vs. descriptors for beaninfo generated 
descriptors. */
    protected static Hashtable beanInfoGenerated = new Hashtable(19);
    
    /** Returns a class descriptor from a beaninfo. */
    public XMLClassDescriptor  getDescriptorFromBeanInfo( Class c ) {
        BeanInfo bi = null;
        XMLClassDescriptorImpl classDesc  = 
(XMLClassDescriptorImpl)beanInfoGenerated.get(c);
        if (classDesc == null ) {
                // attempt to specifically load the beaninfo class
                // we do this because we want the real bean info. 
                // If the beaninfo does not exist, then the introspection
                // will be performed by the standard Castor Introspection, not the 
JavaBean introspection.
                try {
                        String beanInfoClassName = c.getName() + "BeanInfo";
                        Class beanInfoClass = Class.forName(beanInfoClassName, true, 
c.getClassLoader());
                        bi = (BeanInfo)beanInfoClass.newInstance();
                        BeanDescriptor bd =  bi.getBeanDescriptor();
                        String beanDescription = bd.getDisplayName();
                        PropertyDescriptor[] propertyDescriptors = 
bi.getPropertyDescriptors();
                        // if we have managed to get this far, then the introspection 
is probably going to succeed, so we'll create the
                        // ClassDescriptor
                        classDesc  = new IntrospectedXMLClassDescriptor(c);
                beanInfoGenerated.put(c, classDesc);
                        
                        for ( int counter = 0 ; counter < propertyDescriptors.length ; 
counter++ ) {
                                PropertyDescriptor propertyDescriptor = 
propertyDescriptors[counter];
                                // I don't understand how a property descriptor can be 
missing a read or a write method, but is seems that
                                // some are missing at least the write method.  These 
will be ignored.
                                if ( propertyDescriptor.getWriteMethod() != null && 
propertyDescriptor.getReadMethod() != null) {
                                        //-- create XMLFieldDescriptor
                                        String fieldName = 
propertyDescriptor.getName();
                                        System.out.println ("Adding \"" + fieldName);
                                        String xmlName = _naming.toXMLName(fieldName);
                                        Class type = 
propertyDescriptor.getReadMethod().getReturnType();
                                        XMLFieldDescriptorImpl fieldDesc = 
createFieldDescriptor(type, fieldName, xmlName);
                                        // create the handler
                                        TypeInfo typeInfo = new TypeInfo(c);
                                        FieldHandler h = new 
FieldHandlerImpl(fieldName, null, null, propertyDescriptor.getReadMethod(), 
                                                        
propertyDescriptor.getWriteMethod(),  typeInfo);
                                        fieldDesc.setHandler(h);
                                        //-- add FieldDescriptor to ClassDescriptor
                                        classDesc.addFieldDescriptor(fieldDesc);
                                }
                        }
                        
                } catch ( Exception notLoadable ) {
                        // ignore the bean info need not exist.
                }
        }
        return classDesc;
    }
    

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to