1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
package net.smartlab.web.bean; |
24 |
|
|
25 |
|
import java.beans.PropertyDescriptor; |
26 |
|
import java.lang.reflect.InvocationTargetException; |
27 |
|
import java.util.Iterator; |
28 |
|
import java.util.Locale; |
29 |
|
import java.util.Map; |
30 |
|
|
31 |
|
import org.apache.commons.beanutils.DynaBean; |
32 |
|
import org.apache.commons.beanutils.DynaClass; |
33 |
|
import org.apache.commons.beanutils.DynaProperty; |
34 |
|
import org.apache.commons.beanutils.PropertyUtils; |
35 |
|
import org.apache.commons.logging.Log; |
36 |
|
import org.apache.commons.logging.LogFactory; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
@author |
42 |
|
|
43 |
|
|
|
|
| 44,8% |
Uncovered Elements: 74 (134) |
Complexity: 40 |
Complexity Density: 0,48 |
|
44 |
|
public class Valorizer { |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
private static Log logger = LogFactory.getLog(Valorizer.class); |
49 |
|
|
50 |
|
private static ConverterManager converter = ConverterManager.getDefault(); |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
@param |
57 |
|
@param |
58 |
|
@param |
59 |
|
@param |
60 |
|
@throws |
61 |
|
@throws |
62 |
|
|
|
|
| 44,6% |
Uncovered Elements: 46 (83) |
Complexity: 23 |
Complexity Density: 0,4 |
|
63 |
52
|
protected static void set(Object bean, String name, Object value, Locale locale) throws ValorizationException,... |
64 |
|
ConversionException { |
65 |
52
|
if (logger.isTraceEnabled()) { |
66 |
0
|
logger.trace("setProperty(" + bean + ", " + name + ", " + value + ", " + locale.getDisplayName() + ")"); |
67 |
|
} |
68 |
52
|
try { |
69 |
|
|
70 |
52
|
Object target = bean; |
71 |
52
|
int delim = name.lastIndexOf(PropertyUtils.NESTED_DELIM); |
72 |
52
|
if (delim >= 0) { |
73 |
0
|
try { |
74 |
0
|
target = PropertyUtils.getProperty(bean, name.substring(0, delim)); |
75 |
|
} catch (NoSuchMethodException e) { |
76 |
|
|
77 |
|
return; |
78 |
|
} |
79 |
0
|
name = name.substring(delim + 1); |
80 |
0
|
if (logger.isTraceEnabled()) { |
81 |
0
|
logger.trace(" target bean = " + target); |
82 |
0
|
logger.trace(" target name = " + name); |
83 |
|
} |
84 |
|
} |
85 |
|
|
86 |
52
|
String property = null; |
87 |
|
|
88 |
52
|
Class type = null; |
89 |
|
|
90 |
52
|
int index = -1; |
91 |
|
|
92 |
52
|
String key = null; |
93 |
|
|
94 |
52
|
property = name; |
95 |
52
|
int i = property.indexOf(PropertyUtils.INDEXED_DELIM); |
96 |
52
|
if (i >= 0) { |
97 |
0
|
int k = property.indexOf(PropertyUtils.INDEXED_DELIM2); |
98 |
0
|
try { |
99 |
0
|
index = Integer.parseInt(property.substring(i + 1, k)); |
100 |
|
} catch (NumberFormatException nfe) { |
101 |
|
logger.debug(nfe); |
102 |
|
} |
103 |
0
|
property = property.substring(0, i); |
104 |
|
} |
105 |
52
|
int j = property.indexOf(PropertyUtils.MAPPED_DELIM); |
106 |
52
|
if (j >= 0) { |
107 |
0
|
int k = property.indexOf(PropertyUtils.MAPPED_DELIM2); |
108 |
0
|
try { |
109 |
0
|
key = property.substring(j + 1, k); |
110 |
|
} catch (IndexOutOfBoundsException ioobe) { |
111 |
|
logger.debug(ioobe); |
112 |
|
} |
113 |
0
|
property = property.substring(0, j); |
114 |
|
} |
115 |
|
|
116 |
52
|
if (target instanceof DynaBean) { |
117 |
0
|
DynaClass dynaClass = ((DynaBean)target).getDynaClass(); |
118 |
0
|
DynaProperty dynaProperty = dynaClass.getDynaProperty(property); |
119 |
0
|
if (dynaProperty == null) { |
120 |
|
|
121 |
0
|
return; |
122 |
|
} |
123 |
0
|
type = dynaProperty.getType(); |
124 |
|
} else { |
125 |
52
|
PropertyDescriptor descriptor = null; |
126 |
52
|
try { |
127 |
52
|
descriptor = PropertyUtils.getPropertyDescriptor(target, name); |
128 |
52
|
if (descriptor == null) { |
129 |
|
|
130 |
0
|
return; |
131 |
|
} |
132 |
|
} catch (NoSuchMethodException nsme) { |
133 |
|
|
134 |
|
return; |
135 |
|
} |
136 |
52
|
type = descriptor.getPropertyType(); |
137 |
52
|
if (type == null) { |
138 |
|
|
139 |
0
|
if (logger.isTraceEnabled()) { |
140 |
0
|
logger.trace(" target type for property '" + property + "' is null, so skipping this setter"); |
141 |
|
} |
142 |
0
|
return; |
143 |
|
} |
144 |
|
} |
145 |
52
|
if (logger.isTraceEnabled()) { |
146 |
0
|
logger.trace(" target property = " + property + ", type = " + type + ", index = " + index |
147 |
|
+ ", key = " + key); |
148 |
|
} |
149 |
|
|
150 |
52
|
if (index >= 0) { |
151 |
0
|
converter.convert(type, value, locale); |
152 |
0
|
try { |
153 |
0
|
PropertyUtils.setIndexedProperty(target, property, index, value); |
154 |
|
} catch (NoSuchMethodException e) { |
155 |
|
throw new InvocationTargetException(e, "Cannot set " + property); |
156 |
|
} |
157 |
52
|
} else if (key != null) { |
158 |
|
|
159 |
|
|
160 |
|
|
161 |
0
|
try { |
162 |
0
|
PropertyUtils.setMappedProperty(target, property, key, value); |
163 |
|
} catch (NoSuchMethodException e) { |
164 |
|
throw new InvocationTargetException(e, "Cannot set " + property); |
165 |
|
} |
166 |
|
} else { |
167 |
|
|
168 |
52
|
value = converter.convert(type, value, locale); |
169 |
52
|
try { |
170 |
52
|
PropertyUtils.setSimpleProperty(target, property, value); |
171 |
|
} catch (NoSuchMethodException e) { |
172 |
|
throw new InvocationTargetException(e, "Cannot set " + property); |
173 |
|
} |
174 |
|
} |
175 |
|
} catch (IllegalAccessException iae) { |
176 |
|
throw new ValorizationException(iae); |
177 |
|
} catch (InvocationTargetException ite) { |
178 |
|
throw new ValorizationException(ite.getTargetException()); |
179 |
|
} |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
@param |
186 |
|
@param |
187 |
|
@param |
188 |
|
@throws |
189 |
|
@throws |
190 |
|
|
|
|
| 42,9% |
Uncovered Elements: 28 (49) |
Complexity: 17 |
Complexity Density: 0,63 |
|
191 |
2
|
public static void copy(Object src, Object dst, Locale locale) throws ConversionException, ValorizationException {... |
192 |
2
|
if (dst == null) { |
193 |
0
|
throw new IllegalArgumentException("No destination specified"); |
194 |
|
} |
195 |
2
|
if (src == null) { |
196 |
0
|
throw new IllegalArgumentException("No origin specified"); |
197 |
|
} |
198 |
2
|
if (logger.isDebugEnabled()) { |
199 |
0
|
logger.debug("populate(" + dst + ", " + src + ")"); |
200 |
|
} |
201 |
2
|
if (src instanceof DynaBean) { |
202 |
0
|
DynaProperty descriptors[] = ((DynaBean)src).getDynaClass().getDynaProperties(); |
203 |
0
|
for (int i = 0; i < descriptors.length; i++) { |
204 |
0
|
String name = descriptors[i].getName(); |
205 |
0
|
if (PropertyUtils.isWriteable(dst, name)) { |
206 |
0
|
Object value = ((DynaBean)src).get(name); |
207 |
0
|
Valorizer.set(dst, name, value, locale); |
208 |
|
} |
209 |
|
} |
210 |
2
|
} else if (src instanceof Map) { |
211 |
0
|
Iterator names = ((Map)src).keySet().iterator(); |
212 |
0
|
while (names.hasNext()) { |
213 |
0
|
String name = (String)names.next(); |
214 |
0
|
if (PropertyUtils.isWriteable(dst, name)) { |
215 |
0
|
Object value = ((Map)src).get(name); |
216 |
0
|
Valorizer.set(dst, name, value, locale); |
217 |
|
} |
218 |
|
} |
219 |
|
} else { |
220 |
2
|
PropertyDescriptor descriptors[] = PropertyUtils.getPropertyDescriptors(src); |
221 |
59
|
for (int i = 0; i < descriptors.length; i++) { |
222 |
57
|
String name = descriptors[i].getName(); |
223 |
57
|
if (!"class".equals(name) && PropertyUtils.isReadable(src, name) |
224 |
|
&& PropertyUtils.isWriteable(dst, name)) { |
225 |
52
|
try { |
226 |
52
|
Object value = PropertyUtils.getSimpleProperty(src, name); |
227 |
52
|
Valorizer.set(dst, name, value, locale); |
228 |
|
} catch (NoSuchMethodException nsme) { |
229 |
|
logger.warn(nsme); |
230 |
|
} catch (IllegalAccessException iae) { |
231 |
|
logger.warn(iae); |
232 |
|
} catch (InvocationTargetException ite) { |
233 |
|
logger.warn(ite); |
234 |
|
} |
235 |
|
} |
236 |
|
} |
237 |
|
} |
238 |
|
} |
239 |
|
} |