Example usage for org.apache.commons.beanutils MappedPropertyDescriptor getName

List of usage examples for org.apache.commons.beanutils MappedPropertyDescriptor getName

Introduction

In this page you can find the example usage for org.apache.commons.beanutils MappedPropertyDescriptor getName.

Prototype

public String getName() 

Source Link

Document

Gets the programmatic name of this feature.

Usage

From source file:no.sesat.search.datamodel.DataModelFactoryImplTest.java

/**
 *
 * @throws java.lang.Exception//from  w w w.ja v  a 2s .c om
 */
@Test
public void testDataObjectGetters() throws Exception {

    LOG.info("testDataObjectGetters()");
    scan(DataObject.class, DataModel.class, new Command() {
        public void execute(Object... args) {

            try {
                final Class<?> cls = (Class<?>) args[0];
                final Object dataObject = testInstantiate(cls);

                final PropertyDescriptor[] properties = Introspector.getBeanInfo(cls).getPropertyDescriptors();
                for (PropertyDescriptor property : properties) {
                    if (null != property.getReadMethod()) {
                        final Object value = invoke(property.getReadMethod(), dataObject, new Object[0]);
                        LOG.info("        Getter on " + property.getName() + " returned " + value);
                    }
                    if (property instanceof MappedPropertyDescriptor) {
                        final MappedPropertyDescriptor mappedProperty = (MappedPropertyDescriptor) property;
                        if (null != mappedProperty.getReadMethod()) {
                            final Object value = invoke(mappedProperty.getMappedReadMethod(), dataObject, "");
                            LOG.info("        Getter on " + mappedProperty.getName() + " returned " + value);
                        }
                    }
                }
            } catch (IntrospectionException ie) {
                LOG.info(ie.getMessage(), ie);
                throw new RuntimeException(ie.getMessage(), ie);
            }
        }
    });
}

From source file:no.sesat.search.datamodel.generic.MapDataObjectBeanInfo.java

/** Work around for 4984912.
 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4984912
 * TODO remove with java6//from ww  w  .  j  a  v  a 2  s .c om
 **/
private static void fixForJavaBug4984912(final Class<?> cls, final MappedPropertyDescriptor mpd)
        throws IntrospectionException {

    try {
        final String name = mpd.getName();
        final String captialised = Character.toUpperCase(name.charAt(0)) + name.substring(1);
        Method m = null;
        try {
            // try plural with "s" first, then fall back onto "es"
            m = cls.getMethod("get" + captialised + 's', new Class[0]);
        } catch (NoSuchMethodException nsme) {
            // who on earth designed the english language!?? :@
            m = cls.getMethod("get" + captialised + "es", new Class[0]);
        }
        mpd.setReadMethod(m);

    } catch (NoSuchMethodException ex) {
        LOG.error(ex.getMessage(), ex);
    } catch (SecurityException ex) {
        LOG.error(ex.getMessage(), ex);
    }
}