01: package liquibase.util;
02:
03: import java.lang.reflect.InvocationTargetException;
04: import java.lang.reflect.Method;
05:
06: public class ObjectUtil {
07:
08: public static void setProperty(Object object, String propertyName,
09: String propertyValue) throws IllegalAccessException,
10: InvocationTargetException {
11: String methodName = "set"
12: + propertyName.substring(0, 1).toUpperCase()
13: + propertyName.substring(1);
14: Method[] methods = object.getClass().getMethods();
15: for (Method method : methods) {
16: if (method.getName().equals(methodName)) {
17: if (method.getParameterTypes().length == 1
18: && method.getParameterTypes()[0]
19: .equals(Boolean.class)) {
20: method.invoke(object, Boolean
21: .valueOf(propertyValue));
22: return;
23: } else if (method.getParameterTypes().length == 1
24: && method.getParameterTypes()[0]
25: .equals(String.class)) {
26: method.invoke(object, propertyValue);
27: return;
28: } else if (method.getParameterTypes().length == 1
29: && method.getParameterTypes()[0]
30: .equals(Integer.class)) {
31: method.invoke(object, Integer
32: .valueOf(propertyValue));
33: return;
34: }
35: }
36: }
37: throw new RuntimeException("Property not found: "
38: + propertyName);
39: }
40: }
|