Search in sources :

Example 76 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project nebula.widgets.nattable by eclipse.

the class ReflectiveColumnPropertyAccessor method setDataValue.

@Override
public void setDataValue(R rowObj, int columnIndex, Object newValue) {
    try {
        PropertyDescriptor propertyDesc = getPropertyDescriptor(rowObj, columnIndex);
        Method writeMethod = propertyDesc.getWriteMethod();
        if (writeMethod == null) {
            throw new RuntimeException(// $NON-NLS-1$
            "Setter method not found in backing bean for value at column index: " + columnIndex);
        }
        writeMethod.invoke(rowObj, newValue);
    } catch (IllegalArgumentException ex) {
        // $NON-NLS-1$
        LOG.error("Data type being set does not match the data type of the setter method in the backing bean", ex);
    } catch (Exception e) {
        LOG.error(e);
        // $NON-NLS-1$
        throw new RuntimeException("Error while setting data value");
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method) IntrospectionException(java.beans.IntrospectionException)

Example 77 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project nebula.widgets.nattable by eclipse.

the class ReflectiveColumnPropertyAccessor method getPropertyDescriptor.

/**
 * @param rowObj
 *            The Java Bean for which the {@link PropertyDescriptor} is
 *            requested.
 * @param propertyName
 *            The name of the property for which the {@link PropertyDescriptor}
 *            is requested.
 * @return The {@link PropertyDescriptor} that describes the property with the
 *         given name in the given Java Bean object that exports it via a pair
 *         of accessor methods.
 * @throws IntrospectionException
 *             if an exception occurs during introspection
 * @since 1.6
 */
protected PropertyDescriptor getPropertyDescriptor(Object rowObj, String propertyName) throws IntrospectionException {
    synchronized (rowObj) {
        Map<String, PropertyDescriptor> descriptorMap = this.propertyDescriptorMap.get(rowObj.getClass());
        if (descriptorMap == null) {
            PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(rowObj.getClass()).getPropertyDescriptors();
            Map<String, PropertyDescriptor> propertiesByAttribute = new HashMap<String, PropertyDescriptor>();
            for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                propertiesByAttribute.put(propertyDescriptor.getName(), propertyDescriptor);
            }
            descriptorMap = propertiesByAttribute;
            this.propertyDescriptorMap.put(rowObj.getClass(), propertiesByAttribute);
        }
        return descriptorMap.get(propertyName);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap)

Example 78 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project nebula.widgets.nattable by eclipse.

the class HierarchicalHelper method getPropertyDescriptor.

/**
 * Retrieves the {@link PropertyDescriptor} for the given object and
 * property name.
 *
 * @param rowObj
 *            The object for which the {@link PropertyDescriptor} is
 *            requested.
 * @param propertyName
 *            The name of the property for which the
 *            {@link PropertyDescriptor} is requested.
 * @param propertyDescriptorMap
 *            The map of already identified {@link PropertyDescriptor}s, to
 *            avoid multiple lookups via reflection.
 * @return The {@link PropertyDescriptor} for the property with the given
 *         name in the given object.
 * @throws IntrospectionException
 *             if an exception occurs during introspection
 */
private static PropertyDescriptor getPropertyDescriptor(Object rowObj, String propertyName, Map<Class<?>, Map<String, PropertyDescriptor>> propertyDescriptorMap) throws IntrospectionException {
    synchronized (rowObj) {
        Map<String, PropertyDescriptor> descriptorMap = propertyDescriptorMap.get(rowObj.getClass());
        if (descriptorMap == null) {
            PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(rowObj.getClass()).getPropertyDescriptors();
            Map<String, PropertyDescriptor> propertiesByAttribute = new HashMap<String, PropertyDescriptor>();
            for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                propertiesByAttribute.put(propertyDescriptor.getName(), propertyDescriptor);
            }
            descriptorMap = propertiesByAttribute;
            propertyDescriptorMap.put(rowObj.getClass(), propertiesByAttribute);
        }
        return descriptorMap.get(propertyName);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 79 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project nebula.widgets.nattable by eclipse.

the class HierarchicalReflectiveColumnPropertyAccessor method setDataValue.

@Override
public void setDataValue(HierarchicalWrapper rowObj, int columnIndex, Object newValue) {
    String propertyName = getColumnProperty(columnIndex);
    String[] split = propertyName.split(HierarchicalHelper.PROPERTY_SEPARATOR_REGEX);
    Object levelObject = rowObj.getObject(split.length - 1);
    if (levelObject != null) {
        try {
            PropertyDescriptor propertyDesc = getPropertyDescriptor(levelObject, split[split.length - 1]);
            Method writeMethod = propertyDesc.getWriteMethod();
            if (writeMethod == null) {
                throw new RuntimeException(// $NON-NLS-1$
                "Setter method not found in backing bean for value at column index: " + columnIndex);
            }
            writeMethod.invoke(levelObject, newValue);
        } catch (IllegalArgumentException ex) {
            // $NON-NLS-1$
            LOG.error("Data type being set does not match the data type of the setter method in the backing bean", ex);
        } catch (Exception e) {
            LOG.error(e);
            // $NON-NLS-1$
            throw new RuntimeException("Error while setting data value");
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method)

Example 80 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project nebula.widgets.nattable by eclipse.

the class HierarchicalReflectiveColumnPropertyAccessor method getDataValue.

@Override
public Object getDataValue(HierarchicalWrapper rowObj, int columnIndex) {
    String propertyName = getColumnProperty(columnIndex);
    String[] split = propertyName.split(HierarchicalHelper.PROPERTY_SEPARATOR_REGEX);
    Object levelObject = rowObj.getObject(split.length - 1);
    if (levelObject != null) {
        try {
            PropertyDescriptor propertyDesc = getPropertyDescriptor(levelObject, split[split.length - 1]);
            Method readMethod = propertyDesc.getReadMethod();
            return readMethod.invoke(levelObject);
        } catch (Exception e) {
            LOG.warn(e);
            throw new RuntimeException(e);
        }
    }
    return null;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method)

Aggregations

PropertyDescriptor (java.beans.PropertyDescriptor)836 Method (java.lang.reflect.Method)396 BeanInfo (java.beans.BeanInfo)339 IntrospectionException (java.beans.IntrospectionException)188 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)171 SimpleBeanInfo (java.beans.SimpleBeanInfo)142 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)141 InvocationTargetException (java.lang.reflect.InvocationTargetException)108 ArrayList (java.util.ArrayList)90 HashMap (java.util.HashMap)66 Field (java.lang.reflect.Field)59 Map (java.util.Map)52 Test (org.junit.Test)39 IOException (java.io.IOException)34 MockJavaBean (org.apache.harmony.beans.tests.support.mock.MockJavaBean)34 List (java.util.List)33 HashSet (java.util.HashSet)21 Type (java.lang.reflect.Type)20 Set (java.util.Set)20 LinkedHashMap (java.util.LinkedHashMap)19