Example usage for org.apache.commons.beanutils DynaProperty getType

List of usage examples for org.apache.commons.beanutils DynaProperty getType

Introduction

In this page you can find the example usage for org.apache.commons.beanutils DynaProperty getType.

Prototype

public Class getType() 

Source Link

Document

Gets the Java class representing the data type of the underlying property values.

There are issues with serializing primitive class types on certain JVM versions (including java 1.3).

Usage

From source file:jp.terasoluna.fw.util.BeanUtil.java

/**
 * ??JavaBean????/* w ww .  j  a v  a2 s  .c om*/
 * @param bean
 *            ????JavaBean
 * @param property
 *            JavaBean?
 * @return ?
 * @throws PropertyAccessException ??????
 */
public static Class<?> getBeanPropertyType(Object bean, String property) throws PropertyAccessException {
    try {
        Class<?> type = null;
        if (bean instanceof DynaBean) {
            DynaProperty descriptor = ((DynaBean) bean).getDynaClass().getDynaProperty(property);
            if (descriptor != null) {
                type = descriptor.getType();
            }
        } else {
            type = PropertyUtils.getPropertyType(bean, property);
        }
        return type;
    } catch (IllegalArgumentException e) {
        throw new PropertyAccessException(e);
    } catch (IllegalAccessException e) {
        throw new PropertyAccessException(e);
    } catch (InvocationTargetException e) {
        throw new PropertyAccessException(e);
    } catch (NoSuchMethodException e) {
        throw new PropertyAccessException(e);
    }
}

From source file:com.mooing.wss.common.util.BeanUtil.java

/**
 * ?JavaBean// www  . j  a v a  2 s.  c o  m
 * @param bean JavaBean
 * @param property ??
 * @return 
 * @throws PropertyAccessException 
 */
@SuppressWarnings("rawtypes")
public static Class getBeanPropertyType(Object bean, String property) throws PropertyAccessException {
    try {
        Class type = null;
        if (bean instanceof DynaBean) {
            DynaProperty descriptor = ((DynaBean) bean).getDynaClass().getDynaProperty(property);
            if (descriptor != null) {
                type = descriptor.getType();
            }
        } else {
            type = PropertyUtils.getPropertyType(bean, property);
        }
        return type;
    } catch (IllegalArgumentException e) {
        throw new PropertyAccessException(e);
    } catch (IllegalAccessException e) {
        throw new PropertyAccessException(e);
    } catch (InvocationTargetException e) {
        throw new PropertyAccessException(e);
    } catch (NoSuchMethodException e) {
        throw new PropertyAccessException(e);
    }
}

From source file:com.expressui.core.util.ReflectionUtil.java

/**
 * Finds all properties in the bean that are complex, that is not BeanUtils.isSimpleValueType
 *
 * @param bean bean to reflectively analyze
 * @return collection of properties on the bean
 */// ww  w. j a v a  2 s  .  c  o m
public static Collection<String> findComplexProperties(Object bean) {
    Collection<String> complexProperties = new ArrayList<String>();

    WrapDynaBean wrapDynaBean = new WrapDynaBean(bean);
    DynaProperty[] properties = wrapDynaBean.getDynaClass().getDynaProperties();
    for (DynaProperty property : properties) {
        String propertyName = property.getName();
        Class propertyType = property.getType();
        if (!BeanUtils.isSimpleValueType(propertyType)) {
            complexProperties.add(propertyName);
        }
    }

    return complexProperties;
}

From source file:com.expressui.core.util.ReflectionUtil.java

/**
 * Asks if the bean's properties are empty. boolean properties that are false and numbers
 * that are zero are considered empty. String values that are zero-length are considered empty.
 * All other property types must be null to be considered empty.
 *
 * @param bean bean to check//from  w w w .  j  a v a  2 s .c om
 * @return true if bean has no values
 */
public static boolean isBeanEmpty(Object bean) {
    if (bean == null) {
        return true;
    }

    WrapDynaBean wrapDynaBean = new WrapDynaBean(bean);
    DynaProperty[] properties = wrapDynaBean.getDynaClass().getDynaProperties();
    for (DynaProperty property : properties) {
        String propertyName = property.getName();
        Class propertyType = property.getType();

        Object value = wrapDynaBean.get(propertyName);
        if (propertyType.isPrimitive()) {
            if (value instanceof Number && !value.toString().equals("0")) {
                return false;
            } else if (value instanceof Boolean && !value.toString().equals("false")) {
                return false;
            } else if (!value.toString().isEmpty()) {
                return false;
            }
        } else if (value != null) {
            if (!(value instanceof Collection)) {
                String convertedStringValue = ConvertUtils.convert(value);
                if (!StringUtil.isEmpty(convertedStringValue)) {
                    return false;
                }
            }
        }
    }

    return true;
}

From source file:com.github.haixing_hu.ilibrary.model.FieldTemplateTest.java

@Test
public void testToDynaProperty() {
    final FieldTemplate title = getTitleField();
    final DynaProperty titleProp = title.toDynaProperty();
    assertEquals("title", titleProp.getName());
    assertEquals(String.class, titleProp.getType());

    final FieldTemplate authors = getAuthorsField();
    final DynaProperty authorsProp = authors.toDynaProperty();
    assertEquals("authors", authorsProp.getName());
    assertEquals(List.class, authorsProp.getType());
    assertEquals(Responsibility.class, authorsProp.getContentType());
}

