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.gbif.common.search.builder.SearchResponseBuilder.java

/**
 * Sets the value of the highlighted field in object 'bean'.
 *//*  w  w  w .  j  av  a  2s . c  o m*/
private void setHighlightedFieldValue(T bean, String solrField, String hlValue) {
    try {
        if (List.class.isAssignableFrom(
                PropertyUtils.getPropertyType(bean, solrField2javaPropertiesMap.get(solrField)))) {
            List<String> listProperty = (List<String>) PropertyUtils.getProperty(bean,
                    solrField2javaPropertiesMap.get(solrField));
            // Cleans the hl markers
            String hlCleanValue = cleanHighlightingMarks(hlValue);
            // Position of the value
            int hlValueIndex = listProperty.indexOf(hlCleanValue);
            if (hlValueIndex != -1) { // replace the value with the highlighted value
                listProperty.remove(hlValueIndex);
                listProperty.add(hlValueIndex, hlValue);
            }

        } else if (HighlightableList.class.isAssignableFrom(
                PropertyUtils.getPropertyType(bean, solrField2javaPropertiesMap.get(solrField)))) {
            HighlightableList property = (HighlightableList) PropertyUtils.getProperty(bean,
                    solrField2javaPropertiesMap.get(solrField));
            // Cleans the hl markers
            String hlCleanValue = cleanHighlightingMarks(hlValue);
            // Position of the value
            List<String> values = property.getValues();
            int hlValueIndex = values.indexOf(hlCleanValue);
            if (hlValueIndex != -1) { // replace the value with the highlighted value
                property.replaceValue(hlValueIndex, hlValue);
            }

        } else {
            String propertyName = solrField2javaPropertiesMap.get(solrField);
            if (PropertyUtils.getPropertyDescriptor(bean, propertyName).getPropertyType()
                    .isAssignableFrom(String.class)) {
                BeanUtils.setProperty(bean, propertyName, hlValue);
            }
        }
    } catch (IllegalAccessException e) {
        LOG.error("Error accessing field for setting highlighted value", e);
        throw new SearchException(e);
    } catch (InvocationTargetException e) {
        LOG.error("Error setting highlighted value", e);
        throw new SearchException(e);
    } catch (NoSuchMethodException e) {
        LOG.error("Error invoking method to set highlighted value", e);
        throw new SearchException(e);
    }
}

From source file:org.ikasan.configurationService.service.ConfigurationFactoryDefaultImpl.java

/**
 * @param runtimeConfiguration//from w ww .  j a v  a 2 s .  c o  m
 * @return
 */
public Configuration<List<ConfigurationParameter>> createConfiguration(String configurationResourceId,
        Object runtimeConfiguration) {
    if (runtimeConfiguration == null) {
        throw new ConfigurationException("Runtime configuration object cannot be 'null'");
    }

    Configuration<List<ConfigurationParameter>> configuration = new DefaultConfiguration(
            configurationResourceId, new ArrayList<ConfigurationParameter>());

    try {
        Map<String, Object> properties = PropertyUtils.describe(runtimeConfiguration);

        for (Map.Entry<String, Object> entry : properties.entrySet()) {
            String name = entry.getKey();
            Object value = entry.getValue();

            // TODO - is there a cleaner way of ignoring the class property ?
            if (!"class".equals(name)) {
                if (value == null) {
                    Class<?> cls = PropertyUtils.getPropertyType(runtimeConfiguration, name);

                    if (cls.isAssignableFrom(String.class)) {
                        if (isMasked(runtimeConfiguration, name)) {
                            configuration.getParameters()
                                    .add(new ConfigurationParameterMaskedStringImpl(name, null));
                        } else {
                            configuration.getParameters().add(new ConfigurationParameterStringImpl(name, null));
                        }
                    } else if (cls.isAssignableFrom(Long.class)) {
                        configuration.getParameters().add(new ConfigurationParameterLongImpl(name, null));
                    } else if (cls.isAssignableFrom(Integer.class)) {
                        configuration.getParameters().add(new ConfigurationParameterIntegerImpl(name, null));
                    } else if (cls.isAssignableFrom(Boolean.class)) {
                        configuration.getParameters().add(new ConfigurationParameterBooleanImpl(name, null));
                    } else if (cls.isAssignableFrom(List.class)) {
                        configuration.getParameters().add(new ConfigurationParameterListImpl(name, null));
                    } else if (cls.isAssignableFrom(Map.class)) {
                        configuration.getParameters().add(new ConfigurationParameterMapImpl(name, null));
                    } else {
                        logger.warn(
                                "Ignoring unsupported configurationParameter class [" + cls.getName() + "].");
                    }
                } else {
                    if (value instanceof String) {
                        if (isMasked(runtimeConfiguration, name)) {
                            configuration.getParameters()
                                    .add(new ConfigurationParameterMaskedStringImpl(name, (String) value));
                        } else {
                            configuration.getParameters()
                                    .add(new ConfigurationParameterStringImpl(name, (String) value));
                        }
                    } else if (value instanceof Long) {
                        configuration.getParameters()
                                .add(new ConfigurationParameterLongImpl(name, (Long) value));
                    } else if (value instanceof Integer) {
                        configuration.getParameters()
                                .add(new ConfigurationParameterIntegerImpl(name, (Integer) value));
                    } else if (value instanceof Boolean) {
                        configuration.getParameters()
                                .add(new ConfigurationParameterBooleanImpl(name, (Boolean) value));
                    } else if (value instanceof List) {
                        configuration.getParameters()
                                .add(new ConfigurationParameterListImpl(name, (List) value));
                    } else if (value instanceof Map) {
                        configuration.getParameters().add(new ConfigurationParameterMapImpl(name, (Map) value));
                    } else {
                        logger.warn("Ignoring unsupported configurationParameter class ["
                                + value.getClass().getName() + "].");
                    }
                }
            }
        }
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        throw new ConfigurationException(e);
    }

    return configuration;
}

