Java Code Examples for org.apache.commons.beanutils.PropertyUtils.getPropertyType()

The following are Jave code examples for showing how to use getPropertyType() of the org.apache.commons.beanutils.PropertyUtils class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: TranskribusCore   File: CustomTag.java   View Source Code Vote up 6 votes
public Class<?> getAttributeType(String name) {
	if (!hasAttribute(name))
		return null;

	if (isPredefinedAttribute(name)) { // get type via reflection for
										// predefined attributes
		try {
			return PropertyUtils.getPropertyType(this, name);
		} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
			logger.error(e.getMessage(), e);
			return null;
		}
	} else {
		CustomTagAttribute att = getAttribute(name);
		return att.getType();
	}
}
 
Example 2
Project: QLExpress   File: ExpressUtil.java   View Source Code Vote up 5 votes
public static void setProperty(Object bean, Object name, Object value) {
	try {
		if (bean instanceof Class) {
			Field f = ((Class<?>) bean).getDeclaredField(name.toString());
			f.set(null, value);
		}else if(bean instanceof Map ){
			((Map<Object,Object>)bean).put(name, value);
	    } else {
	    	Class<?> filedClass = PropertyUtils.getPropertyType(bean, name.toString());
			PropertyUtils.setProperty(bean, name.toString(),ExpressUtil.castObject(value, filedClass, false));
		}
	} catch (Exception e) {
		throw new RuntimeException("不能访问" + bean + "的property:" + name,e);
	}
}
 
Example 3
Project: audible   File: TypeSafeCopy.java   View Source Code Vote up 5 votes
@SuppressWarnings("unchecked")
public void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {
    PropertyDescriptor[] origDescriptors =
            PropertyUtils.getPropertyDescriptors(orig);
    for (PropertyDescriptor origDescriptor : origDescriptors) {
        String name = origDescriptor.getName();
        if ("class".equals(name)) {
            continue; // No point in trying to set an object's class
        }
        if (PropertyUtils.isReadable(orig, name) &&
                PropertyUtils.isWriteable(dest, name)) {


            try {
                Class origPropClass = PropertyUtils.getPropertyType(orig, name);
                Class destPropClass = PropertyUtils.getPropertyType(dest, name);

                if (destPropClass.isAssignableFrom(origPropClass)) {
                    Object value =
                            PropertyUtils.getSimpleProperty(orig, name);
                    BeanUtils.copyProperty(dest, name, value);
                }
            } catch (NoSuchMethodException e) {
                // Should not happen
            }
        }
    }
}
 
Example 4
Project: twitter-java-ads-sdk   File: ParameterUtils.java   View Source Code Vote up 5 votes
public static MultivaluedMap<String, String> getMappedValues(Object object) {
	MultivaluedMap<String, String> parameters = new MultivaluedMapImpl();

	for (Method method : object.getClass().getMethods()) {
		if (method.getName().indexOf("set") > -1) {
			String propertyName = method.getName().replace("set", "").toLowerCase();
			try {
				Object value = PropertyUtils.getProperty(object, propertyName);
				if (value != null) {
					Class<?> pt = PropertyUtils.getPropertyType(object, propertyName);
					if (java.util.Date.class.equals(pt)) {
						Date dt = (Date) value;
						DateFormat df = new SimpleDateFormat(TIMESTAMP_DATE_PATTERN);
						String dateAsString = df.format(dt);
						parameters.add(propertyName, dateAsString);
					} else if (pt.isArray()) {
						List<Object> objectList = Arrays.asList((Object[]) value);
						String csvString = Joiner.on(',').join(objectList);
						parameters.add(propertyName, csvString);
					} else {
						parameters.add(propertyName, value.toString());
					}
				}
			} catch (Exception e) {
				log.error(e.toString());
			}
		}
	}
	return parameters;
}
 
Example 5
Project: twitter-java-ads-sdk   File: ParameterUtils.java   View Source Code Vote up 5 votes
public static MultivaluedMap<String, String> getUpdateValues(Object object) {

		MultivaluedMap<String, String> parameters = new MultivaluedMapImpl();

		for (Field field : object.getClass().getDeclaredFields()) {
			if (field.isAnnotationPresent(com.steelhouse.twitter.ads.annotations.Updatable.class)) {

				try {
					String propertyName = field.getName().charAt(0) + field.getName().substring(1);
					Object value = PropertyUtils.getProperty(object, propertyName);

					if (value != null) {
						Class<?> pt = PropertyUtils.getPropertyType(object, propertyName);
						if (java.util.Date.class.equals(pt)) {
							Date dt = (Date) value;
							DateFormat df = new SimpleDateFormat(TIMESTAMP_DATE_PATTERN);
							String dateAsString = df.format(dt);
							parameters.add(propertyName, dateAsString);
						} else if (pt.isArray()) {
							List<Object> objectList = Arrays.asList((Object[]) value);
							String csvString = Joiner.on(',').join(objectList);
							parameters.add(propertyName, csvString);
						} else {
							String propertyString = URLEncoder.encode(value.toString(), "UTF-8").replaceAll("\\+","%20");
							parameters.add(propertyName, propertyString);
						}
					}
				} catch (Exception e) {
					log.error(e.toString());
				}
			}
		}
		return parameters;
	}
 
