|
22 | 22 | import java.lang.reflect.Method;
|
23 | 23 | import java.lang.reflect.Modifier;
|
24 | 24 | import java.util.Arrays;
|
| 25 | +import java.util.Collections; |
25 | 26 | import java.util.Comparator;
|
| 27 | +import java.util.HashSet; |
26 | 28 | import java.util.Map;
|
| 29 | +import java.util.Set; |
27 | 30 | import java.util.concurrent.ConcurrentHashMap;
|
28 | 31 |
|
29 | 32 | import org.springframework.core.MethodParameter;
|
|
50 | 53 | */
|
51 | 54 | public class ReflectivePropertyAccessor implements PropertyAccessor {
|
52 | 55 |
|
| 56 | + private static final Set<Class<?>> BOOLEAN_TYPES; |
| 57 | + static { |
| 58 | + Set<Class<?>> booleanTypes = new HashSet<Class<?>>(); |
| 59 | + booleanTypes.add(Boolean.class); |
| 60 | + booleanTypes.add(Boolean.TYPE); |
| 61 | + BOOLEAN_TYPES = Collections.unmodifiableSet(booleanTypes); |
| 62 | + } |
| 63 | + |
| 64 | + private static final Set<Class<?>> ANY_TYPES = Collections.emptySet(); |
| 65 | + |
| 66 | + |
53 | 67 | private final Map<CacheKey, InvokerPair> readerCache = new ConcurrentHashMap<CacheKey, InvokerPair>(64);
|
54 | 68 |
|
55 | 69 | private final Map<CacheKey, Member> writerCache = new ConcurrentHashMap<CacheKey, Member>(64);
|
@@ -319,29 +333,33 @@ private Field findField(String name, Class<?> clazz, Object target) {
|
319 | 333 | * Find a getter method for the specified property.
|
320 | 334 | */
|
321 | 335 | protected Method findGetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) {
|
322 |
| - return findMethodForProperty(getPropertyMethodSuffixes(propertyName), |
323 |
| - new String[] { "get", "is" }, clazz, mustBeStatic, 0); |
| 336 | + Method method = findMethodForProperty(getPropertyMethodSuffixes(propertyName), |
| 337 | + "get", clazz, mustBeStatic, 0, ANY_TYPES); |
| 338 | + if (method == null) { |
| 339 | + method = findMethodForProperty(getPropertyMethodSuffixes(propertyName), |
| 340 | + "is", clazz, mustBeStatic, 0, BOOLEAN_TYPES); |
| 341 | + } |
| 342 | + return method; |
324 | 343 | }
|
325 | 344 |
|
326 | 345 | /**
|
327 | 346 | * Find a setter method for the specified property.
|
328 | 347 | */
|
329 | 348 | protected Method findSetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) {
|
330 | 349 | return findMethodForProperty(getPropertyMethodSuffixes(propertyName),
|
331 |
| - new String[] { "set" }, clazz, mustBeStatic, 1); |
| 350 | + "set", clazz, mustBeStatic, 1, ANY_TYPES); |
332 | 351 | }
|
333 | 352 |
|
334 |
| - private Method findMethodForProperty(String[] methodSuffixes, String[] prefixes, Class<?> clazz, |
335 |
| - boolean mustBeStatic, int numberOfParams) { |
| 353 | + private Method findMethodForProperty(String[] methodSuffixes, String prefix, Class<?> clazz, |
| 354 | + boolean mustBeStatic, int numberOfParams, Set<Class<?>> requiredReturnTypes) { |
336 | 355 | Method[] methods = getSortedClassMethods(clazz);
|
337 | 356 | for (String methodSuffix : methodSuffixes) {
|
338 |
| - for (String prefix : prefixes) { |
339 |
| - for (Method method : methods) { |
340 |
| - if (method.getName().equals(prefix + methodSuffix) |
341 |
| - && method.getParameterTypes().length == numberOfParams |
342 |
| - && (!mustBeStatic || Modifier.isStatic(method.getModifiers()))) { |
343 |
| - return method; |
344 |
| - } |
| 357 | + for (Method method : methods) { |
| 358 | + if (method.getName().equals(prefix + methodSuffix) |
| 359 | + && method.getParameterTypes().length == numberOfParams |
| 360 | + && (!mustBeStatic || Modifier.isStatic(method.getModifiers())) |
| 361 | + && (requiredReturnTypes.isEmpty() || requiredReturnTypes.contains(method.getReturnType()))) { |
| 362 | + return method; |
345 | 363 | }
|
346 | 364 | }
|
347 | 365 | }
|
|
0 commit comments