@@ -318,42 +318,34 @@ private Field findField(String name, Class<?> clazz, Object target) {
318
318
* Find a getter method for the specified property.
319
319
*/
320
320
protected Method findGetterForProperty (String propertyName , Class <?> clazz , boolean mustBeStatic ) {
321
- Method [] ms = getSortedClassMethods (clazz );
322
- String propertyMethodSuffix = getPropertyMethodSuffix (propertyName );
323
-
324
- // Try "get*" method...
325
- String getterName = "get" + propertyMethodSuffix ;
326
- for (Method method : ms ) {
327
- if (method .getName ().equals (getterName ) && method .getParameterTypes ().length == 0 &&
328
- (!mustBeStatic || Modifier .isStatic (method .getModifiers ()))) {
329
- return method ;
330
- }
331
- }
332
- // Try "is*" method...
333
- getterName = "is" + propertyMethodSuffix ;
334
- for (Method method : ms ) {
335
- if (method .getName ().equals (getterName ) && method .getParameterTypes ().length == 0 &&
336
- (boolean .class .equals (method .getReturnType ()) || Boolean .class .equals (method .getReturnType ())) &&
337
- (!mustBeStatic || Modifier .isStatic (method .getModifiers ()))) {
338
- return method ;
339
- }
340
- }
341
- return null ;
321
+ return findMethodForProperty (getPropertyMethodSuffixes (propertyName ),
322
+ new String [] { "get" , "is" }, clazz , mustBeStatic , 0 );
342
323
}
343
324
344
325
/**
345
326
* Find a setter method for the specified property.
346
327
*/
347
328
protected Method findSetterForProperty (String propertyName , Class <?> clazz , boolean mustBeStatic ) {
329
+ return findMethodForProperty (getPropertyMethodSuffixes (propertyName ),
330
+ new String [] { "set" }, clazz , mustBeStatic , 1 );
331
+ }
332
+
333
+ private Method findMethodForProperty (String [] methodSuffixes , String [] prefixes , Class <?> clazz ,
334
+ boolean mustBeStatic , int numberOfParams ) {
348
335
Method [] methods = getSortedClassMethods (clazz );
349
- String setterName = "set" + getPropertyMethodSuffix (propertyName );
350
- for (Method method : methods ) {
351
- if (method .getName ().equals (setterName ) && method .getParameterTypes ().length == 1 &&
352
- (!mustBeStatic || Modifier .isStatic (method .getModifiers ()))) {
353
- return method ;
336
+ for (String methodSuffix : methodSuffixes ) {
337
+ for (String prefix : prefixes ) {
338
+ for (Method method : methods ) {
339
+ if (method .getName ().equals (prefix + methodSuffix )
340
+ && method .getParameterTypes ().length == numberOfParams
341
+ && (!mustBeStatic || Modifier .isStatic (method .getModifiers ()))) {
342
+ return method ;
343
+ }
344
+ }
354
345
}
355
346
}
356
347
return null ;
348
+
357
349
}
358
350
359
351
/**
@@ -370,13 +362,29 @@ public int compare(Method o1, Method o2) {
370
362
return methods ;
371
363
}
372
364
365
+ /**
366
+ * Return the method suffixes for a given property name. The default implementation
367
+ * uses JavaBean conventions with additional support for properties of the form 'xY'
368
+ * where the method 'getXY()' is used in preference to the JavaBean convention of
369
+ * 'getxY()'.
370
+ */
371
+ protected String [] getPropertyMethodSuffixes (String propertyName ) {
372
+ String suffix = getPropertyMethodSuffix (propertyName );
373
+ if (suffix .length () > 0 && Character .isUpperCase (suffix .charAt (0 ))) {
374
+ return new String [] { suffix };
375
+ }
376
+ return new String [] { suffix , StringUtils .capitalize (suffix ) };
377
+ }
378
+
379
+ /**
380
+ * Return the method suffix for a given property name. The default implementation
381
+ * uses JavaBean conventions.
382
+ */
373
383
protected String getPropertyMethodSuffix (String propertyName ) {
374
384
if (propertyName .length () > 1 && Character .isUpperCase (propertyName .charAt (1 ))) {
375
385
return propertyName ;
376
386
}
377
- else {
378
- return StringUtils .capitalize (propertyName );
379
- }
387
+ return StringUtils .capitalize (propertyName );
380
388
}
381
389
382
390
/**
0 commit comments