Example usage for java.beans PropertyDescriptor getPropertyType

List of usage examples for java.beans PropertyDescriptor getPropertyType

Introduction

In this page you can find the example usage for java.beans PropertyDescriptor getPropertyType.

Prototype

public synchronized Class<?> getPropertyType() 

Source Link

Document

Returns the Java type info for the property.

Usage

From source file:org.apache.empire.spring.EmpireReader.java

@Override
protected void getBeanProperty(Object bean, String property, Object value) {
    try {//w ww  .  j  a v a  2  s .c o m
        if (bean == null)
            throw new InvalidArgumentException("bean", bean);
        if (StringUtils.isEmpty(property))
            throw new InvalidArgumentException("property", property);

        // Get descriptor
        PropertyDescriptor descriptor = BeanUtilsBean.getInstance().getPropertyUtils()
                .getPropertyDescriptor(bean, property);
        if (descriptor == null) {
            return; // Skip this property setter
        }
        // Check enum
        Class<?> type = descriptor.getPropertyType();
        if (type.isEnum()) {
            // Enum<?> ev = Enum.valueOf(type, value);
            boolean found = false;
            Enum<?>[] items = (Enum[]) type.getEnumConstants();
            for (int i = 0; i < items.length; i++) {
                Enum<?> item = items[i];
                if (ObjectUtils.compareEqual(item.name(), value)) {
                    value = item;
                    found = true;
                    break;
                }
            }
            // Enumeration value not found
            if (!found)
                throw new ItemNotFoundException(value);
        }

        // Set Property Value
        if (value != null) { // Bean utils will convert if necessary
            BeanUtils.setProperty(bean, property, value);
        } else { // Don't convert, just set
            PropertyUtils.setProperty(bean, property, null);
        }
    } catch (IllegalAccessException e) {
        log.error(bean.getClass().getName() + ": unable to set property '" + property + "'");
        throw new BeanPropertySetException(bean, property, e);
    } catch (InvocationTargetException e) {
        log.error(bean.getClass().getName() + ": unable to set property '" + property + "'");
        throw new BeanPropertySetException(bean, property, e);
    } catch (NoSuchMethodException e) {
        log.error(bean.getClass().getName() + ": unable to set property '" + property + "'");
        throw new BeanPropertySetException(bean, property, e);
    } catch (NullPointerException e) {
        log.error(bean.getClass().getName() + ": unable to set property '" + property + "'");
        throw new BeanPropertySetException(bean, property, e);
    }
}

From source file:cn.clickvalue.cv2.model.rowmapper.BeanPropertyRowMapper.java

/**
 * Retrieve a JDBC object value for the specified column.
 * <p>The default implementation calls
 * {@link JdbcUtils#getResultSetValue(java.sql.ResultSet, int, Class)}.
 * Subclasses may override this to check specific value types upfront,
 * or to post-process values return from <code>getResultSetValue</code>.
 * @param rs is the ResultSet holding the data
 * @param index is the column index// w ww. j  a v  a2s  . com
 * @param pd the bean property that each result object is expected to match
 * (or <code>null</code> if none specified)
 * @return the Object value
 * @throws SQLException in case of extraction failure
 * @see org.springframework.jdbc.support.JdbcUtils#getResultSetValue(java.sql.ResultSet, int, Class)
 */
protected Object getColumnValue(ResultSet rs, int index, PropertyDescriptor pd) throws SQLException {
    return getResultSetValue(rs, index, pd.getPropertyType());
}

From source file:com.panguso.lc.analysis.format.dao.impl.DaoImpl.java

@Override
public long searchCount(T condition) {
    StringBuilder qString = new StringBuilder(
            "select count(model) from " + entityClass.getSimpleName() + " model");
    StringBuilder qWhere = new StringBuilder(" where ");
    StringBuilder qCondition = new StringBuilder();
    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(entityClass);
    for (int i = 0, count = propertyDescriptors.length; i < count; i++) {
        PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
        String name = propertyDescriptor.getName();
        Class<?> type = propertyDescriptor.getPropertyType();
        String value = null;/*from   ww  w  .ja v a  2  s  .  c om*/
        try {
            value = BeanUtils.getProperty(condition, name);
        } catch (Exception e) {
            // ?
            continue;
        }
        if (value == null || name.equals("class")) {
            continue;
        }
        if ("java.lang.String".equals(type.getName())) {
            qCondition.append("model.");
            qCondition.append(name);
            qCondition.append(" like ");
            qCondition.append("'%");
            qCondition.append(value);
            qCondition.append("%'");
        } else {
            qCondition.append("model.");
            qCondition.append(name);
            qCondition.append("=");
            qCondition.append(value);
        }
        qCondition.append(" and ");
    }
    if (qCondition.length() != 0) {
        qString.append(qWhere).append(qCondition);
        if (qCondition.toString().endsWith(" and ")) {
            qString.delete(qString.length() - " and ".length(), qString.length());
        }
    }
    Query query = em.createQuery(qString.toString());
    return (Long) query.getSingleResult();
}