From source file:org.jbuilt.utils.ValueClosure.java

/**
 * /*w  w w .  ja va 2s.  c o m*/
 * @param bean
 * @param prop
 * @param value
 *            - expecting 0 or 1 value
 */
public ValueClosure(Object bean, String prop, Object... value) {
    this.arguments = new Arguments(value);
    this.bean = bean;
    this.prop = prop;
    //      FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(bean.getClass().getSimpleName()+"."+prop, bean);
    //      this.expectedType = expectedType;
    try {
        this.propertyType = PropertyUtils.getPropertyType(bean, prop);
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (value != null) {
        if (value.length > 0) {
            if (value[0] != null) {
                this.value = value[0];
            }
        }
    } else {
        this.value = null;
    }
    // if (value != null && value.length > 0 && value[0] != null) {
    // this.value = value[0];
    // }
}

From source file:org.jbuilt.utils.ValueClosure.java

public Class<?> getType(Object bean, String prop) throws ELException {
    Class result = null;//from   w  w  w  .j  a  v  a 2 s . c  o  m
    try {
        result = PropertyUtils.getPropertyType(bean, prop);
    } catch (Throwable e) {
        throw new ELException(e);
    }
    return result;
}

From source file:org.jbuilt.utils.ValueClosure.java

@Override
public boolean equals(Object other) {

    if (other == this) {
        return true;
    }/*from www .ja v  a  2 s .  c o m*/

    if (other instanceof ValueExpression) {
        ValueExpression otherVE = (ValueExpression) other;
        Class type = null;
        try {
            type = PropertyUtils.getPropertyType(this.bean, this.prop);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        if (type != null) {
            return type.equals(otherVE.getType(context.getELContext()));
        }
    }
    return false;
}

From source file:org.jmesa.util.ItemUtils.java

/**
 * Get the Class for the property.//from ww w  .  j  a  va 2s .c o m
 * 
 * @param items The Collection of Beans or Maps.
 * @param property The Bean attribute or Map key.
 * @return The Class for the property.
 */
public static Class<?> getPropertyClassType(Collection<?> items, String property) throws Exception {

    Object item = items.iterator().next();

    if (item instanceof Map) {
        for (Object object : items) {
            Map<?, ?> map = (Map<?, ?>) object;
            Object val = map.get(property);

            if (val == null) {
                continue;
            }

            return val.getClass();
        }
    }

    Class<?> type;
    try {
        type = PropertyUtils.getPropertyType(item, property);
    } catch (Exception e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Had problems getting property type by object, trying reflection...");
        }
        type = BeanUtils.getPropertyType(item, property);
    }
    return type;
}

From source file:org.junitext.runners.parameters.factory.SetPropertyWithParameterRule.java

/**
 * @see org.apache.commons.digester.Rule#end(java.lang.String,
 *      java.lang.String)/*from  w  w  w .j  a va 2s  .  co m*/
 */
@Override
public void end(String namespace, String name) throws Exception {

    // Get the name of the property that we are going to set
    // This was pushed onto the stack in the begin method.
    String propertyName = (String) digester.pop(PROPERTY_NAME_STACK);

    // get the parameters
    Object[] parameters = (Object[]) digester.popParams();

    // Get the parent bean to on which we are setting the property
    Object parentBean = digester.peek(0);

    // Only attempt to set the property if an object parameter was set
    // This is so that property tags that have a value are not overwritten.
    if (parameters[0] != null) {

        if (digester.getLogger().isDebugEnabled()) {
            StringBuilder logMessage = new StringBuilder();
            logMessage.append("[SetPropertyWithParameterRule]{");
            logMessage.append(digester.getMatch());
            logMessage.append("} Setting ");
            logMessage.append(parentBean.getClass().getName());
            logMessage.append(".");
            logMessage.append(propertyName);
            logMessage.append(" = ");
            logMessage.append(parameters[0]);
            digester.getLogger().debug(logMessage.toString());
        }

        if (parameters[0] instanceof List) {
            // List objects may also represent array properties. Check if
            // the property is an array instead of a list.
            Class<?> propertyClass = PropertyUtils.getPropertyType(parentBean, propertyName);
            if (propertyClass.isArray()) {
                // It is an array...so we need to convert our list to an
                // array of the correct type.
                parameters[0] = listToArray((List) parameters[0], propertyClass.getComponentType());
            }
        }

        // Attempt to set the property on the parentBean using the given
        // object
        BeanUtils.setProperty(parentBean, propertyName, parameters[0]);
    }
}

From source file:org.kuali.ext.mm.ObjectUtil.java

public static String getSimpleTypeName(Object targetObject, String propertyName) {
    String simpleTypeName = StringUtils.EMPTY;
    try {//from   www.  j av a 2  s . c o m
        simpleTypeName = PropertyUtils.getPropertyType(targetObject, propertyName).getSimpleName();
    } catch (Exception e) {
        LOG.debug(e);
    }

    return simpleTypeName;
}

From source file:org.kuali.ext.mm.TestDataPreparator.java

/**
 * Generates transaction data for a business object from properties
 * /*from www.  j  a v a 2  s  .  c om*/
 * @param businessObject the transction business object
 * @return the transction business object with data
 * @throws Exception thrown if an exception is encountered for any reason
 */
public static <T> T buildTestDataObject(Class<? extends T> clazz, Properties properties) {
    T testData = null;

    try {
        testData = clazz.newInstance();

        Iterator propsIter = properties.keySet().iterator();
        while (propsIter.hasNext()) {
            String propertyName = (String) propsIter.next();
            String propertyValue = (String) properties.get(propertyName);

            // if searchValue is empty and the key is not a valid property ignore
            if (StringUtils.isBlank(propertyValue) || !(PropertyUtils.isWriteable(testData, propertyName))) {
                continue;
            }

            String propertyType = PropertyUtils.getPropertyType(testData, propertyName).getSimpleName();
            Object finalPropertyValue = ObjectUtil.valueOf(propertyType, propertyValue);

            if (finalPropertyValue != null) {
                PropertyUtils.setProperty(testData, propertyName, finalPropertyValue);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Cannot build a test data object with the given data. " + e);
    }

    return testData;
}

From source file:org.kuali.kfs.gl.web.TestDataGenerator.java

/**
 * This method gets the approperiate property value by examining the given parameters
 * /*w  w w  .j av a2s .c  om*/
 * @param businessObject the given business object
 * @param propertyName the given property name
 * @param propertyValue the given property value
 * @return the processed property value
 */
private Object getPropertyValue(Object businessObject, String propertyName, String propertyValue) {
    // get the property type of the given business object
    Class propertyType = null;
    try {
        propertyType = PropertyUtils.getPropertyType(businessObject, propertyName);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // implement the type conversion
    String propertyTypeName = propertyType.getName();
    Object finalPropertyValue = propertyValue;
    if (propertyType.isPrimitive()) {
        finalPropertyValue = null;
    } else if (propertyTypeName.indexOf("Integer") >= 0) {
        finalPropertyValue = new Integer(propertyValue.trim());
    } else if (propertyTypeName.indexOf("Boolean") >= 0) {
        finalPropertyValue = new Boolean(propertyValue.trim());
    } else if (propertyTypeName.indexOf("KualiDecimal") >= 0) {
        finalPropertyValue = new KualiDecimal(propertyValue.trim());
    }

    return finalPropertyValue;
}