org.apache.commons.beanutils.DynaProperty Java Examples

The following examples show how to use org.apache.commons.beanutils.DynaProperty. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SqlDynaClass.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes the primary key and non primary key property arrays.
 */
protected void initPrimaryKeys()
{
    List           pkProps    = new ArrayList();
    List           nonPkProps = new ArrayList();
    DynaProperty[] properties = getDynaProperties();

    for (int idx = 0; idx < properties.length; idx++)
    {
        if (properties[idx] instanceof SqlDynaProperty)
        {
            SqlDynaProperty sqlProperty = (SqlDynaProperty)properties[idx];

            if (sqlProperty.isPrimaryKey())
            {
                pkProps.add(sqlProperty);
            }
            else
            {
                nonPkProps.add(sqlProperty);
            }
        }
    }
    _primaryKeyProperties    = (SqlDynaProperty[])pkProps.toArray(new SqlDynaProperty[pkProps.size()]);
    _nonPrimaryKeyProperties = (SqlDynaProperty[])nonPkProps.toArray(new SqlDynaProperty[nonPkProps.size()]);
}
 
Example #2
Source File: DynaBeanPropertyPointer.java    From commons-jxpath with Apache License 2.0 6 votes vote down vote up
/**
 * Convert a value to the appropriate property type.
 * @param value to convert
 * @param element whether this should be a collection element.
 * @return conversion result
 */
private Object convert(Object value, boolean element) {
    DynaClass dynaClass = dynaBean.getDynaClass();
    DynaProperty property = dynaClass.getDynaProperty(getPropertyName());
    Class type = property.getType();
    if (element) {
        if (type.isArray()) {
            type = type.getComponentType();
        }
        else {
            return value; // No need to convert
        }
    }

    try {
        return TypeUtils.convert(value, type);
    }
    catch (Exception ex) {
        String string = value == null ? "null" : value.getClass().getName();
        throw new JXPathTypeConversionException(
                "Cannot convert value of class " + string + " to type "
                        + type, ex);
    }
}
 
Example #3
Source File: DynaBeanPropertyPointer.java    From commons-jxpath with Apache License 2.0 6 votes vote down vote up
public String[] getPropertyNames() {
    /* @todo do something about the sorting - LIKE WHAT? - MJB */
    if (names == null) {
        DynaClass dynaClass = dynaBean.getDynaClass();
        DynaProperty[] dynaProperties = dynaClass.getDynaProperties();
        ArrayList properties = new ArrayList(dynaProperties.length);
        for (int i = 0; i < dynaProperties.length; i++) {
            String name = dynaProperties[i].getName();
            if (!CLASS.equals(name)) {
                properties.add(name);
            }
        }
        names = (String[]) properties.toArray(new String[properties.size()]);
        Arrays.sort(names);
    }
    return names;
}
 
Example #4
Source File: MultiWrapDynaBean.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new instance of {@code MultiWrapDynaBean} and initializes it
 * with the given collections of beans to be wrapped.
 *
 * @param beans the wrapped beans
 */
public MultiWrapDynaBean(final Collection<?> beans)
{
    propsToBeans = new HashMap<>();
    final Collection<DynaClass> beanClasses =
            new ArrayList<>(beans.size());

    for (final Object bean : beans)
    {
        final DynaBean dynaBean = createDynaBean(bean);
        final DynaClass beanClass = dynaBean.getDynaClass();
        for (final DynaProperty prop : beanClass.getDynaProperties())
        {
            // ensure an order of properties
            if (!propsToBeans.containsKey(prop.getName()))
            {
                propsToBeans.put(prop.getName(), dynaBean);
            }
        }
        beanClasses.add(beanClass);
    }

    dynaClass = new MultiWrapDynaClass(beanClasses);
}
 
Example #5
Source File: ConfigurationDynaClass.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
@Override
public DynaProperty[] getDynaProperties()
{
    if (LOG.isTraceEnabled())
    {
        LOG.trace("getDynaProperties()");
    }

    final Iterator<String> keys = configuration.getKeys();
    final List<DynaProperty> properties = new ArrayList<>();
    while (keys.hasNext())
    {
        final String key = keys.next();
        final DynaProperty property = getDynaProperty(key);
        properties.add(property);
    }

    final DynaProperty[] propertyArray = new DynaProperty[properties.size()];
    properties.toArray(propertyArray);
    if (LOG.isDebugEnabled())
    {
        LOG.debug("Found " + properties.size() + " properties.");
    }

    return propertyArray;
}
 
