Hi all,

I've experienced proplems with beans that have a method named 'add...' which
is NOT part of the get/set/add pattern for indexed bean properties. The
current version of betwixt assumes that for every method starting with "add"
(e.g. "addFoo"), there is a matching bean property ("foo"). Being of the
kind who constantly steps into the 1% chance gap guaranteed by Murphy's law,
this broke my code, as I have an "add(..)" method in my bean.

A little modification in
org.apache.commons.betwixt.digester.XMLIntrospectorHelper did the job (see
below).

James/Martin - maybe you could update the source in this sense ?

cheers,
Diego


    public static void defaultAddMethods( XMLIntrospector introspector,
ElementDescriptor rootDescriptor, Class beanClass ) {
        // lets iterate over all methods looking for one of the form
        // add*(PropertyType)
        if ( beanClass != null ) {

         //******* START MODIFIED
         PropertyDescriptor[] propDescriptors = null;
         try {
          propDescriptors =
Introspector.getBeanInfo(beanClass).getPropertyDescriptors();
         }
         catch (IntrospectionException e) {
          log.error(e);
         }
         ArrayList propNames = new ArrayList();
         for (int i=0; i<propDescriptors.length; i++) {
          propNames.add(propDescriptors[i].getName());
         }
         //******* END MODIFIED

            Method[] methods = beanClass.getMethods();
            for ( int i = 0, size = methods.length; i < size; i++ ) {
                Method method = methods[i];
                String name = method.getName();

          //******* START MODIFIED
                String propertyName = Introspector.decapitalize(
name.substring(3) );
                boolean isBeanProperty = (propNames.indexOf(propertyName) >=
0);
                if ( name.startsWith( "add" ) && isBeanProperty) {
          //******* END MODIFIED

                    // XXX: should we filter out non-void returning methods?
                    // some beans will return something as a helper
                    Class[] types = method.getParameterTypes();
                    if ( types != null && types.length == 1 ) {

            //******* START MODIFIED
                        //String propertyName = Introspector.decapitalize(
name.substring(3) );
            //******* END MODIFIED

                        // now lets try find the ElementDescriptor which
displays
                        // a property which starts with propertyName
                        // and if so, we'll set a new Updater on it if there
                        // is not one already
                        ElementDescriptor descriptor =
findGetCollectionDescriptor( introspector, rootDescriptor, propertyName );

                            if ( log.isDebugEnabled() ) {
                                log.debug( "!! " + propertyName + " -> " +
descriptor);
                            }

                        if ( descriptor != null ) {
                            descriptor.setUpdater( new MethodUpdater(
method ) );
                            descriptor.setSingularPropertyType( types[0] );

                            if ( log.isDebugEnabled() ) {
                                log.debug( "!! " + method);
                                log.debug( "!! " + types[0]);
                            }

                            // is there a child element with no localName
                            ElementDescriptor[] children =
descriptor.getElementDescriptors();
                            if ( children != null && children.length > 0 ) {
                                ElementDescriptor child = children[0];
                                String localName = child.getLocalName();
                                if ( localName == null || localName.length()
== 0 ) {
                                    child.setLocalName(
introspector.getElementNameMapper().mapTypeToElementName( propertyName ) );
                                }
                            }
                        }
                        else {
                            if ( log.isDebugEnabled() ) {
                                log.debug(
                                    "Could not find an ElementDescriptor
with property name: "
                                    + propertyName + " to attach the add
method: " + method
                                );
                            }
                        }
                    }
                }
            }
        }
    }


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to