19
19
import java .beans .IntrospectionException ;
20
20
import java .beans .PropertyDescriptor ;
21
21
import java .lang .reflect .Method ;
22
+ import java .util .ArrayList ;
22
23
import java .util .Collection ;
23
24
import java .util .Enumeration ;
25
+ import java .util .List ;
24
26
import java .util .Map ;
25
27
import java .util .TreeMap ;
26
28
@@ -52,8 +54,10 @@ abstract class PropertyDescriptorUtils {
52
54
* @see SimpleBeanInfoFactory
53
55
* @see java.beans.Introspector#getBeanInfo(Class)
54
56
*/
55
- public static Collection <PropertyDescriptor > determineBasicProperties (Class <?> beanClass ) throws IntrospectionException {
56
- Map <String , PropertyDescriptor > pdMap = new TreeMap <>();
57
+ public static Collection <? extends PropertyDescriptor > determineBasicProperties (Class <?> beanClass )
58
+ throws IntrospectionException {
59
+
60
+ Map <String , BasicPropertyDescriptor > pdMap = new TreeMap <>();
57
61
58
62
for (Method method : beanClass .getMethods ()) {
59
63
String methodName = method .getName ();
@@ -81,39 +85,26 @@ else if (methodName.startsWith("is") && method.getParameterCount() == 0 && metho
81
85
continue ;
82
86
}
83
87
84
- PropertyDescriptor pd = pdMap .get (propertyName );
88
+ BasicPropertyDescriptor pd = pdMap .get (propertyName );
85
89
if (pd != null ) {
86
90
if (setter ) {
87
91
if (pd .getWriteMethod () == null ||
88
92
pd .getWriteMethod ().getParameterTypes ()[0 ].isAssignableFrom (method .getParameterTypes ()[0 ])) {
89
- try {
90
- pd .setWriteMethod (method );
91
- }
92
- catch (IntrospectionException ex ) {
93
- // typically a type mismatch -> ignore
94
- }
93
+ pd .setWriteMethod (method );
94
+ }
95
+ else {
96
+ pd .addWriteMethod (method );
95
97
}
96
98
}
97
99
else {
98
100
if (pd .getReadMethod () == null ||
99
101
(pd .getReadMethod ().getReturnType () == method .getReturnType () && method .getName ().startsWith ("is" ))) {
100
- try {
101
- pd .setReadMethod (method );
102
- }
103
- catch (IntrospectionException ex ) {
104
- // typically a type mismatch -> ignore
105
- }
102
+ pd .setReadMethod (method );
106
103
}
107
104
}
108
105
}
109
106
else {
110
- pd = new BasicPropertyDescriptor (propertyName , beanClass );
111
- if (setter ) {
112
- pd .setWriteMethod (method );
113
- }
114
- else {
115
- pd .setReadMethod (method );
116
- }
107
+ pd = new BasicPropertyDescriptor (propertyName , (!setter ? method : null ), (setter ? method : null ));
117
108
pdMap .put (propertyName , pd );
118
109
}
119
110
}
@@ -277,8 +268,12 @@ private static class BasicPropertyDescriptor extends PropertyDescriptor {
277
268
@ Nullable
278
269
private Method writeMethod ;
279
270
280
- public BasicPropertyDescriptor (String propertyName , Class <?> beanClass ) throws IntrospectionException {
281
- super (propertyName , beanClass , null , null );
271
+ private final List <Method > alternativeWriteMethods = new ArrayList <>();
272
+
273
+ public BasicPropertyDescriptor (String propertyName , @ Nullable Method readMethod , @ Nullable Method writeMethod )
274
+ throws IntrospectionException {
275
+
276
+ super (propertyName , readMethod , writeMethod );
282
277
}
283
278
284
279
@ Override
@@ -297,11 +292,33 @@ public void setWriteMethod(@Nullable Method writeMethod) {
297
292
this .writeMethod = writeMethod ;
298
293
}
299
294
295
+ public void addWriteMethod (Method writeMethod ) {
296
+ if (this .writeMethod != null ) {
297
+ this .alternativeWriteMethods .add (this .writeMethod );
298
+ this .writeMethod = null ;
299
+ }
300
+ this .alternativeWriteMethods .add (writeMethod );
301
+ }
302
+
300
303
@ Override
301
304
@ Nullable
302
305
public Method getWriteMethod () {
306
+ if (this .writeMethod == null && !this .alternativeWriteMethods .isEmpty ()) {
307
+ if (this .readMethod == null ) {
308
+ return this .alternativeWriteMethods .get (0 );
309
+ }
310
+ else {
311
+ for (Method method : this .alternativeWriteMethods ) {
312
+ if (this .readMethod .getReturnType ().isAssignableFrom (method .getParameterTypes ()[0 ])) {
313
+ this .writeMethod = method ;
314
+ break ;
315
+ }
316
+ }
317
+ }
318
+ }
303
319
return this .writeMethod ;
304
320
}
305
321
}
306
322
323
+
307
324
}
0 commit comments