本文整理汇总了Java中org.apache.commons.beanutils.PropertyUtils.getPropertyType方法的典型用法代码示例。如果您正苦于以下问题:Java PropertyUtils.getPropertyType方法的具体用法?Java PropertyUtils.getPropertyType怎么用?Java PropertyUtils.getPropertyType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.beanutils.PropertyUtils
的用法示例。
在下文中一共展示了PropertyUtils.getPropertyType方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAttributeType
import org.apache.commons.beanutils.PropertyUtils; //导入方法依赖的package包/类
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();
}
}
示例2: read
import org.apache.commons.beanutils.PropertyUtils; //导入方法依赖的package包/类
/**
* 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);
}
}
示例3: populate
import org.apache.commons.beanutils.PropertyUtils; //导入方法依赖的package包/类
/**
* 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);
}
}
}
示例4: setProperty
import org.apache.commons.beanutils.PropertyUtils; //导入方法依赖的package包/类
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);
}
}
示例5: copyProperties
import org.apache.commons.beanutils.PropertyUtils; //导入方法依赖的package包/类
@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
}
}
}
}
示例6: getMappedValues
import org.apache.commons.beanutils.PropertyUtils; //导入方法依赖的package包/类
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;
}
示例7: getUpdateValues
import org.apache.commons.beanutils.PropertyUtils; //导入方法依赖的package包/类
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;
}
示例8: copyMap2Bean
import org.apache.commons.beanutils.PropertyUtils; //导入方法依赖的package包/类
/**
* 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;
}
}
}