1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.util.codingsupport;
18
19 import java.beans.BeanInfo;
20 import java.beans.IntrospectionException;
21 import java.beans.Introspector;
22 import java.beans.PropertyDescriptor;
23 import java.lang.reflect.Array;
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
29
30
31
32
33
34
35
36
37 public final class BeanPropertyUtils {
38
39
40
41
42 private BeanPropertyUtils() { }
43
44
45
46
47
48
49
50 public static Object getProperty(Object entity, String propertyName) {
51 PropertyDescriptor descriptor = getDescriptor(entity, propertyName);
52 Method reader = descriptor.getReadMethod();
53 if (reader == null) {
54 throw new IllegalArgumentException("Property " + propertyName
55 + "is not readable.");
56 }
57
58 boolean accessible = reader.isAccessible();
59 try {
60 reader.setAccessible(true);
61 return reader.invoke(entity, new Object[0]);
62 } catch (IllegalAccessException e) {
63 throw new RuntimeException(e);
64 } catch (InvocationTargetException e) {
65 throw new RuntimeException(e);
66 } finally {
67 reader.setAccessible(accessible);
68 }
69 }
70
71
72
73
74
75
76
77 public static Object getPropertySimplified(Object entity, String propertyName) {
78 Object value = getProperty(entity, propertyName);
79 if (value.getClass().isArray()) {
80
81 List<Object> list = new ArrayList<Object>(((Object[]) value).length);
82 for (Object entry : (Object[]) value) {
83 list.add(entry);
84 }
85 return list;
86 } else {
87 return value;
88 }
89 }
90
91
92
93
94
95
96
97 private static PropertyDescriptor getDescriptor(Object entity, String propertyName) {
98 Class<?> entityClass = entity.getClass();
99 return getDescriptorByClass(entityClass, propertyName);
100 }
101
102
103
104
105
106
107
108 private static PropertyDescriptor getDescriptorByClass(Class<?> entityClass, String propertyName) {
109 BeanInfo info;
110 try {
111 info = Introspector.getBeanInfo(entityClass);
112 } catch (IntrospectionException e) {
113 throw new RuntimeException("Cannot introspect " + entityClass, e);
114 }
115 PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
116 PropertyDescriptor descriptor = null;
117 for (PropertyDescriptor candidate : descriptors) {
118 if (propertyName.equals(candidate.getName())) {
119 descriptor = candidate;
120 break;
121 }
122 }
123 if (descriptor == null) {
124 throw new IllegalArgumentException("No property " + propertyName + " in class "
125 + entityClass);
126 }
127 return descriptor;
128 }
129
130
131
132
133
134
135
136 public static void setProperty(Object entity, String propertyName, Object value) {
137 PropertyDescriptor descriptor = getDescriptor(entity, propertyName);
138 Method writer = descriptor.getWriteMethod();
139 if (writer == null) {
140 throw new IllegalArgumentException("Property " + propertyName
141 + "is not writable.");
142 }
143
144 boolean accessible = writer.isAccessible();
145 try {
146 writer.setAccessible(true);
147 writer.invoke(entity, value);
148 } catch (IllegalAccessException e) {
149 throw new RuntimeException(e);
150 } catch (InvocationTargetException e) {
151 throw new RuntimeException(e);
152 } finally {
153 writer.setAccessible(accessible);
154 }
155 }
156
157
158
159
160
161
162
163
164 public static void setPropertySimplified(Object entity, String propertyName, Object value) {
165 PropertyDescriptor descriptor = getDescriptor(entity, propertyName);
166 Method writer = descriptor.getWriteMethod();
167
168 if (writer.getParameterTypes()[0].isArray() && value instanceof Collection) {
169 Class<?> componentClass = writer.getParameterTypes()[0].getComponentType();
170
171
172
173
174
175 @SuppressWarnings("unchecked")
176 Collection collection = (Collection) value;
177 Object array = Array.newInstance(componentClass, collection.size());
178 int i = 0;
179 for (Object entry : collection) {
180 Array.set(array, i, entry);
181 i++;
182 }
183 setProperty(entity, propertyName, array);
184 } else {
185 setProperty(entity, propertyName, value);
186 }
187 }
188
189
190
191
192
193
194
195 public static Method getReadMethod(Class<?> entityClass, String propertyName) {
196 PropertyDescriptor descriptor = getDescriptorByClass(entityClass, propertyName);
197 return descriptor.getReadMethod();
198 }
199
200
201
202
203
204
205
206 public static Method getWriteMethod(Class<?> entityClass, String propertyName) {
207 PropertyDescriptor descriptor = getDescriptorByClass(entityClass, propertyName);
208 return descriptor.getWriteMethod();
209 }
210 }