org.apache.commons.beanutils.DynaClass Java Examples

The following examples show how to use org.apache.commons.beanutils.DynaClass. 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: 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 #2
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 #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: 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 #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: 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 #10
Source File: TestMultiWrapDynaBean.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether the class of bean can be queried.
 */
@Test
public void testGetDynaClass()
{
    final DynaClass cls = createBean(false).getDynaClass();
    assertNotNull("Property not found (1)",
            cls.getDynaProperty("throwExceptionOnMissing"));
    assertNotNull("Property not found (2)", cls.getDynaProperty("text"));
}
 
Example #11
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 #12
Source File: MultiWrapDynaClass.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance of {@code MultiWrapDynaClass} and initializes it
 * with the collection of classes to be wrapped.
 *
 * @param wrappedCls the collection with wrapped classes
 */
public MultiWrapDynaClass(final Collection<? extends DynaClass> wrappedCls)
{
    properties = new LinkedList<>();
    namedProperties = new HashMap<>();
    initProperties(wrappedCls);
}
 
Example #13
Source File: DynaClassCache.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link SqlDynaClass} for the given bean.
 * 
 * @param dynaBean The bean
 * @return The dyna bean class
 */
public SqlDynaClass getDynaClass(DynaBean dynaBean) throws SqlDynaException
{
    DynaClass dynaClass = dynaBean.getDynaClass();

    if (dynaClass instanceof SqlDynaClass)
    {
        return (SqlDynaClass)dynaClass;
    }
    else
    {
        // TODO: we could autogenerate an SqlDynaClass here ?
        throw new SqlDynaException("The dyna bean is not an instance of a SqlDynaClass");
    }
}
 
Example #14
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 #15
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 #16
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 #17
Source File: DynaClassCache.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link SqlDynaClass} for the given bean.
 * 
 * @param dynaBean The bean
 * @return The dyna bean class
 */
public SqlDynaClass getDynaClass(DynaBean dynaBean) throws SqlDynaException
{
    DynaClass dynaClass = dynaBean.getDynaClass();

    if (dynaClass instanceof SqlDynaClass)
    {
        return (SqlDynaClass)dynaClass;
    }
    else
    {
        // TODO: we could autogenerate an SqlDynaClass here ?
        throw new SqlDynaException("The dyna bean is not an instance of a SqlDynaClass");
    }
}
 
Example #18
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 #19
Source File: MTable.java    From viritin with Apache License 2.0 4 votes vote down vote up
private ListContainer<T> createContainer(DynaClass type) {
    return new ListContainer<>(type);
}
 
Example #20
Source File: FilterableListContainer.java    From viritin with Apache License 2.0 4 votes vote down vote up
public FilterableListContainer(DynaClass type) {
	super(type);
}
 
Example #21
Source File: FilterableListContainer.java    From viritin with Apache License 2.0 4 votes vote down vote up
public FilterableListContainer(DynaClass type, String... properties) {
	super(type, properties);
}
 
Example #22
Source File: ConfigurationDynaBean.java    From commons-configuration with Apache License 2.0 4 votes vote down vote up
@Override
public DynaClass getDynaClass()
{
    return new ConfigurationDynaClass(getConfiguration());
}
 
Example #23
Source File: MultiWrapDynaBean.java    From commons-configuration with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc} This implementation returns an instance of
 * {@code MultiWrapDynaClass}.
 */
@Override
public DynaClass getDynaClass()
{
    return dynaClass;
}
 
Example #24
Source File: DynaBeanPropertyPointer.java    From commons-jxpath with Apache License 2.0 4 votes vote down vote up
/**
 * Returns true if the bean has the currently selected property.
 * @return boolean
 */
protected boolean isActualProperty() {
    DynaClass dynaClass = dynaBean.getDynaClass();
    return dynaClass.getDynaProperty(getPropertyName()) != null;
}
 
Example #25
Source File: DynaBeanPropertyPointer.java    From commons-jxpath with Apache License 2.0 4 votes vote down vote up
/**
 * Learn whether the property referenced is an indexed property.
 * @return boolean
 */
protected boolean isIndexedProperty() {
    DynaClass dynaClass = dynaBean.getDynaClass();
    DynaProperty property = dynaClass.getDynaProperty(name);
    return property.isIndexed();
}
 
Example #26
Source File: MTable.java    From viritin with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs a Table with explicit bean type. Handy for example if your
 * beans are JPA proxies or the table is empty when showing it initially.
 *
 * @param type the type of beans that are listed in this table
 */
public MTable(DynaClass type) {
    bic = createContainer(type);
    setContainerDataSource(bic);
}
 
Example #27
Source File: SqlDynaBean.java    From gemfirexd-oss with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new dyna bean of the given class.
 * 
 * @param dynaClass The dyna class
 */
public SqlDynaBean(DynaClass dynaClass)
{
    super(dynaClass);
}
 
Example #28
Source File: SqlDynaBean.java    From gemfirexd-oss with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new dyna bean of the given class.
 * 
 * @param dynaClass The dyna class
 */
public SqlDynaBean(DynaClass dynaClass)
{
    super(dynaClass);
}