My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.ndpar.utils.bean;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;

/**
* Extension for apache commons <code>PropertyUtils</code>.
*
* Two reasons of its existence:
* - wrap checked exceptions and throw unchecked ones;
* - provide missing methods.
*
* @author Andrey Paramononov
* @since 1.0
*/
public abstract class ExtendedPropertyUtils {

/**
* <p>Copy property values from the "source" bean to the "destination" bean.
*
* @exception IllegalArgumentException if underlying class throws any exception
* @see PropertyUtils#copyProperties(Object, Object)
*/
public static void copyBeanProperties(Object source, Object destination) {
try {
PropertyUtils.copyProperties(destination, source);
} catch (Exception e) {
throw new IllegalArgumentException("Cannot merge " + source + " -> " + destination, e);
}
}

/**
* Retrieve the property descriptors for the specified class and property type.
* @see PropertyUtils#getPropertyDescriptors(Class)
*/
@SuppressWarnings("unchecked")
public static List<PropertyDescriptor> getPropertyDescriptors(Class beanClass, Class propertyClass) {
PropertyDescriptor[] allPropertyDescriptors = PropertyUtils.getPropertyDescriptors(beanClass);
List<PropertyDescriptor> result = new ArrayList<PropertyDescriptor>();
for (PropertyDescriptor propertyDescriptor : allPropertyDescriptors) {
if (propertyClass.equals(propertyDescriptor.getPropertyType())) {
result.add(propertyDescriptor);
}
}
return result;
}

/**
* Replace oldValue by newValue on the given bean for all properties of propertyClass class.
* Supports null for both oldValue and newValue.
*/
@SuppressWarnings("unchecked")
public static void replacePropertyValue(Object bean, Class propertyClass, Object oldValue, Object newValue) {
try {
List<PropertyDescriptor> pds = getPropertyDescriptors(bean.getClass(), propertyClass);
for (PropertyDescriptor propertyDescriptor : pds) {
Method readMethod = propertyDescriptor.getReadMethod();
Object propertyValue = readMethod.invoke(bean);
if ((propertyValue == null && oldValue == null) || (propertyValue != null && propertyValue.equals(oldValue))) {
Method writeMethod = propertyDescriptor.getWriteMethod();
writeMethod.invoke(bean, newValue);
}
}
} catch (Exception e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}

/**
* Replace oldValue by newValue on the given bean for all properties of type Double.
*/
public static void replacePropertyValue(Object bean, Double oldValue, Double newValue) {
replacePropertyValue(bean, Double.class, oldValue, newValue);
}

/**
* Replace oldValue by newValue on the given bean for all properties of type Integer.
*/
public static void replacePropertyValue(Object bean, Integer oldValue, Integer newValue) {
replacePropertyValue(bean, Integer.class, oldValue, newValue);
}
}

Change log

r9 by andrey.paramonov on Jul 23, 2009   Diff
Created ExtendedPropertyUtils class
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 3459 bytes, 86 lines
Powered by Google Project Hosting