boolean test = PropertyUtils.isWriteable(top, name); if (!test) throw new NoSuchMethodException("Property " + name + " can't be set");
} else if (url.contains(":firebirdsql:")) { PropertyUtils.setProperty(ds, "databaseName", url.replace("jdbc:firebirdsql:", "")); } else if (PropertyUtils.isWriteable(ds, "URL")) { PropertyUtils.setProperty(ds, "URL", url); } else if (PropertyUtils.isWriteable(ds, "Url")) { PropertyUtils.setProperty(ds, "Url", url); } else if (PropertyUtils.isWriteable(ds, "url")) { PropertyUtils.setProperty(ds, "url", url); } else if (PropertyUtils.isWriteable(ds, "serverName") && PropertyUtils.isWriteable(ds, "portNumber") && PropertyUtils.isWriteable(ds, "databaseName") && URL_PATTERN.matcher(url).matches()) { if (PropertyUtils.isWriteable(ds, "userName")) { PropertyUtils.setProperty(ds, "userName", userName); } else if (PropertyUtils.isWriteable(ds, "user")) { PropertyUtils.setProperty(ds, "user", userName); } else { if (PropertyUtils.isWriteable(ds, "password")) { PropertyUtils.setProperty(ds, "password", password); } else {
private static boolean isPropertyWritable(Object bean, String name) { try { return PropertyUtils.isWriteable(bean, name); } catch (NestedNullException e) { return false; } }
public static boolean isWriteable(Object bean, String name) { return org.apache.commons.beanutils.PropertyUtils.isWriteable(bean, name); }
private static void diffProperty(Object empty, Object to, Object from, PropertyDescriptor descriptor) throws IllegalStateException { if(!PropertyUtils.isReadable(to, descriptor.getName()) || !PropertyUtils.isWriteable(to, descriptor.getName())) { LOGGER.warn("Can't access getter/setter of property {} for bean {}", descriptor.getName(), to); return; } try { Method readMethod = descriptor.getReadMethod(); Object fromValue = readMethod.invoke(from); Object toValue = readMethod.invoke(to); if(fromValue != null && !fromValue.equals(toValue)) { Method writeMethod = descriptor.getWriteMethod(); writeMethod.invoke(empty, fromValue); } } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { throw new IllegalStateException("Failed to diff property", ex); } }
/** * Set a property on the bean only if the property exists * * @param bean the bean * @param propName the name of the property * @param value the property's value * @throws ConfigurationRuntimeException if the property is not writeable or * an error occurred */ public static void setProperty(Object bean, String propName, Object value) { if (PropertyUtils.isWriteable(bean, propName)) { initProperty(bean, propName, value); } }
/** * Set a property on the bean only if the property exists * * @param bean the bean * @param propName the name of the property * @param value the property's value * @throws ConfigurationRuntimeException if the property is not writeable or * an error occurred */ public static void setProperty(Object bean, String propName, Object value) { if (PropertyUtils.isWriteable(bean, propName)) { initProperty(bean, propName, value); } }
/** * Set a property on a tool instance * @param tool tool instance * @param name property name * @param value property value * @throws Exception if setting the property throwed */ protected void setProperty(Object tool, String name, Object value) throws Exception { if (PropertyUtils.isWriteable(tool, name)) { //TODO? support property conversion here? // heavy-handed way is BeanUtils.copyProperty(...) PropertyUtils.setProperty(tool, name, value); } }
/** * Returns true if it was able to set the property * * @param source * @param value * @param propNames * @return * @throws IllegalAccessException * @throws InvocationTargetException * @throws NoSuchMethodException */ protected boolean attemptToSetProperty(Object source, Object value, String... propNames) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { for (String propName : propNames) { if (PropertyUtils.isWriteable(source, propName)) { PropertyUtils.setProperty(source, propName, value); return true; } } return false; }
private static void updateProperty(Object origin, Object update, PropertyDescriptor descriptor) throws IllegalStateException { if(!PropertyUtils.isReadable(origin, descriptor.getName()) || !PropertyUtils.isWriteable(origin, descriptor.getName())) { LOGGER.warn("Can't access getter/setter of propert {} for bean {}", descriptor.getName(), origin); return; } try { Method readMethod = descriptor.getReadMethod(); Object updateValue = readMethod.invoke(update); Object originalValue = readMethod.invoke(origin); if(updateValue != null && !updateValue.equals(originalValue)) { Method writeMethod = descriptor.getWriteMethod(); writeMethod.invoke(origin, updateValue); } } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { throw new IllegalStateException("Failed to update property", ex); } }
private static Object cloneBean(Object object, Object cloneOriginalBean, Object originalBean) { try { // Object cloneBean = object.getClass().newInstance(); Object cloneBean = object.getClass().getConstructor().newInstance(); Object bean = retornarObjeto(object); Map<?, ?> describe = BeanUtils.describe(bean); for (Map.Entry<?, ?> entry : describe.entrySet()) { Object propertyValue = PropertyUtils.getProperty(bean, entry.getKey().toString()); if (propertyValue != null && !propertyValue.getClass().isAssignableFrom(Class.class) && PropertyUtils.isWriteable(cloneBean, entry.getKey().toString())) { setProperty(cloneOriginalBean, originalBean, cloneBean, entry, propertyValue); } } return cloneBean; } catch (Exception ex) { throw new RuntimeException(ex); } }
private static Object cloneBean(Object object, Object cloneOriginalBean, Object originalBean) { try { // Object cloneBean = object.getClass().newInstance(); Object cloneBean = object.getClass().getConstructor().newInstance(); Object bean = retornarObjeto(object); Map<?, ?> describe = BeanUtils.describe(bean); for (Map.Entry<?, ?> entry : describe.entrySet()) { Object propertyValue = PropertyUtils.getProperty(bean, entry.getKey().toString()); if (propertyValue != null && !propertyValue.getClass().isAssignableFrom(Class.class) && PropertyUtils.isWriteable(cloneBean, entry.getKey().toString())) { setProperty(cloneOriginalBean, originalBean, cloneBean, entry, propertyValue); } } return cloneBean; } catch (Exception ex) { throw new RuntimeException(ex); } }
/** * If {@link #isIgnoreUnknownProperties()} returns true, make sure that * every non-ignored ({@see #addIgnoreProperty(String)}) property * matches a writable property on the target bean. * @param bean the bean to validate * @param attributes the list of properties to validate * @throws JellyTagException when a property is not writeable */ protected void validateBeanProperties(Object bean, Map attributes) throws JellyTagException { if (!isIgnoreUnknownProperties()) { for (Iterator i=attributes.keySet().iterator();i.hasNext();) { String attrName = (String)i.next(); if (! PropertyUtils.isWriteable(bean, attrName)) { throw new JellyTagException("No bean property found: " + attrName); } } } }
/** * If {@link #isIgnoreUnknownProperties()} returns true, make sure that * every non-ignored ({@see #addIgnoreProperty(String)}) property * matches a writable property on the target bean. * @param bean the bean to validate * @param attributes the list of properties to validate * @throws JellyTagException when a property is not writeable */ protected void validateBeanProperties(Object bean, Map attributes) throws JellyTagException { if (!isIgnoreUnknownProperties()) { for (Iterator i=attributes.keySet().iterator();i.hasNext();) { String attrName = (String)i.next(); if (! PropertyUtils.isWriteable(bean, attrName)) { throw new JellyTagException("No bean property found: " + attrName); } } } }
/** * If {@link #isIgnoreUnknownProperties()} returns true, make sure that * every non-ignored ({@see #addIgnoreProperty(String)}) property * matches a writable property on the target bean. * @param bean the bean to validate * @param attributes the list of properties to validate * @throws JellyTagException when a property is not writeable */ protected void validateBeanProperties(Object bean, Map attributes) throws JellyTagException { if (!isIgnoreUnknownProperties()) { for (Iterator i=attributes.keySet().iterator();i.hasNext();) { String attrName = (String)i.next(); if (! PropertyUtils.isWriteable(bean, attrName)) { throw new JellyTagException("No bean property found: " + attrName); } } } }
/** * If {@link #isIgnoreUnknownProperties()} returns true, make sure that * every non-ignored ({@see #addIgnoreProperty(String)}) property * matches a writable property on the target bean. * @param bean the bean to validate * @param attributes the list of properties to validate * @throws JellyTagException when a property is not writeable */ protected void validateBeanProperties(Object bean, Map attributes) throws JellyTagException { if (!isIgnoreUnknownProperties()) { for (Iterator i=attributes.keySet().iterator();i.hasNext();) { String attrName = (String)i.next(); if (! PropertyUtils.isWriteable(bean, attrName)) { throw new JellyTagException("No bean property found: " + attrName); } } } }
private <T> Map<String, Object> convertMap(T param) throws Exception { Map<String, Object> description = new LinkedHashMap<String, Object>(); PropertyDescriptor[] descriptors = PropertyUtils .getPropertyDescriptors(param); for (int i = 0; i < descriptors.length; i++) { String name = descriptors[i].getName(); if (PropertyUtils.isReadable(param, name) && PropertyUtils.isWriteable(param, name)) { if (descriptors[i].getReadMethod() != null) { Object value = PropertyUtils.getProperty(param, name); description.put(name, value); description.put(nameStrategy.getFieldName(name), value); } } } return description; }
/** * This method builds a map of business object with its property names and values * * @param businessObject the given business object * @return the map of business object with its property names and values */ public static LinkedHashMap buildPropertyMap(Object businessObject) { DynaClass dynaClass = WrapDynaClass.createDynaClass(businessObject.getClass()); DynaProperty[] properties = dynaClass.getDynaProperties(); LinkedHashMap propertyMap = new LinkedHashMap(); try { for (int numOfProperty = 0; numOfProperty < properties.length; numOfProperty++) { String propertyName = properties[numOfProperty].getName(); if (PropertyUtils.isWriteable(businessObject, propertyName)) { Object propertyValue = PropertyUtils.getProperty(businessObject, propertyName); propertyMap.put(propertyName, propertyValue); } } } catch (Exception e) { LOG.error("OJBUtility.buildPropertyMap()" + e); } return propertyMap; }
protected void copyParametersToBO(Map<String, String> parameters, PersistableBusinessObject newBO) throws Exception{ for (String parmName : parameters.keySet()) { String propertyValue = parameters.get(parmName); if (StringUtils.isNotBlank(propertyValue)) { String propertyName = parmName; // set value of property in bo if (PropertyUtils.isWriteable(newBO, propertyName)) { Class type = ObjectUtils.easyGetPropertyType(newBO, propertyName); if (type != null && Formatter.getFormatter(type) != null) { Formatter formatter = Formatter.getFormatter(type); Object obj = formatter.convertFromPresentationFormat(propertyValue); ObjectUtils.setObjectProperty(newBO, propertyName, obj.getClass(), obj); } else { ObjectUtils.setObjectProperty(newBO, propertyName, String.class, propertyValue); } } } } }
public static Object deepCopy(Object orig) throws Exception { Object dest = orig.getClass().newInstance(); PropertyDescriptor[] origDescriptors = PropertyUtils .getPropertyDescriptors(orig); for (PropertyDescriptor propertyDescriptor : origDescriptors) { String name = propertyDescriptor.getName(); if (PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name)) { Object value = PropertyUtils.getSimpleProperty(orig, name); Object valueDest = null; if (value != null && canDeepCopyObject(value)) { valueDest=deepCopy(value); } else { valueDest = value; } PropertyUtils.setSimpleProperty(dest, name, valueDest); } } return dest; }