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:com.ixcode.framework.javabean.format.JavaBeanFormatter.java

/**
 * This needs a map of parsers to type so we dont have to have this big if statement.
 *///from w  w  w  .  ja  v  a  2  s .c  om
public Object parseStringToPropertyValue(Object model, String propertyName, Locale locale, String sourceValue)
        throws JavaBeanParseException, FormatterException {
    try {
        PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(model, propertyName);
        if (propertyDescriptor == null) {
            throw new IllegalStateException("Could not find a property in model " + model.getClass().getName()
                    + " for property name '" + propertyName + "'");
        }

        Class propertyType = propertyDescriptor.getPropertyType();

        IJavaBeanValueFormat format = getFormat(locale, propertyType);

        return format.parse(sourceValue);

    } catch (IllegalAccessException e) {
        throw new FormatterException(e);
    } catch (InvocationTargetException e) {
        throw new FormatterException(e);
    } catch (NoSuchMethodException e) {
        throw new FormatterException(e);
    }

}

From source file:net.sourceforge.vulcan.web.struts.forms.PluginConfigForm.java

@SuppressWarnings("unchecked")
private String getTypeAndPrepare(String propertyName, PropertyDescriptor pd) {
    final Class<?> c = pd.getPropertyType();
    final ConfigChoice choicesType = (ConfigChoice) pd.getValue(PluginConfigDto.ATTR_CHOICE_TYPE);

    if (choicesType != null) {
        switch (choicesType) {
        case PROJECTS:
            choices.put(propertyName, availableProjects);
            break;
        case INLINE:
            choices.put(propertyName, (List<String>) pd.getValue(PluginConfigDto.ATTR_AVAILABLE_CHOICES));
        }/*from w  w  w .j  a  v a 2s.  c  om*/
        if (c.isArray()) {
            return "choice-array";
        }
        return "enum";
    }

    final Widget widget = (Widget) pd.getValue(PluginConfigDto.ATTR_WIDGET_TYPE);

    if (Widget.PASSWORD.equals(widget)) {
        hidePassword(propertyName);
    }

    if (c.isArray()) {
        final Class<?> componentType = c.getComponentType();
        if (isPrimitive(componentType)) {
            return "primitive-array";
        } else if (Enum.class.isAssignableFrom(componentType)) {
            populateEnumChoices(propertyName, componentType);
            return "choice-array";
        }
        return "object-array";
    } else if (isBoolean(c)) {
        return "boolean";
    } else if (isPrimitive(c)) {
        if (widget != null) {
            return widget.name().toLowerCase();
        }
        return "primitive";
    } else if (Enum.class.isAssignableFrom(c)) {
        populateEnumChoices(propertyName, c);
        if (widget != null) {
            return widget.name().toLowerCase();
        }
        return "enum";
    }
    return "object";
}

From source file:com.bstek.dorado.idesupport.robot.EntityDataTypeReflectionRobot.java

protected void reflectAndComplete(Element element, Class<?> cls) throws Exception {
    Context context = Context.getCurrent();
    DataTypeManager dataTypeManager = (DataTypeManager) context.getServiceBean("dataTypeManager");
    Document document = element.getOwnerDocument();

    Map<String, Element> propertyDefElementMap = new HashMap<String, Element>();
    for (Element propertyDefElement : DomUtils.getChildrenByTagName(element, DataXmlConstants.PROPERTY_DEF)) {
        String name = propertyDefElement.getAttribute(XmlConstants.ATTRIBUTE_NAME);
        propertyDefElementMap.put(name, propertyDefElement);
    }/*from ww w. j a  v  a  2  s .  c o  m*/

    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(cls);
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
        String name = propertyDescriptor.getName();
        if ("class".equals(name))
            continue;
        Element propertyDefElement = propertyDefElementMap.get(name);
        if (propertyDefElement == null) {
            String dataTypeName = null;

            DataType propertyDataType = dataTypeManager.getDataType(propertyDescriptor.getPropertyType());
            if (propertyDataType != null) {
                dataTypeName = propertyDataType.getName();
                if (IGNORE_DATATYPES.contains(dataTypeName)) {
                    continue;
                }
            }

            propertyDefElement = document.createElement(DataXmlConstants.PROPERTY_DEF);
            propertyDefElement.setAttribute(XmlConstants.ATTRIBUTE_NAME, name);
            createPropertyElement(propertyDefElement, DataXmlConstants.ATTRIBUTE_DATA_TYPE, dataTypeName);
            element.appendChild(propertyDefElement);
        }
    }
}