Example #6
Source File: OJBUtility.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * This method builds a map of business object with its property names and values
 * 
 * @param businessObject the given business object
 * @return the map of business object with its property names and values
 */
public static LinkedHashMap buildPropertyMap(Object businessObject) {
    DynaClass dynaClass = WrapDynaClass.createDynaClass(businessObject.getClass());
    DynaProperty[] properties = dynaClass.getDynaProperties();
    LinkedHashMap propertyMap = new LinkedHashMap();

    try {
        for (int numOfProperty = 0; numOfProperty < properties.length; numOfProperty++) {
            String propertyName = properties[numOfProperty].getName();
            if (PropertyUtils.isWriteable(businessObject, propertyName)) {
                Object propertyValue = PropertyUtils.getProperty(businessObject, propertyName);
                propertyMap.put(propertyName, propertyValue);
            }
        }
    }
    catch (Exception e) {
        LOG.error("OJBUtility.buildPropertyMap()" + e);
    }
    return propertyMap;
}
 
Example #7
Source File: ObjectUtil.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * determine if the source object has a field with null as its value
 * 
 * @param sourceObject the source object
 */
public static boolean hasNullValueField(Object sourceObject) {
    DynaClass dynaClass = WrapDynaClass.createDynaClass(sourceObject.getClass());
    DynaProperty[] properties = dynaClass.getDynaProperties();

    for (DynaProperty property : properties) {
        String propertyName = property.getName();

        if (PropertyUtils.isReadable(sourceObject, propertyName)) {
            try {
                Object propertyValue = PropertyUtils.getProperty(sourceObject, propertyName);
                if (propertyValue == null) {
                    return true;
                }
            }
            catch (Exception e) {
                LOG.info(e);
                return false;
            }
        }
    }
    return false;
}
 
Example #8
Source File: ObjectUtil.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * build a map of business object with its specified property names and corresponding values
 * 
 * @param businessObject the given business object
 * @param the specified fields that need to be included in the return map
 * @return the map of business object with its property names and values
 */
public static Map<String, Object> buildPropertyMap(Object object, List<String> keyFields) {
    DynaClass dynaClass = WrapDynaClass.createDynaClass(object.getClass());
    DynaProperty[] properties = dynaClass.getDynaProperties();
    Map<String, Object> propertyMap = new LinkedHashMap<String, Object>();

    for (DynaProperty property : properties) {
        String propertyName = property.getName();

        if (PropertyUtils.isReadable(object, propertyName) && keyFields.contains(propertyName)) {
            try {
                Object propertyValue = PropertyUtils.getProperty(object, propertyName);

                if (propertyValue != null && !StringUtils.isEmpty(propertyValue.toString())) {
                    propertyMap.put(propertyName, propertyValue);
                }
            }
            catch (Exception e) {
                LOG.info(e);
            }
        }
    }
    return propertyMap;
}
 
Example #9
Source File: TestAgainstLiveDatabaseBase.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Determines the value of the bean's property that has the given name. Depending on the
 * case-setting of the current builder, the case of teh name is considered or not. 
 * 
 * @param bean     The bean
 * @param propName The name of the property
 * @return The value
 */
protected Object getPropertyValue(DynaBean bean, String propName)
{
    if (getPlatform().isDelimitedIdentifierModeOn())
    {
        return bean.get(propName);
    }
    else
    {
        DynaProperty[] props = bean.getDynaClass().getDynaProperties();

        for (int idx = 0; idx < props.length; idx++)
        {
            if (propName.equalsIgnoreCase(props[idx].getName()))
            {
                return bean.get(props[idx].getName());
            }
        }
        throw new IllegalArgumentException("The bean has no property with the name "+propName);
    }
}
 
Example #10
Source File: SqlDynaBean.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public String toString()
{
    StringBuilder   result = new StringBuilder();
    DynaClass      type   = getDynaClass();
    DynaProperty[] props  = type.getDynaProperties();

    result.append(type.getName());
    result.append(": ");
    for (int idx = 0; idx < props.length; idx++)
    {
        if (idx > 0)
        {
            result.append(", ");
        }
        result.append(props[idx].getName());
        result.append(" = ");
        result.append(get(props[idx].getName()));
    }
    return result.toString();
}
 
