1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.util;
17
18 import org.springframework.beans.BeanWrapper;
19 import org.springframework.beans.BeanWrapperImpl;
20 import org.springframework.beans.BeansException;
21
22 import java.beans.PropertyDescriptor;
23 import java.util.Map;
24
25
26
27
28
29
30
31 public class ObjectPropertyUtils {
32 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ObjectPropertyUtils.class);
33
34 public static void copyPropertiesToObject(Map<String, String> properties, Object object) {
35 for (Map.Entry<String, String> property : properties.entrySet()) {
36 setPropertyValue(object, property.getKey(), property.getValue());
37 }
38 }
39
40 public static PropertyDescriptor[] getPropertyDescriptors(Object object) {
41 return wrapObject(object).getPropertyDescriptors();
42 }
43
44 public static Class<?> getPropertyType(Class<?> object, String propertyPath) {
45 return new BeanWrapperImpl(object).getPropertyType(propertyPath);
46 }
47
48 public static Class<?> getPropertyType(Object object, String propertyPath) {
49 return wrapObject(object).getPropertyType(propertyPath);
50 }
51
52 @SuppressWarnings("unchecked")
53 public static <T extends Object> T getPropertyValue(Object object, String propertyPath) {
54 T result = null;
55 try {
56 result = (T) wrapObject(object).getPropertyValue(propertyPath);
57 } catch (RuntimeException e) {
58 throw new RuntimeException("Error getting property '" + propertyPath + "' from " + object, e);
59 }
60 return result;
61 }
62
63 public static void initializeProperty(Object object, String propertyPath) {
64 Class<?> propertyType = getPropertyType(object, propertyPath);
65 try {
66 setPropertyValue(object, propertyPath, propertyType.newInstance());
67 }
68 catch (Exception e) {
69 throw new RuntimeException("Unable to set new instance for property: " + propertyPath, e);
70 }
71 }
72
73 public static void setPropertyValue(Object object, String propertyPath, Object propertyValue) {
74 wrapObject(object).setPropertyValue(propertyPath, propertyValue);
75 }
76
77 public static void setPropertyValue(Object object, String propertyPath, Object propertyValue, boolean ignoreUnknown) {
78 try {
79 wrapObject(object).setPropertyValue(propertyPath, propertyValue);
80 }
81 catch (BeansException e) {
82
83 if (!ignoreUnknown) {
84 throw new RuntimeException(e);
85 }
86 if (LOG.isTraceEnabled()) {
87 LOG.trace("Ignoring exception thrown during setting of property '" + propertyPath + "': "
88 + e.getLocalizedMessage());
89 }
90 }
91 }
92
93 public static boolean isReadableProperty(Object object, String propertyPath) {
94 return wrapObject(object).isReadableProperty(propertyPath);
95 }
96
97 public static boolean isWritableProperty(Object object, String propertyPath) {
98 return wrapObject(object).isWritableProperty(propertyPath);
99 }
100
101 public static BeanWrapper wrapObject(Object object) {
102 BeanWrapper beanWrapper = new BeanWrapperImpl(object);
103 beanWrapper.setAutoGrowNestedPaths(true);
104
105 return beanWrapper;
106 }
107
108 }