|
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);
|
@@ -314,29 +328,33 @@ private Field findField(String name, Class<?> clazz, Object target) {
|
314 | 328 | * Find a getter method for the specified property.
|
315 | 329 | */
|
316 | 330 | protected Method findGetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) {
|
317 |
| - return findMethodForProperty(getPropertyMethodSuffixes(propertyName), |
318 |
| - new String[] { "get", "is" }, clazz, mustBeStatic, 0); |
| 331 | + Method method = findMethodForProperty(getPropertyMethodSuffixes(propertyName), |
| 332 | + "get", clazz, mustBeStatic, 0, ANY_TYPES); |
| 333 | + if (method == null) { |
| 334 | + method = findMethodForProperty(getPropertyMethodSuffixes(propertyName), |
| 335 | + "is", clazz, mustBeStatic, 0, BOOLEAN_TYPES); |
| 336 | + } |
| 337 | + return method; |
319 | 338 | }
|
320 | 339 |
|
321 | 340 | /**
|
322 | 341 | * Find a setter method for the specified property.
|
323 | 342 | */
|
324 | 343 | protected Method findSetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) {
|
325 | 344 | return findMethodForProperty(getPropertyMethodSuffixes(propertyName),
|
326 |
| - new String[] { "set" }, clazz, mustBeStatic, 1); |
| 345 | + "set", clazz, mustBeStatic, 1, ANY_TYPES); |
327 | 346 | }
|
328 | 347 |
|
329 |
| - private Method findMethodForProperty(String[] methodSuffixes, String[] prefixes, Class<?> clazz, |
330 |
| - boolean mustBeStatic, int numberOfParams) { |
| 348 | + private Method findMethodForProperty(String[] methodSuffixes, String prefix, Class<?> clazz, |
| 349 | + boolean mustBeStatic, int numberOfParams, Set<Class<?>> requiredReturnTypes) { |
331 | 350 | Method[] methods = getSortedClassMethods(clazz);
|
332 | 351 | for (String methodSuffix : methodSuffixes) {
|
333 |
| - for (String prefix : prefixes) { |
334 |
| - for (Method method : methods) { |
335 |
| - if (method.getName().equals(prefix + methodSuffix) |
336 |
| - && method.getParameterTypes().length == numberOfParams |
337 |
| - && (!mustBeStatic || Modifier.isStatic(method.getModifiers()))) { |
338 |
| - return method; |
339 |
| - } |
| 352 | + for (Method method : methods) { |
| 353 | + if (method.getName().equals(prefix + methodSuffix) |
| 354 | + && method.getParameterTypes().length == numberOfParams |
| 355 | + && (!mustBeStatic || Modifier.isStatic(method.getModifiers())) |
| 356 | + && (requiredReturnTypes.isEmpty() || requiredReturnTypes.contains(method.getReturnType()))) { |
| 357 | + return method; |
340 | 358 | }
|
341 | 359 | }
|
342 | 360 | }
|
|
0 commit comments