From source file:com.nfwork.dbfound.json.JSONDynaBean.java

public void set(String name, Object value) {
    DynaProperty property = getDynaProperty(name);
    if (property.getType() == null) {
        throw new NullPointerException("Unspecified property type for " + name);
    }//from   www.j  ava 2s  .co  m

    if (value == null) {
        if (property.getType().isPrimitive()) {
            throw new NullPointerException("Property type for " + name + " is primitive");
        }
    } else if (!isDynaAssignable(property.getType(), value.getClass())) {
        try {
            value = ConvertUtils.convert(String.valueOf(value), value.getClass());
        } catch (Exception e) {
            throw new IllegalArgumentException("Unassignable property type for " + name);
        }
    }
    // log.debug( name + " = " + value );
    dynaValues.put(name, value);
}

From source file:net.sf.json.util.DynaBeanToBeanMorpher.java

/**
 * DOCUMENT ME!// w w  w. j a v a 2 s  . c o  m
 *
 * @param value DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 */
public Object morph(Object value) {
    if (value == null) {
        return null;
    }

    if (!supports(value.getClass())) {
        throw new MorphException("value is not a DynaBean");
    }

    Object bean = null;

    try {
        bean = beanClass.newInstance();

        DynaBean dynaBean = (DynaBean) value;
        DynaClass dynaClass = dynaBean.getDynaClass();
        PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(beanClass);

        for (int i = 0; i < pds.length; i++) {
            PropertyDescriptor pd = pds[i];
            String name = pd.getName();
            DynaProperty dynaProperty = dynaClass.getDynaProperty(name);

            if (dynaProperty != null) {
                Class dynaType = dynaProperty.getType();
                Class type = pd.getPropertyType();

                if (type.isAssignableFrom(dynaType)) {
                    PropertyUtils.setProperty(bean, name, dynaBean.get(name));
                } else {
                    if (IdentityObjectMorpher.getInstance() == morpherRegistry.getMorpherFor(type)) {
                        throw new MorphException("Can't find a morpher for target class " + type);
                    } else {
                        PropertyUtils.setProperty(bean, name, morpherRegistry.morph(type, dynaBean.get(name)));
                    }
                }
            }
        }
    } catch (InstantiationException e) {
        throw new MorphException(e);
    } catch (IllegalAccessException e) {
        throw new MorphException(e);
    } catch (InvocationTargetException e) {
        throw new MorphException(e);
    } catch (NoSuchMethodException e) {
        throw new MorphException(e);
    }

    return bean;
}

From source file:es.caib.zkib.jxpath.ri.model.dynabeans.DynaBeanPropertyPointer.java

/**
 * Convert a value to the appropriate property type.
 * @param value to convert//from   www .  jav a  2  s.  co  m
 * @param element whether this should be a collection element.
 * @return conversion result
 */
private Object convert(Object value, boolean element) {
    DynaClass 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);
    }
}

From source file:ddf.catalog.data.dynamic.impl.MetacardPropertyDescriptorImpl.java

/**
 * Creates a property desriptor based on the given DynaProperty object and the specified values for
 * indexedBySource, tokenized, and stored.
 * This works for the majority of cases. In cases where the underlying class representing this
 * property is the same as other property formats, {@link #setFormat(AttributeFormat)} should be
 * called after creation to set the correct format.
 * @param dynaProperty descriptor of the attribute (name, value, etc.)
 * @param indexedBySource indicates whether the source should index this attribute for search
 * @param stored indicates whether the source should store this value
 * @param tokenized indicates whether the value for this attribute should be parsed
 *//*from  w  w  w. j  a v a  2 s .co  m*/
public MetacardPropertyDescriptorImpl(DynaProperty dynaProperty, boolean indexedBySource, boolean stored,
        boolean tokenized) {
    super(dynaProperty.getName(), dynaProperty.getType(), dynaProperty.getContentType());
    this.indexedBySource = indexedBySource;
    this.stored = stored;
    this.tokenized = tokenized;
    format = getFormatFromClass(
            dynaProperty.getContentType() == null ? dynaProperty.getType() : dynaProperty.getContentType());
}

From source file:net.sf.json.JSONDynaClass.java

/**
 * DOCUMENT ME!/*from  www  . jav  a  2s  . c o m*/
 *
 * @param obj DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 */
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }

    if (obj == null) {
        return false;
    }

    if (!(obj instanceof JSONDynaClass)) {
        return false;
    }

    JSONDynaClass other = (JSONDynaClass) obj;
    EqualsBuilder builder = new EqualsBuilder().append(this.name, other.name).append(this.type, other.type);

    for (int i = 0; i < dynaProperties.length; i++) {
        DynaProperty a = this.dynaProperties[i];
        DynaProperty b = other.dynaProperties[i];
        builder.append(a.getName(), b.getName());
        builder.append(a.getType(), b.getType());
    }

    return builder.isEquals();
}