List of usage examples for org.apache.commons.beanutils PropertyUtils getSimpleProperty
public static Object getSimpleProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Return the value of the specified simple property of the specified bean, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:pt.ist.maidSyncher.domain.SynchableObject.java
/** * //from w ww . j a v a 2s . com * @param orig * @return a collection with the {@link PropertyDescriptor} s that changed * @throws IllegalAccessException * @throws InvocationTargetException * @throws NoSuchMethodException * @throws TaskNotVisibleException */ public Collection<String> copyPropertiesFrom(Object orig) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, TaskNotVisibleException { Set<PropertyDescriptor> propertyDescriptorsThatChanged = new HashSet<PropertyDescriptor>(); Object dest = this; if (orig == null) { throw new IllegalArgumentException("No origin bean specified"); } PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig); for (PropertyDescriptor origDescriptor : origDescriptors) { String name = origDescriptor.getName(); if (PropertyUtils.isReadable(orig, name)) { if (PropertyUtils.isWriteable(dest, name)) { Object valueDest = PropertyUtils.getSimpleProperty(dest, name); Object valueOrigin = PropertyUtils.getSimpleProperty(orig, name); if (valueOrigin != null && valueOrigin.getClass().getPackage().getName() .equalsIgnoreCase("org.eclipse.egit.github.core")) continue; //let's skip the properties with egit core objects (they shall be copied from a custom overriden version of this method) if (valueOrigin instanceof Date) valueOrigin = new DateTime(valueOrigin); if (Objects.equal(valueDest, valueOrigin) == false) propertyDescriptorsThatChanged.add(origDescriptor); try { //let's see if this is actually a Date, if so, let's convert it PropertyUtils.setSimpleProperty(dest, name, valueOrigin); } catch (IllegalArgumentException ex) { throw new Error("setSimpleProperty returned an exception, dest: " + dest.getClass().getName() + " oid: " + ((DomainObject) dest).getExternalId() + " name : " + name + " valueOrig: " + valueOrigin, ex); } LOGGER.trace("Copied property " + name + " from " + orig.getClass().getName() + " object to a " + dest.getClass().getName() + " oid: " + getExternalId()); } } } return Collections2.transform(propertyDescriptorsThatChanged, new Function<PropertyDescriptor, String>() { @Override public String apply(PropertyDescriptor propertyDescriptor) { if (propertyDescriptor == null) { return null; } return propertyDescriptor.getName(); } }); }
From source file:pt.ist.maidSyncher.domain.SynchableObject.java
public void copyPropertiesTo(Object dest) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, TaskNotVisibleException { Set<PropertyDescriptor> propertyDescriptorsThatChanged = new HashSet<PropertyDescriptor>(); Object orig = this; checkNotNull(dest);// www.j a v a2s .c o m PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig); for (PropertyDescriptor origDescriptor : origDescriptors) { String name = origDescriptor.getName(); if (PropertyUtils.isReadable(orig, name)) { PropertyDescriptor destDescriptor = PropertyUtils.getPropertyDescriptor(dest, origDescriptor.getName()); if (PropertyUtils.isWriteable(dest, name) || (destDescriptor != null && MiscUtils.getWriteMethodIncludingFlowStyle(destDescriptor, dest.getClass()) != null)) { Object valueDest = PropertyUtils.getSimpleProperty(dest, name); Object valueOrigin = PropertyUtils.getSimpleProperty(orig, name); LOGGER.debug("OrigDescriptor PropertyType: " + origDescriptor.getPropertyType().getName()); // System.out.println("OrigDescriptor PropertyType: " + origDescriptor.getPropertyType().getName()); //let's ignore the properties were the values are our domain packages if (valueOrigin != null && (SynchableObject.class.isAssignableFrom(valueOrigin.getClass()))) { // System.out.println("Skipping"); continue; //let's skip these properties } if (SynchableObject.class.isAssignableFrom(origDescriptor.getPropertyType())) { // System.out.println("Skipping"); continue; } if (origDescriptor instanceof IndexedPropertyDescriptor) { IndexedPropertyDescriptor indexedPropertyDescriptor = (IndexedPropertyDescriptor) origDescriptor; // System.out.println("OrigDescriptor IndexedPropertyDescriptor: " + indexedPropertyDescriptor.getName()); if (SynchableObject.class .isAssignableFrom(indexedPropertyDescriptor.getIndexedPropertyType())) { // System.out.println("Skipping"); continue; } } //let's ignore all of the dates - as they should be filled by //the system if (valueOrigin instanceof LocalTime) continue; if (Objects.equal(valueDest, valueOrigin) == false) propertyDescriptorsThatChanged.add(origDescriptor); try { if (PropertyUtils.isWriteable(dest, name) == false) { //let's use the flow version Class<?> origPropertyType = origDescriptor.getPropertyType(); Method writeMethodIncludingFlowStyle = MiscUtils .getWriteMethodIncludingFlowStyle(destDescriptor, dest.getClass()); if (Arrays.asList(writeMethodIncludingFlowStyle.getParameterTypes()) .contains(origPropertyType)) { writeMethodIncludingFlowStyle.invoke(dest, valueOrigin); } else { continue; } } else { PropertyUtils.setSimpleProperty(dest, name, valueOrigin); } } catch (IllegalArgumentException ex) { throw new Error("setSimpleProperty returned an exception, dest: " + dest.getClass().getName() + " name : " + name + " valueOrig: " + valueOrigin, ex); } LOGGER.trace("Copied property " + name + " from " + orig.getClass().getName() + " object to a " + dest.getClass().getName() + " oid: " + getExternalId()); } // System.out.println("--"); } } }
From source file:pt.ist.maidSyncher.domain.SynchableObject.java
/** * /*from w w w. j a va 2s. c o m*/ * @throws IllegalAccessException * @throws InvocationTargetException * @throws NoSuchMethodException * @throws TaskNotVisibleException * @throws IllegalArgumentException if the property descriptors of the arguments differ * * @return a collection of {@link PropertyDescriptor} that changed, or an empty collection */ public static Collection<PropertyDescriptor> propertiesEqual(Object object1, Object object2) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, TaskNotVisibleException, IllegalArgumentException { List<PropertyDescriptor> changedDescriptors = new ArrayList<>(); if (object1 == null || object2 == null) { throw new IllegalArgumentException("Both objects must be non null"); } PropertyDescriptor object1Descriptors[] = PropertyUtils.getPropertyDescriptors(object1); PropertyDescriptor object2Descriptors[] = PropertyUtils.getPropertyDescriptors(object2); //let's make sure that they match checkArgument(ObjectUtils.equals(object1Descriptors, object2Descriptors), "Error, object1 : " + object1.getClass().getSimpleName() + " and object2 : " + object2.getClass().getSimpleName() + " don't match"); for (PropertyDescriptor object1Descriptor : object1Descriptors) { String name = object1Descriptor.getName(); if (PropertyUtils.isReadable(object1, name) && PropertyUtils.isReadable(object2, name)) { //if both are readable, let's check on the values Object valueObject1 = PropertyUtils.getSimpleProperty(object1, name); Object valueObject2 = PropertyUtils.getSimpleProperty(object2, name); // if (isGitHubObject(valueObject1) || isGitHubObject(valueObject2)) // continue; //let's skip the GitHub properties, we won't be able to compare that if (!ObjectUtils.equals(valueObject1, valueObject2)) changedDescriptors.add(object1Descriptor); } } return changedDescriptors; }
From source file:see.properties.impl.BeanPropertyResolver.java
@Override public Object get(Object bean, PropertyAccess property) { try {//from w w w . j av a 2 s . c om return PropertyUtils.getSimpleProperty(bean, getPropertyName(property)); } catch (Exception e) { throw Throwables.propagate(e); } }
From source file:service.ExcelWorkSheetHandler.java
private String getPropertyValue(Object targetObj, String propertyName) { String value = ""; if (null == targetObj || StringUtils.isBlank(propertyName)) { LOG.error("targetObj or propertyName is null, both require to retrieve a value"); return value; }//from ww w . ja va2 s .c om if (PropertyUtils.isReadable(targetObj, propertyName)) { Object v = null; try { v = PropertyUtils.getSimpleProperty(targetObj, propertyName); } catch (IllegalAccessException ex) { Logger.getLogger(ExcelWorkSheetHandler.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { Logger.getLogger(ExcelWorkSheetHandler.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchMethodException ex) { Logger.getLogger(ExcelWorkSheetHandler.class.getName()).log(Level.SEVERE, null, ex); } if (null != v && StringUtils.isNotBlank(v.toString())) { value = v.toString(); } } else { LOG.error("Given property (" + propertyName + ") is not readable!"); } return value; }