Example #11
Source File: TestAgainstLiveDatabaseBase.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Determines the value of the bean's property that has the given name. Depending on the
 * case-setting of the current builder, the case of teh name is considered or not. 
 * 
 * @param bean     The bean
 * @param propName The name of the property
 * @return The value
 */
protected Object getPropertyValue(DynaBean bean, String propName)
{
    if (getPlatform().isDelimitedIdentifierModeOn())
    {
        return bean.get(propName);
    }
    else
    {
        DynaProperty[] props = bean.getDynaClass().getDynaProperties();

        for (int idx = 0; idx < props.length; idx++)
        {
            if (propName.equalsIgnoreCase(props[idx].getName()))
            {
                return bean.get(props[idx].getName());
            }
        }
        throw new IllegalArgumentException("The bean has no property with the name "+propName);
    }
}
 
Example #12
Source File: SqlDynaClass.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes the primary key and non primary key property arrays.
 */
protected void initPrimaryKeys()
{
    List           pkProps    = new ArrayList();
    List           nonPkProps = new ArrayList();
    DynaProperty[] properties = getDynaProperties();

    for (int idx = 0; idx < properties.length; idx++)
    {
        if (properties[idx] instanceof SqlDynaProperty)
        {
            SqlDynaProperty sqlProperty = (SqlDynaProperty)properties[idx];

            if (sqlProperty.isPrimaryKey())
            {
                pkProps.add(sqlProperty);
            }
            else
            {
                nonPkProps.add(sqlProperty);
            }
        }
    }
    _primaryKeyProperties    = (SqlDynaProperty[])pkProps.toArray(new SqlDynaProperty[pkProps.size()]);
    _nonPrimaryKeyProperties = (SqlDynaProperty[])nonPkProps.toArray(new SqlDynaProperty[nonPkProps.size()]);
}
 
Example #13
Source File: SqlDynaBean.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public String toString()
{
    StringBuilder   result = new StringBuilder();
    DynaClass      type   = getDynaClass();
    DynaProperty[] props  = type.getDynaProperties();

    result.append(type.getName());
    result.append(": ");
    for (int idx = 0; idx < props.length; idx++)
    {
        if (idx > 0)
        {
            result.append(", ");
        }
        result.append(props[idx].getName());
        result.append(" = ");
        result.append(get(props[idx].getName()));
    }
    return result.toString();
}
 
Example #14
Source File: BeanListUnivariateImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
  *  Creates a {@link org.apache.commons.beanutils.DynaBean} with a 
  *  {@link org.apache.commons.beanutils.DynaProperty} named 
  *  <code>propertyName,</code> sets the value of the property to <code>v</code>
  *  and adds the DynaBean to the underlying list.
  *
  */
public void addValue(double v)  {
    DynaProperty[] props = new DynaProperty[] {
            new DynaProperty(propertyName, Double.class)
    };
    BasicDynaClass dynaClass = new BasicDynaClass(null, null, props);
    DynaBean dynaBean = null;
    try {
        dynaBean = dynaClass.newInstance();
    } catch (Exception ex) {              // InstantiationException, IllegalAccessException
        throw new RuntimeException(ex);   // should never happen
    }
	dynaBean.set(propertyName, Double.valueOf(v));
	addObject(dynaBean);
}
 
Example #15
Source File: SqlDynaClass.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the properties of this dyna class.
 * 
 * @return The properties
 */
public SqlDynaProperty[] getSqlDynaProperties()
{
    DynaProperty[]    props  = getDynaProperties();
    SqlDynaProperty[] result = new SqlDynaProperty[props.length];

    System.arraycopy(props, 0, result, 0, props.length);
    return result;
}
 
Example #16
Source File: TestConfigurationDynaBean.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Base for testGetDescriptorXxxxx() series of tests.
 *
 * @param name Name of the property to be retrieved
 * @param type Expected class type of this property
 */
protected void testGetDescriptorBase(final String name, final Class<?> type)
{
    final DynaProperty descriptor = bean.getDynaClass().getDynaProperty(name);

    assertNotNull("Failed to get descriptor", descriptor);
    assertEquals("Got incorrect type", type, descriptor.getType());
}
 