From source file:org.malaguna.cmdit.service.reflection.ReflectionUtils.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private void updateOldValue(Object oldObj, PropertyDescriptor desc, Object oldValue, Object newValue) {
    if (Collection.class.isAssignableFrom(desc.getPropertyType())) {
        Collection oldSetAux = (Collection) oldValue;
        Collection newSetAux = (Collection) newValue;

        if (oldSetAux == null) {
            setNewValue(oldObj, desc.getWriteMethod(), newSetAux);
        } else {// ww  w  .  ja  v a 2s  .  c  o m
            //oldSetAux.clear();

            if (newSetAux != null) {
                Collection<?> intersection = CollectionUtils.intersection(oldSetAux, newSetAux);
                Collection<?> nuevos = CollectionUtils.removeAll(newSetAux, intersection);
                Collection<?> borrados = CollectionUtils.removeAll(oldSetAux, intersection);

                oldSetAux.removeAll(borrados);
                oldSetAux.addAll(nuevos);
            }
        }
    } else {
        setNewValue(oldObj, desc.getWriteMethod(), newValue);
    }
}

From source file:org.openmobster.core.mobileObject.TestBeanSyntax.java

private Object initializeSimpleProperty(Object parentObject, String property,
        PropertyDescriptor propertyMetaData) throws Exception {
    Object propertyValue = null;//from   w w w . j a v  a 2s  .  co  m

    //A Regular Property
    propertyValue = PropertyUtils.getProperty(parentObject, property);
    if (propertyValue == null) {
        Object newlyInitialized = propertyMetaData.getPropertyType().newInstance();
        PropertyUtils.setProperty(parentObject, property, newlyInitialized);
        propertyValue = newlyInitialized;
    }

    return propertyValue;
}

From source file:com.panguso.lc.analysis.format.dao.impl.DaoImpl.java

@Override
public List<T> search(T condition, int pageNo, int pageSize) {
    StringBuilder qString = new StringBuilder("select model from " + entityClass.getSimpleName() + " model");
    StringBuilder qWhere = new StringBuilder(" where ");
    StringBuilder qCondition = new StringBuilder();
    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(entityClass);
    for (int i = 0, count = propertyDescriptors.length; i < count; i++) {
        PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
        String name = propertyDescriptor.getName();
        Class<?> type = propertyDescriptor.getPropertyType();
        String value = null;/*from  w  w w .  j  av  a  2 s  . c  o  m*/
        try {
            value = BeanUtils.getProperty(condition, name);
        } catch (Exception e) {
            // ?
            continue;
        }
        if (value == null || name.equals("class")) {
            continue;
        }
        if ("java.lang.String".equals(type.getName())) {
            qCondition.append("model.");
            qCondition.append(name);
            qCondition.append(" like ");
            qCondition.append("'%");
            qCondition.append(value);
            qCondition.append("%'");
        } else {
            qCondition.append("model.");
            qCondition.append(name);
            qCondition.append("=");
            qCondition.append(value);
        }
        qCondition.append(" and ");

    }
    if (qCondition.length() != 0) {
        qString.append(qWhere).append(qCondition);
        if (qCondition.toString().endsWith(" and ")) {
            qString.delete(qString.length() - " and ".length(), qString.length());
        }
    }
    Query query = em.createQuery(qString.toString());
    query.setFirstResult(pageNo * pageSize);
    query.setMaxResults(pageSize);
    return query.getResultList();
}

From source file:be.fgov.kszbcss.rhq.websphere.component.jdbc.db2.pool.ConnectionContextImpl.java