Example 6
Project: abina-common-util   File: ParamsUtils.java   View Source Code Vote up 5 votes
/**
 * Map内的key与Bean中属性相同的内容复制到BEAN中
 * 对于存在空值的取默认值
 * @param bean Object
 * @param properties Map
 * @param defaultValue String
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
public static void copyMap2Bean(Object bean, Map properties, String defaultValue) throws
    IllegalAccessException, InvocationTargetException {
    // Do nothing unless both arguments have been specified
    if ( (bean == null) || (properties == null)) {
        return;
    }
    // Loop through the property name/value pairs to be set
    Iterator names = properties.keySet().iterator();
    while (names.hasNext()) {
        String name = (String) names.next();
        // Identify the property name and value(s) to be assigned
        if (name == null) {
            continue;
        }
        Object value = properties.get(name);
        try {
            Class clazz = PropertyUtils.getPropertyType(bean, name);
            if (null == clazz) {
                continue;
            }
            String className = clazz.getName();
            if (className.equalsIgnoreCase("java.sql.Timestamp")) {
                if (value == null || value.equals("")) {
                    continue;
                }
            }
            if (className.equalsIgnoreCase("java.lang.String")) {
                if (value == null) {
                    value = defaultValue;
                }
            }
            getInstance().setSimpleProperty(bean, name, value);
        }
        catch (NoSuchMethodException e) {
            continue;
        }
    }
}
 
Example 7
Project: OpenCyclos   File: CSVReader.java   View Source Code Vote up 5 votes
/**
 * Reads a single object from a SCV line
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<E> read(final BufferedReader in) throws CSVParseException {
    int linesIndex = 0;
    final List<E> list = new LinkedList<E>();
    try {

        List<String> values;
        while ((values = readLine(in)) != null) {
            linesIndex++;
            // Ignore the header lines
            if (headerLines >= linesIndex) {
                continue;
            }
            final E object = elementClass.newInstance();
            final int size = Math.min(columns.size(), values.size());
            for (int i = 0; i < size; i++) {
                final Column column = columns.get(i);
                final String property = column.getProperty();
                if (property == null) {
                    continue;
                }
                final Class type = PropertyUtils.getPropertyType(object, property);
                final String stringValue = values.get(i);
                final Object objectValue = PropertyHelper.getAsObject(type, stringValue, column.getConverter());
                PropertyHelper.set(object, property, objectValue);
            }
            list.add(object);
        }
        return list;
    } catch (final Exception e) {
        throw new CSVParseException(linesIndex);
    }
}
 
Example 8
Project: OpenCyclos   File: BaseSettingsHandler.java   View Source Code Vote up 5 votes
/**
 * Populate a settings object, using a Map of converters, and a Map of values. Only 2 levels of beans are supported, ie, xxxSettings.x.y. If there
 * were a nested bean on x, making it be xxxSettings.x.y.z, z would be ignored
 */
private void populate(final Object settings, final Map<String, String> values) {
    for (final Map.Entry<String, Converter<?>> entry : converters.entrySet()) {
        final String name = entry.getKey();
        final Converter<?> converter = entry.getValue();
        if (values.containsKey(name)) {
            final String value = values.get(name);
            final Object realValue = converter.valueOf(value);
            // Check if there is a nested object
            if (name.contains(".")) {
                final String first = PropertyHelper.firstProperty(name);
                // No bean: instantiate it
                if (PropertyHelper.get(settings, first) == null) {
                    try {
                        final Class<?> type = PropertyUtils.getPropertyType(settings, first);
                        final Object bean = type.newInstance();
                        PropertyHelper.set(settings, first, bean);
                    } catch (final Exception e) {
                        LOGGER.warn("Error while setting nested settings bean", e);
                        throw new IllegalStateException();
                    }
                }
            }
            PropertyHelper.set(settings, name, realValue);
        }
    }
}