Example #17
Source File: TestConfigurationDynaBean.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Positive test for getDynaPropertys().  Each property name
 * listed in {@code properties} should be returned exactly once.
 */
@Test
public void testGetDescriptors()
{
    final DynaProperty pd[] = bean.getDynaClass().getDynaProperties();
    assertNotNull("Got descriptors", pd);
    final int count[] = new int[properties.length];
    for (final DynaProperty element : pd) {
        final String name = element.getName();
        for (int j = 0; j < properties.length; j++)
        {
            if (name.equals(properties[j]))
            {
                count[j]++;
            }
        }
    }

    for (int j = 0; j < properties.length; j++)
    {
        if (count[j] < 0)
        {
            fail("Missing property " + properties[j]);
        }
        else if (count[j] > 1)
        {
            fail("Duplicate property " + properties[j]);
        }
    }
}
 
Example #18
Source File: TestConfigurationDynaBean.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Corner cases on getDynaProperty invalid arguments.
 */
@Test(expected = IllegalArgumentException.class)
public void testGetDescriptorArguments()
{
    final DynaProperty descriptor = bean.getDynaClass().getDynaProperty("unknown");
    assertNull("Unknown property descriptor should be null", descriptor);
    bean.getDynaClass().getDynaProperty(null);
}
 
Example #19
Source File: SqlDynaBean.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean equals(Object obj)
{
    if (obj instanceof SqlDynaBean)
    {
        SqlDynaBean other     = (SqlDynaBean)obj;
        DynaClass   dynaClass = getDynaClass();

        if (dynaClass.equals(other.getDynaClass()))
        {
            DynaProperty[] props = dynaClass.getDynaProperties();

            for (int idx = 0; idx < props.length; idx++)
            {
                Object value      = get(props[idx].getName());
                Object otherValue = other.get(props[idx].getName());

                if (value == null)
                {
                    if (otherValue != null)
                    {
                        return false;
                    }
                }
                else
                {
                    return value.equals(otherValue);
                }
            }
            return true;
        }
    }
    return false;
}
 
Example #20
Source File: MultiWrapDynaClass.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the members related to the properties of the wrapped classes.
 *
 * @param wrappedCls the collection with the wrapped classes
 */
private void initProperties(final Collection<? extends DynaClass> wrappedCls)
{
    for (final DynaClass cls : wrappedCls)
    {
        final DynaProperty[] props = cls.getDynaProperties();
        for (final DynaProperty p : props)
        {
            properties.add(p);
            namedProperties.put(p.getName(), p);
        }
    }
}
 
Example #21
Source File: SqlDynaClass.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the properties of this dyna class.
 * 
 * @return The properties
 */
public SqlDynaProperty[] getSqlDynaProperties()
{
    DynaProperty[]    props  = getDynaProperties();
    SqlDynaProperty[] result = new SqlDynaProperty[props.length];

    System.arraycopy(props, 0, result, 0, props.length);
    return result;
}
 
Example #22
Source File: ObjectUtil.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Populate the target object with the source object
 * 
 * @param targetObject the target object
 * @param sourceObject the source object
 */
public static void buildObjectWithoutReferenceFields(Object targetObject, Object sourceObject) {
    DynaClass dynaClass = WrapDynaClass.createDynaClass(targetObject.getClass());
    DynaProperty[] properties = dynaClass.getDynaProperties();

    for (DynaProperty property : properties) {
        ObjectUtil.setProperty(targetObject, sourceObject, property, true);
    }
}
 
Example #23
Source File: ObjectUtil.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Populate the target object with the source object
 * 
 * @param targetObject the target object
 * @param sourceObject the source object
 */
public static void buildObject(Object targetObject, Object sourceObject) {
    DynaClass dynaClass = WrapDynaClass.createDynaClass(targetObject.getClass());
    DynaProperty[] properties = dynaClass.getDynaProperties();

    for (DynaProperty property : properties) {
        ObjectUtil.setProperty(targetObject, sourceObject, property, false);
    }
}
 
