Example usage for org.apache.commons.beanutils PropertyUtils getPropertyType

List of usage examples for org.apache.commons.beanutils PropertyUtils getPropertyType

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils getPropertyType.

Prototype

public static Class getPropertyType(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Return the Java Class representing the property type of the specified property, or null if there is no such property for the specified bean.

For more details see PropertyUtilsBean.

Usage

From source file:org.cloudifysource.dsl.internal.BaseDslScript.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private void applyPropertyToObject(final Object object, final String name, final Object value) {

    if (!isProperyExistsInBean(object, name)) {
        throw new IllegalArgumentException("Could not find property: " + name + " on Object: " + object);
    }//from   ww  w  . j a  v  a  2 s  .  c  o m

    // Check for duplicate properties.
    if (this.usedProperties == null) {
        throw new IllegalArgumentException("used properties can not be null. Property: " + name + ", Value: "
                + value.toString() + ", Active object: " + this.activeObject);
    }
    if (this.usedProperties.contains(name)) {
        if (!isDuplicatePropertyAllowed(value)) {
            throw new IllegalArgumentException(
                    "Property duplication was found: Property " + name + " is define" + "d more than once.");
        }
    }

    this.usedProperties.add(name);
    Object convertedValue = null;
    try {
        convertedValue = convertValueToExecutableDSLEntryIfNeeded(getDSLFile().getParentFile(), object, name,
                value);
        if (logger.isLoggable(Level.FINEST)) {
            logger.finest("BeanUtils.setProperty(object=" + object + ",name=" + name + ",value="
                    + convertedValue + ",value.getClass()=" + convertedValue.getClass());
        }

        // Check if writable
        if (!PropertyUtils.isWriteable(object, name)) {
            throw new IllegalArgumentException("Field " + name + " in object of type: "
                    + object.getClass().getName() + " is not writable");
        }

        // If value is a map, merge with existing map
        final Object currentValue = PropertyUtils.getProperty(object, name);
        if (currentValue != null && currentValue instanceof Map<?, ?> && convertedValue != null
                && convertedValue instanceof Map<?, ?>) {

            final Map<Object, Object> currentMap = (Map<Object, Object>) currentValue;
            currentMap.putAll((Map<Object, Object>) convertedValue);

        } else if (PropertyUtils.getPropertyType(object, name).isEnum() && value instanceof String) {
            final Class enumClass = PropertyUtils.getPropertyType(object, name);
            final Enum enumValue = Enum.valueOf(enumClass, (String) value);
            BeanUtils.setProperty(object, name, enumValue);
        } else {
            // Then set it
            BeanUtils.setProperty(object, name, convertedValue);
        }
    } catch (final DSLValidationException e) {
        throw new DSLValidationRuntimeException(e);
    } catch (final Exception e) {
        throw new IllegalArgumentException("Failed to set property " + name + " of Object " + object
                + " to value: " + value + ". Error was: " + e.getMessage(), e);
    }

    checkForApplicationServiceBlockNameParameter(name, value);

}

From source file:org.craftercms.commons.jackson.mvc.CrafterJackson2MessageConverter.java

private void injectValue(final Object object, final Field field) {
    //Should be null due we ask before if the annotation exists !!
    String propertyToUseName = field.getAnnotation(InjectValue.class).useProperty();
    try {/*from ww w. j  a  v  a 2 s.c  o m*/
        Object propertyValue = PropertyUtils.getProperty(object, propertyToUseName);
        Object valueToInject = injectValueFactory.getObjectFor(
                PropertyUtils.getPropertyType(object, field.getName()), propertyValue, propertyToUseName,
                object);
        if (valueToInject != null) {
            PropertyUtils.setProperty(object, field.getName(), valueToInject);
        }
    } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        log.error("Unable to inject value " + field.getName() + " for class " + object.getClass(), e);
    }
}

From source file:org.displaytag.decorator.Decorator.java

/**
 * Looks for a getter for the given property using introspection.
 * @param propertyName name of the property to check
 * @return boolean true if the decorator has a getter for the given property
 *//*from w w w  .  j  a  va2  s  .  co  m*/
public boolean searchGetterFor(String propertyName) {

    Class type = null;

    try {
        // using getPropertyType instead of isReadable since isReadable doesn't support mapped properties.
        // Note that this method usually returns null if a property is not found and doesn't throw any exception
        // also for non existent properties
        type = PropertyUtils.getPropertyType(this, propertyName);
    } catch (IllegalAccessException e) {
        // ignore
    } catch (InvocationTargetException e) {
        // ignore
    } catch (NoSuchMethodException e) {
        // ignore
    }

    return type != null;

}

From source file:org.drools.informer.domain.DomainModelSupportTest.java

private void setTextProperty(String property, String value) {
    try {/*from  w ww  . jav  a2s.  c om*/
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.QuestionType.TYPE_TEXT, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.drools.informer.domain.DomainModelSupportTest.java

private void setNumberProperty(String property, Long value) {
    try {/*from w w  w .  j a v  a  2s .  c  o m*/
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.QuestionType.TYPE_NUMBER, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.drools.informer.domain.DomainModelSupportTest.java

private void setDecimalProperty(String property, BigDecimal value) {
    try {/*from  w  w  w  .  j  a va2s .  com*/
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.QuestionType.TYPE_DECIMAL, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.drools.informer.domain.DomainModelSupportTest.java

private void setBooleanProperty(String property, Boolean value) {
    try {//from   w  ww.j av a  2  s. c om
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.QuestionType.TYPE_BOOLEAN, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.drools.informer.domain.DomainModelSupportTest.java

private void setDateProperty(String property, Date value) {
    try {//from www  .ja  v  a  2  s .  com
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.QuestionType.TYPE_DATE, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.drools.informer.domain.DomainModelSupportTest.java

private void setListProperty(String property, String value) {
    try {/*w w  w . j  a  va2s  . c o  m*/
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.QuestionType.TYPE_LIST, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.fornax.cartridges.sculptor.framework.propertyeditor.OptionEditor.java

/**
 * Format the Object as String of concatenated properties.
 *//*w  ww.  j a v a2 s . c  o m*/
public String getAsText() {

    Object value = getValue();
    if (value == null) {
        return "";
    }

    String propertyName = null; // used in error handling below
    try {
        StringBuffer label = new StringBuffer();

        for (int i = 0; i < properties.length; i++) {
            propertyName = properties[i];
            Class<?> propertyType = PropertyUtils.getPropertyType(value, propertyName);
            Object propertyValue = PropertyUtils.getNestedProperty(value, propertyName);
            PropertyEditor editor = registry.findCustomEditor(propertyType,
                    registryPropertyNamePrefix + propertyName);
            if (editor == null) {
                label.append(propertyValue);
            } else {
                editor.setValue(propertyValue);
                label.append(editor.getAsText());
                editor.setValue(null);
            }

            if (i < (properties.length - 1)) {
                label.append(separator);
            }
        }

        return label.toString();

    } catch (Exception e) {
        throw new IllegalArgumentException("Couldn't access " + propertyName + " of "
                + value.getClass().getName() + " : " + e.getMessage(), e);
    }

}