1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.openpermis.editor.policy.beans; |
11 | |
|
12 | |
import java.lang.reflect.InvocationTargetException; |
13 | |
import java.lang.reflect.Method; |
14 | |
|
15 | |
import org.slf4j.Logger; |
16 | |
import org.slf4j.LoggerFactory; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
public class PropertyAccess { |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 1 | private static final Logger LOGGER = |
31 | |
LoggerFactory.getLogger(PropertyAccess.class); |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
private final Object bean; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 22 | public PropertyAccess (Object bean) { |
49 | 22 | if (bean == null) { |
50 | 1 | throw new IllegalArgumentException("Illegal bean [null]."); |
51 | |
} |
52 | 21 | if (!BeanUtilities.isJavaBean(bean)) { |
53 | 1 | throw new IllegalArgumentException( |
54 | |
"Illegal bean object of class [" + bean.getClass() + "], this is not a bean." |
55 | |
); |
56 | |
} |
57 | 20 | this.bean = bean; |
58 | 20 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
protected void warn (String message, Throwable cause) { |
67 | 0 | LOGGER.warn(message, cause); |
68 | 0 | } |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
private void checkProperty (String property) { |
79 | 21 | if (property == null) { |
80 | 2 | throw new IllegalArgumentException("Illegal property [null]."); |
81 | 19 | } else if (property.length() == 0) { |
82 | 1 | throw new IllegalArgumentException("Illegal empty property."); |
83 | |
} |
84 | 18 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public boolean canGetProperty (String property) { |
93 | 0 | checkProperty(property); |
94 | 0 | return BeanUtilities.getter(this.bean.getClass(), property) != null; |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
public boolean canSetProperty (String property) { |
104 | 0 | checkProperty(property); |
105 | 0 | return BeanUtilities.setter(this.bean.getClass(), property) != null; |
106 | |
} |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public boolean canGetSetProperty (String property) { |
115 | 0 | checkProperty(property); |
116 | 0 | return |
117 | |
BeanUtilities.getter(this.bean.getClass(), property) != null && |
118 | |
BeanUtilities.setter(this.bean.getClass(), property) != null; |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public void set (String property, Object value) { |
131 | 12 | checkProperty(property); |
132 | 11 | final Method method = BeanUtilities.setter(this.bean.getClass(), property); |
133 | 11 | if (method == null) { |
134 | 2 | throw new IllegalArgumentException( |
135 | |
"No getter for [" + property + |
136 | |
"] in bean class [" + this.bean.getClass() + "]." |
137 | |
); |
138 | |
} |
139 | |
try { |
140 | 9 | method.invoke(this.bean, new Object[] { value }); |
141 | 1 | } catch (IllegalArgumentException e) { |
142 | 1 | final String v = value == null ? "[null]" : " of type [" + value.getClass() + "]"; |
143 | 1 | final String message = |
144 | |
"Incompatible value " + v + " for property [" + property + |
145 | |
"] while calling [" + method + "] in bean class [" + this.bean.getClass() + "]."; |
146 | 1 | warn(message, e); |
147 | 1 | throw new IllegalArgumentException(message, e); |
148 | 0 | } catch (IllegalAccessException e) { |
149 | 0 | warn("Cannot access bean method [" + method + "].", e); |
150 | 0 | throw new IllegalStateException( |
151 | |
"Cannot access bean method [" + method + "].", e |
152 | |
); |
153 | 1 | } catch (InvocationTargetException e) { |
154 | 1 | warn("Error while executing bean method [" + method + "].", e.getCause()); |
155 | 1 | throw new IllegalStateException( |
156 | |
"Error while executing bean method [" + method + "].", |
157 | |
e.getCause() |
158 | |
); |
159 | 7 | } |
160 | 7 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
public Object get (String property) { |
171 | 9 | checkProperty(property); |
172 | 7 | final Method method = BeanUtilities.getter(this.bean.getClass(), property); |
173 | 7 | if (method == null) { |
174 | 2 | throw new IllegalArgumentException( |
175 | |
"No getter for [" + property + |
176 | |
"] in bean class [" + this.bean.getClass() + "]." |
177 | |
); |
178 | |
} |
179 | |
try { |
180 | 5 | return method.invoke(this.bean); |
181 | 0 | } catch (IllegalArgumentException e) { |
182 | 0 | warn("Unexpected exception while invoking method [" + method + "].", e); |
183 | 0 | throw new IllegalArgumentException("Internal error.", e); |
184 | 0 | } catch (IllegalAccessException e) { |
185 | 0 | warn("Cannot access bean method [" + method + "].", e); |
186 | 0 | throw new IllegalStateException( |
187 | |
"Cannot access bean method [" + method + "].", e |
188 | |
); |
189 | 1 | } catch (InvocationTargetException e) { |
190 | 1 | warn("Error while executing bean method [" + method + "].", e.getCause()); |
191 | 1 | throw new IllegalStateException( |
192 | |
"Error while executing bean method [" + method + "].", |
193 | |
e.getCause() |
194 | |
); |
195 | |
} |
196 | |
} |
197 | |
|
198 | |
} |