Example #24
Source File: SqlDynaBean.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean equals(Object obj)
{
    if (obj instanceof SqlDynaBean)
    {
        SqlDynaBean other     = (SqlDynaBean)obj;
        DynaClass   dynaClass = getDynaClass();

        if (dynaClass.equals(other.getDynaClass()))
        {
            DynaProperty[] props = dynaClass.getDynaProperties();

            for (int idx = 0; idx < props.length; idx++)
            {
                Object value      = get(props[idx].getName());
                Object otherValue = other.get(props[idx].getName());

                if (value == null)
                {
                    if (otherValue != null)
                    {
                        return false;
                    }
                }
                else
                {
                    return value.equals(otherValue);
                }
            }
            return true;
        }
    }
    return false;
}
 
Example #25
Source File: BeanListUnivariateImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
  *  Creates a {@link org.apache.commons.beanutils.DynaBean} with a 
  *  {@link org.apache.commons.beanutils.DynaProperty} named 
  *  <code>propertyName,</code> sets the value of the property to <code>v</code>
  *  and adds the DynaBean to the underlying list.
  *
  */
public void addValue(double v)  {
    DynaProperty[] props = new DynaProperty[] {
            new DynaProperty(propertyName, Double.class)
    };
    BasicDynaClass dynaClass = new BasicDynaClass(null, null, props);
    DynaBean dynaBean = null;
    try {
        dynaBean = dynaClass.newInstance();
    } catch (Exception ex) {              // InstantiationException, IllegalAccessException
        throw new RuntimeException(ex);   // should never happen
    }
	dynaBean.set(propertyName, new Double(v));
	addObject(dynaBean);
}
 
Example #26
Source File: BeanListUnivariateImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
  *  Creates a {@link org.apache.commons.beanutils.DynaBean} with a 
  *  {@link org.apache.commons.beanutils.DynaProperty} named 
  *  <code>propertyName,</code> sets the value of the property to <code>v</code>
  *  and adds the DynaBean to the underlying list.
  *
  */
public void addValue(double v)  {
    DynaProperty[] props = new DynaProperty[] {
            new DynaProperty(propertyName, Double.class)
    };
    BasicDynaClass dynaClass = new BasicDynaClass(null, null, props);
    DynaBean dynaBean = null;
    try {
        dynaBean = dynaClass.newInstance();
    } catch (Exception ex) {              // InstantiationException, IllegalAccessException
        throw new RuntimeException(ex);   // should never happen
    }
	dynaBean.set(propertyName, Double.valueOf(v));
	addObject(dynaBean);
}
 
Example #27
Source File: DynaBeanAdapter.java    From reflectutils with Apache License 2.0 5 votes vote down vote up
public Class<?> getFieldType(Object obj, String name) {
    DynaClass dynaClass = ((DynaBean) obj).getDynaClass();
    DynaProperty dynaProperty = dynaClass.getDynaProperty(name);
    if (dynaProperty == null) {
        throw new FieldnameNotFoundException("DynaBean: Could not find this fieldName ("+name+") on the target object: " + obj, name, null);
    }
    return dynaProperty.getType();
}
 
Example #28
Source File: DynaBeanAdapter.java    From reflectutils with Apache License 2.0 5 votes vote down vote up
public Map<String, Object> getFieldValues(Object obj, FieldsFilter filter) {
    Map<String, Object> values = new HashMap<String, Object>();
    DynaProperty[] descriptors =
        ((DynaBean) obj).getDynaClass().getDynaProperties();
    for (int i = 0; i < descriptors.length; i++) {
        String name = descriptors[i].getName();
        // cannot filter the values for dynabeans -AZ
        Object o = getSimpleValue(obj, name);
        values.put(name, o);
    }
    return values;
}
 
Example #29
Source File: DynaBeanAdapter.java    From reflectutils with Apache License 2.0 5 votes vote down vote up
public Object getSimpleValue(Object obj, String name) {
    DynaProperty descriptor =
        ((DynaBean) obj).getDynaClass().getDynaProperty(name);
    if (descriptor == null) {
        throw new FieldnameNotFoundException(name);
    }
    Object value = (((DynaBean) obj).get(name));
    return value;
}
 
Example #30
Source File: DynaBeanAdapter.java    From reflectutils with Apache License 2.0 5 votes vote down vote up
public Object getIndexedValue(Object obj, String name, int index) {
    DynaProperty descriptor =
        ((DynaBean) obj).getDynaClass().getDynaProperty(name);
    if (descriptor == null) {
        throw new FieldnameNotFoundException(name);
    }
    Object value = ((DynaBean) obj).get(name, index);
    return value;
}