From source file:com.yosanai.java.swing.editor.ObjectEditorTableModel.java

@SuppressWarnings("rawtypes")
protected void addRows(BeanWrapper beanWrapper, PropertyDescriptor propertyDescriptor, String prefix,
        Set<Integer> visited) {
    if (StringUtils.isBlank(prefix)) {
        prefix = "";
    } else if (!prefix.endsWith(".")) {
        prefix += ".";
    }//from w w  w.  j a  v a  2  s .c om
    Object propertyValue = beanWrapper.getPropertyValue(propertyDescriptor.getName());
    if (isPrimitive(propertyDescriptor.getPropertyType())) {
        String value = "";
        if (null != propertyValue) {
            if (propertyDescriptor.getPropertyType().isEnum()) {
                value = ((Enum) propertyValue).name();
            } else {
                value = propertyValue.toString();
            }
        }
        addRow(new Object[] { prefix + propertyDescriptor.getName(), value });
    } else if (expandAllProperties) {
        addRows(propertyValue, prefix + propertyDescriptor.getName(), visited);
    }
}

From source file:com.bstek.dorado.data.type.EntityDataTypeSupport.java

protected void doCreatePropertyDefinitons() throws Exception {
    Class<?> type = getMatchType();
    if (type == null) {
        type = getCreationType();/*from ww  w.  j  av a2  s  .co m*/
    }
    if (type == null || type.isPrimitive() || type.isArray() || Map.class.isAssignableFrom(type)) {
        return;
    }

    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(type);
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
        String name = propertyDescriptor.getName();
        if (!BeanPropertyUtils.isValidProperty(type, name))
            continue;

        PropertyDef propertyDef = getPropertyDef(name);
        DataType dataType = null;
        Class<?> propertyType = propertyDescriptor.getPropertyType();
        if (Collection.class.isAssignableFrom(propertyType)) {
            ParameterizedType parameterizedType = (ParameterizedType) propertyDescriptor.getReadMethod()
                    .getGenericReturnType();
            if (parameterizedType != null) {
                dataType = DataUtils.getDataType(parameterizedType);
            }
        }

        if (dataType == null) {
            dataType = DataUtils.getDataType(propertyType);
        }

        if (propertyDef == null) {
            if (dataType != null) {
                propertyDef = new BasePropertyDef(name);
                propertyDef.setDataType(dataType);
                addPropertyDef(propertyDef);

                if (dataType instanceof EntityDataType || dataType instanceof AggregationDataType) {
                    propertyDef.setIgnored(true);
                }
            }
        } else if (propertyDef.getDataType() == null) {
            if (dataType != null)
                propertyDef.setDataType(dataType);
        }
    }
}

From source file:com.gdcn.modules.db.jdbc.processor.CamelBeanProcessor.java

/**
 * Creates a new object and initializes its fields from the ResultSet.
 * @param <T> The type of bean to create
 * @param rs The result set./*from ww w  .j a v  a2  s.  c  om*/
 * @param type The bean type (the return type of the object).
 * @param props The property descriptors.
 * @param columnToProperty The column indices in the result set.
 * @return An initialized object.
 * @throws SQLException if a database error occurs.
 */
private <T> T createBean(ResultSet rs, Class<T> type, PropertyDescriptor[] props, int[] columnToProperty)
        throws SQLException {

    T bean = this.newInstance(type);

    for (int i = 1; i < columnToProperty.length; i++) {

        if (columnToProperty[i] == PROPERTY_NOT_FOUND) {
            continue;
        }

        PropertyDescriptor prop = props[columnToProperty[i]];
        Class<?> propType = prop.getPropertyType();

        Object value = this.processColumn(rs, i, propType);

        if (propType != null && value == null && propType.isPrimitive()) {
            value = primitiveDefaults.get(propType);
        }

        this.callSetter(bean, prop, value);
    }

    return bean;
}

From source file:org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldIntrospectorImpl.java

/**
 * Let's give the user some hints as to what could be wrong.
 *///from  w  ww .  j  a va 2 s  . c  o  m
