public static Set < Class > findMatchingSetters ( Class obClass , String propertyName ) { /*log.fine("private Set<Class> findMatchingSetters(Object ob, String propertyName): called");*/ Set < Class > types = new HashSet < Class > ( ) ; // Convert the first letter of the property name to upper case to match against the upper case version of // it that will be in the setter method name. For example the property test will have a setter method called // setTest. String upperPropertyName = Character . toUpperCase ( propertyName . charAt ( 0 ) ) + propertyName . substring ( 1 ) ; // Scan through all the objects methods. Method [ ] methods = obClass . getMethods ( ) ; for ( Method nextMethod : methods ) { // Get the next method. /*log.fine("nextMethod = " + nextMethod.getName());*/ // Check if a method has the correct name, accessibility and the correct number of arguments to be a setter // method for the property. String methodName = nextMethod . getName ( ) ; if ( methodName . equals ( "set" + upperPropertyName ) && Modifier . isPublic ( nextMethod . getModifiers ( ) ) && ( nextMethod . getParameterTypes ( ) . length == 1 ) ) { /*log.fine(methodName + " is a valid setter method for the property " + propertyName + " with argument of type " + nextMethod.getParameterTypes()[0]);*/ // Add its argument type to the array of setter types. types . add ( nextMethod . getParameterTypes ( ) [ 0 ] ) ; } } return types ; }
Finds the argument types of all setter methods on a bean for a given property name . For a method to be a setter method it must have a void return type be public and accept only a single argument . Its name must be set followed by the property name .