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

The following are Jave code examples for showing how to use isWriteable() 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: nixmash-blog   File: SiteOptions.java   View Source Code Vote up 6 votes
public void setSiteOptionProperty(String property, Object value)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    if (PropertyUtils.isWriteable(this, property)) {

        switch (PropertyUtils
                .getPropertyDescriptor(this, property).getPropertyType().getSimpleName()) {
            case OPTION_VALUE_TYPE_BOOLEAN:
                value = Boolean.valueOf(value.toString());
                break;
            case OPTION_VALUE_TYPE_INTEGER:
                value = Integer.parseInt(value.toString());
                break;
            case OPTION_VALUE_TYPE_USERREGISTRATION:
                value = UserRegistration.valueOf(value.toString());
                break;
            default:
                break;
        }
        PropertyUtils.setProperty(this, property, value);
    }
}
 
Example 2
Project: abina-common-util   File: ParamsUtils.java   View Source Code Vote up 6 votes
/**
 * 对象拷贝 数据对象空值不拷贝到目标对象
 * 
 * @param databean 待拷贝对象
 * @param tobean 目标对象
 * @throws NoSuchMethodException
 */
public static void copyBeanNotNull2Bean(Object databean, Object tobean) throws Exception {
	PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(databean);
	for (int i = 0; i < origDescriptors.length; i++) {
		String name = origDescriptors[i].getName();
		// String type = origDescriptors[i].getPropertyType().toString();
		if ("class".equals(name)) {
			continue; // No point in trying to set an object's class
		}
		if (PropertyUtils.isReadable(databean, name) && PropertyUtils.isWriteable(tobean, name)) {
			try {
				Object value = PropertyUtils.getSimpleProperty(databean, name);
				if (value != null) {
					getInstance().setSimpleProperty(tobean, name, value);
				}
			} catch (java.lang.IllegalArgumentException ie) {
				; // Should not happen
			} catch (Exception e) {
				; // Should not happen
			}

		}
	}
}
 
Example 3
Project: stallion-core   File: MappedModel.java   View Source Code Vote up 5 votes
public default Object put(String key, Object value) {
    try {
        if (key.equals("id") || key.equals("lastModifiedMillis") && !(value instanceof Long)) {
            if (value instanceof Double) {
                value = ((Double) value).longValue();
            } else if (value instanceof Integer) {
                value = ((Integer) value).longValue();
            } else if (value instanceof Float) {
                value = ((Float) value).longValue();
            } else if (value instanceof BigInteger) {
                value = ((BigInteger) value).longValue();
            }

        }
        if (PropertyUtils.isWriteable(this, key)) {
            Class cls = null;
            if (value != null) {
                cls = value.getClass();
            }
            PropertyUtils.setProperty(this, key, value);
            return null;
        } else {
            return getAttributes().put(key, value);
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 4
Project: tkhoon   File: ObjectUtil.java   View Source Code Vote up 5 votes
public static void setField(Object obj, String fieldName, Object fieldValue) {
    try {
        if (PropertyUtils.isWriteable(obj, fieldName)) {
            PropertyUtils.setProperty(obj, fieldName, fieldValue);
        }
    } catch (Exception e) {
        logger.error("设置成员变量出错!", e);
        throw new RuntimeException(e);
    }
}
 
Example 5
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 6
Project: MMServerEngine   File: ObjectUtil.java   View Source Code Vote up 5 votes
/**
 * 设置成员变量
 */
public static void setField(Object obj, String fieldName, Object fieldValue) {
    try {
        if (PropertyUtils.isWriteable(obj, fieldName)) {
            PropertyUtils.setProperty(obj, fieldName, fieldValue);
        }
    } catch (Exception e) {
        logger.error("设置成员变量出错!", e);
        throw new RuntimeException(e);
    }
}
 
Example 7
Project: alfresco-repository   File: ComplexContentTransformer.java   View Source Code Vote up 4 votes
/**
 * Sets any transformation option overrides it can.
 */
private void overrideTransformationOptions(TransformationOptions options)
{
    // Set any transformation options overrides if we can
    if(options != null && transformationOptionOverrides != null)
    {
       for(String key : transformationOptionOverrides.keySet())
       {
          if(PropertyUtils.isWriteable(options, key))
          {
             try 
             {
                PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(options, key);
                Class<?> propertyClass = pd.getPropertyType();
                
                Object value = transformationOptionOverrides.get(key);
                if(value != null)
                {
                    if(propertyClass.isInstance(value))
                    {
                        // Nothing to do
                    }
                    else if(value instanceof String && propertyClass.isInstance(Boolean.TRUE))
                    {
                        // Use relaxed converter
                        value = TransformationOptions.relaxedBooleanTypeConverter.convert((String)value);
                    }
                    else
                    {
                        value = DefaultTypeConverter.INSTANCE.convert(propertyClass, value);
                    }
                }
                PropertyUtils.setProperty(options, key, value);
             } 
             catch(MethodNotFoundException mnfe) {}
             catch(NoSuchMethodException nsme) {}
             catch(InvocationTargetException ite) {}
             catch(IllegalAccessException iae) {}
          }
          else
          {
             logger.warn("Unable to set override Transformation Option " + key + " on " + options);
          }
       }
    }
}
 
Example 8
Project: CriteriaBuilder   File: CriteriaServiceImpl.java   View Source Code Vote up 4 votes
static boolean propertyExists(Object bean, String property) {
    return PropertyUtils.isReadable(bean, property) && 
           PropertyUtils.isWriteable(bean, property); 
}
 
Example 9
Project: TranskribusSwtGui   File: Utils.java   View Source Code Vote up 4 votes
public static boolean propertyExists(Object bean, String property) {
	return PropertyUtils.isReadable(bean, property) && PropertyUtils.isWriteable(bean, property);
}
 
Example 10
Project: airtable.java   File: Table.java   View Source Code Vote up 2 votes
/**
 * Check if writable property exists.
 *
 * @param bean bean to inspect
 * @param property name of property
 * @return true if writable property exists.
 */
private static boolean propertyExists(Object bean, String property) {
    return PropertyUtils.isReadable(bean, property)
            && PropertyUtils.isWriteable(bean, property);
}