protected void logProblem(PropertyDescriptor pd, Object anObject, Object aValue, String msg) {
    Logger logger = getLog();
    logger.error("Error in [PersistentFieldPropertyImpl], " + msg);
    logger.error("Declaring class [" + getDeclaringClass().getName() + "]");
    logger.error("Property Name [" + getName() + "]");
    logger.error("Property Type [" + pd.getPropertyType().getName() + "]");

    if (anObject != null) {
        logger.error("anObject was class [" + anObject.getClass().getName() + "]");
    } else {
        logger.error("anObject was null");
    }
    if (aValue != null) {
        logger.error("aValue was class [" + aValue.getClass().getName() + "]");
    } else {
        logger.error("aValue was null");
    }
}

From source file:com.ewcms.common.query.mongo.PropertyConvert.java

/**
 * ?{@link RuntimeException}/* w w  w . j  a v  a  2 s  . c om*/
 * 
 * @param name  ???{@literal null}
 * @return {@value Class<?>}
 */
public Class<?> getPropertyType(String propertyName) {
    if (!StringUtils.hasText(propertyName)) {
        throw new IllegalArgumentException("Property's name must not null or empty!");
    }

    String[] names = StringUtils.tokenizeToStringArray(propertyName, NESTED);
    Class<?> type = beanClass;
    PropertyDescriptor pd = null;
    for (String name : names) {
        pd = BeanUtils.getPropertyDescriptor(type, name);
        if (pd == null) {
            logger.error("\"{}\" property isn't exist.", propertyName);
            throw new RuntimeException(propertyName + " property isn't exist.");
        }
        type = pd.getPropertyType();
    }

    if (type.isArray()) {
        return type.getComponentType();
    }

    if (Collection.class.isAssignableFrom(type)) {
        Method method = pd.getReadMethod();
        if (method == null) {
            logger.error("\"{}\" property is not read method.", propertyName);
            throw new RuntimeException(propertyName + " property is not read method.");
        }
        ParameterizedType returnType = (ParameterizedType) method.getGenericReturnType();
        if (returnType.getActualTypeArguments().length > 0) {
            return (Class<?>) returnType.getActualTypeArguments()[0];
        }
        logger.error("\"{}\" property is collection,but it's not generic.", propertyName);
        throw new RuntimeException(propertyName + " property is collection,but it's not generic.");
    }

    return type;
}

From source file:de.escalon.hypermedia.spring.SpringActionDescriptor.java

/**
 * Recursively navigate to return a BeanWrapper for the nested property path.
 *
 * @param propertyPath/*from w w w .j av a 2s .co m*/
 *         property property path, which may be nested
 * @return a BeanWrapper for the target bean
 */
PropertyDescriptor getPropertyDescriptorForPropertyPath(String propertyPath, Class<?> propertyType) {
    int pos = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(propertyPath);
    // Handle nested properties recursively.
    if (pos > -1) {
        String nestedProperty = propertyPath.substring(0, pos);
        String nestedPath = propertyPath.substring(pos + 1);
        PropertyDescriptor propertyDescriptor = BeanUtils.getPropertyDescriptor(propertyType, nestedProperty);
        //            BeanWrapperImpl nestedBw = getNestedBeanWrapper(nestedProperty);
        return getPropertyDescriptorForPropertyPath(nestedPath, propertyDescriptor.getPropertyType());
    } else {
        return BeanUtils.getPropertyDescriptor(propertyType, propertyPath);
    }
}

From source file:at.molindo.notify.model.BeanParams.java

@Override
public Iterator<ParamValue> iterator() {
    return new Iterator<ParamValue>() {

        private final Iterator<PropertyDescriptor> _iter = getDescriptorsIter();
        private ParamValue _next = findNext();

        private ParamValue findNext() {
            while (_iter.hasNext()) {
                PropertyDescriptor pd = _iter.next();
                if (pd.getWriteMethod() == null || pd.getReadMethod() == null) {
                    continue;
                }// w w  w  .j  a v  a2  s .c o  m
                Object value = invoke(pd.getReadMethod());
                if (value == null) {
                    continue;
                }
                return Param.p(pd.getPropertyType(), pd.getName()).paramValue(value);
            }
            return null;
        }

        @Override
        public boolean hasNext() {
            return _next != null;
        }

        @Override
        public ParamValue next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            ParamValue next = _next;
            _next = findNext();
            return next;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}