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