public static Method getSingleSetter ( String fieldName , Class < ? > clazz ) { String setterName = buildSetterName ( fieldName ) ; try { // Using getMathods(), getMathod(...) expects exact parameter type // matching and ignores inheritance-tree. Method [ ] methods = clazz . getMethods ( ) ; List < Method > candidates = new ArrayList < Method > ( ) ; Set < Class < ? > > parameterTypes = new HashSet < Class < ? > > ( ) ; for ( Method method : methods ) { if ( method . getName ( ) . equals ( setterName ) ) { Class < ? > [ ] paramTypes = method . getParameterTypes ( ) ; if ( paramTypes != null && paramTypes . length == 1 ) { candidates . add ( method ) ; parameterTypes . add ( paramTypes [ 0 ] ) ; } } } if ( parameterTypes . size ( ) > 1 ) { throw LOG . ambiguousSetterMethod ( setterName , clazz . getName ( ) ) ; } if ( candidates . size ( ) >= 1 ) { return candidates . get ( 0 ) ; } return null ; } catch ( SecurityException e ) { throw LOG . unableToAccessMethod ( setterName , clazz . getName ( ) ) ; } }
Returns a setter method based on the fieldName and the java beans setter naming convention or null if none exists . If multiple setters with different parameter types are present an exception is thrown . If they have the same parameter type one of those methods is returned .