ConnectionContextImpl(Map<String, Object> properties) {
    dataSource = new DB2SimpleDataSource();
    BeanInfo beanInfo;/*w  ww  .  j  a v a  2s  .c o m*/
    try {
        beanInfo = Introspector.getBeanInfo(DB2SimpleDataSource.class);
    } catch (IntrospectionException ex) {
        throw new Error(ex);
    }
    for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
        String name = descriptor.getName();
        if (properties.containsKey(name)) {
            Object value = properties.get(name);
            Class<?> propertyType = descriptor.getPropertyType();
            if (log.isDebugEnabled()) {
                log.debug("Setting property " + name + ": propertyType=" + propertyType.getName() + ", value="
                        + value + " (class=" + (value == null ? "<N/A>" : value.getClass().getName()) + ")");
            }
            if (propertyType != String.class && value instanceof String) {
                // Need to convert value to correct type
                if (propertyType == Integer.class || propertyType == Integer.TYPE) {
                    value = Integer.valueOf((String) value);
                }
                if (log.isDebugEnabled()) {
                    log.debug("Converted value to " + value + " (class=" + value.getClass().getName() + ")");
                }
            }
            try {
                descriptor.getWriteMethod().invoke(dataSource, value);
            } catch (IllegalArgumentException ex) {
                throw new RuntimeException("Failed to set '" + name + "' property", ex);
            } catch (IllegalAccessException ex) {
                throw new IllegalAccessError(ex.getMessage());
            } catch (InvocationTargetException ex) {
                Throwable cause = ex.getCause();
                if (cause instanceof RuntimeException) {
                    throw (RuntimeException) cause;
                } else if (cause instanceof Error) {
                    throw (Error) cause;
                } else {
                    throw new RuntimeException(ex);
                }
            }
        }
    }
}

From source file:no.sesat.search.datamodel.DataModelTest.java

private void handleProperty(final Collection<Method> propertyMethods, final PropertyDescriptor property)
        throws IntrospectionException {

    if (null != property.getReadMethod()) {
        propertyMethods.add(property.getReadMethod());
        // recurse down the datamodel heirarchy
        if (null != property.getPropertyType().getAnnotation(DataObject.class)) {
            ensureJavaBeanAPI(property.getPropertyType());
        }/*w  w  w.j  a  va2s . com*/
    }
    if (null != property.getWriteMethod()) {
        propertyMethods.add(property.getWriteMethod());
    }
}

From source file:name.ikysil.beanpathdsl.codegen.Context.java

private void buildTransitiveClosure(Map<Class<?>, IncludedClass> transitiveClosure, Class<?> clazz,
        IncludedClass config) {/*  ww w .j  a v a 2 s  .c  o  m*/
    if (transitiveClosure.containsKey(clazz) || isExcluded(clazz)) {
        return;
    }
    logger.info("Class: {}", clazz);
    transitiveClosure.put(clazz, config);
    if (config.isTransitive()) {
        PropertyDescriptor[] pds = getPropertyDescriptors(clazz);
        for (PropertyDescriptor pd : pds) {
            logger.debug("Property Descriptor: {} :: {}", clazz, pd);
            Class<?> pdClass = pd.getPropertyType();
            if ((pdClass == null) && (pd instanceof IndexedPropertyDescriptor)) {
                pdClass = ((IndexedPropertyDescriptor) pd).getIndexedPropertyType();
            }
            if (pdClass == null) {
                continue;
            }
            IncludedClass pdConfig = transitiveClosure.get(pdClass);
            if (pdConfig == null) {
                pdConfig = config;
            }
            buildTransitiveClosure(transitiveClosure, pdClass, pdConfig);
        }
    }
}

From source file:com.google.code.pathlet.web.ognl.impl.InstantiatingNullHandler.java

public Object nullPropertyValue(Map context, Object target, Object property) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Entering nullPropertyValue [target=" + target + ", property=" + property + "]");
    }/* w w w .ja v a 2  s  .c o  m*/

    if ((target == null) || (property == null)) {
        return null;
    }

    try {
        String propName = property.toString();

        Class clazz = null;

        if (target != null) {
            PropertyDescriptor[] descs = reflectionProvider.getPropertyDescriptors(target);

            for (PropertyDescriptor desc : descs) {
                if (propName.equals(desc.getName())) {
                    clazz = desc.getPropertyType();
                }
            }
        }

        if (clazz == null) {
            // can't do much here!
            return null;
        }

        Object param = createObject(clazz, target, propName, context);
        Ognl.setValue(propName, context, target, param);

        return param;
    } catch (Exception e) {
        LOG.error("Could not create and/or set value back on to object", e);
    }

    return null;
}