Search in sources :

Example 1 with PlainProperty

use of org.directwebremoting.convert.PlainProperty in project ma-core-public by infiniteautomation.

the class H3BeanConverter method getPropertyMapFromObject.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.directwebremoting.convert.BeanConverter#getPropertyMapFromObject(
	 * java.lang.Object, boolean, boolean)
	 */
public Map getPropertyMapFromObject(Object example, boolean readRequired, boolean writeRequired) throws MarshallException {
    Class clazz = getClass(example);
    try {
        BeanInfo info = Introspector.getBeanInfo(clazz);
        PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
        Map properties = new HashMap();
        for (int i = 0; i < descriptors.length; i++) {
            PropertyDescriptor descriptor = descriptors[i];
            String name = descriptor.getName();
            // We don't marshall getClass()
            if (name.equals("class")) {
                continue;
            }
            // And this is something added by hibernate
            if (name.equals("hibernateLazyInitializer")) {
                continue;
            }
            // Access rules mean we might not want to do this one
            if (!isAllowedByIncludeExcludeRules(name)) {
                continue;
            }
            if (readRequired && descriptor.getReadMethod() == null) {
                continue;
            }
            if (writeRequired && descriptor.getWriteMethod() == null) {
                continue;
            }
            if (!assumeSession) {
                // We don't marshall un-initialized properties for
                // Hibernate3
                String propertyName = descriptor.getName();
                Method method = findGetter(example, propertyName);
                if (method == null) {
                    log.warn("Failed to find property: " + propertyName);
                    properties.put(name, new PlainProperty(propertyName, null));
                    continue;
                }
                if (!Hibernate.isPropertyInitialized(example, propertyName)) {
                    properties.put(name, new PlainProperty(propertyName, null));
                    continue;
                }
                // This might be a lazy-collection so we need to double
                // check
                Object retval = method.invoke(example, new Object[] {});
                if (!Hibernate.isInitialized(retval)) {
                    properties.put(name, new PlainProperty(propertyName, null));
                    continue;
                }
            }
            properties.put(name, new H3PropertyDescriptorProperty(descriptor));
        }
        return properties;
    } catch (Exception ex) {
        throw new MarshallException(clazz, ex);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) PlainProperty(org.directwebremoting.convert.PlainProperty) BeanInfo(java.beans.BeanInfo) Method(java.lang.reflect.Method) MarshallException(org.directwebremoting.extend.MarshallException) IntrospectionException(java.beans.IntrospectionException) MarshallException(org.directwebremoting.extend.MarshallException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

BeanInfo (java.beans.BeanInfo)1 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 PlainProperty (org.directwebremoting.convert.PlainProperty)1 MarshallException (org.directwebremoting.extend.MarshallException)1