private Method getPropertyFunction ( Class targetClass , String prefix , Class valueType ) { // TODO: faster implementation... Method returnVal = null ; String methodName = getMethodName ( prefix , mPropertyName ) ; Class args [ ] = null ; if ( valueType == null ) { try { returnVal = targetClass . getMethod ( methodName , args ) ; } catch ( NoSuchMethodException e ) { /* The native implementation uses JNI to do reflection, which allows access to private methods. * getDeclaredMethod(..) does not find superclass methods, so it's implemented as a fallback. */ try { returnVal = targetClass . getDeclaredMethod ( methodName , args ) ; returnVal . setAccessible ( true ) ; } catch ( NoSuchMethodException e2 ) { Log . e ( "PropertyValuesHolder" , "Couldn't find no-arg method for property " + mPropertyName + ": " + e ) ; } } } else { args = new Class [ 1 ] ; Class typeVariants [ ] ; if ( mValueType . equals ( Float . class ) ) { typeVariants = FLOAT_VARIANTS ; } else if ( mValueType . equals ( Integer . class ) ) { typeVariants = INTEGER_VARIANTS ; } else if ( mValueType . equals ( Double . class ) ) { typeVariants = DOUBLE_VARIANTS ; } else { typeVariants = new Class [ 1 ] ; typeVariants [ 0 ] = mValueType ; } for ( Class typeVariant : typeVariants ) { args [ 0 ] = typeVariant ; try { returnVal = targetClass . getMethod ( methodName , args ) ; // change the value type to suit mValueType = typeVariant ; return returnVal ; } catch ( NoSuchMethodException e ) { /* The native implementation uses JNI to do reflection, which allows access to private methods. * getDeclaredMethod(..) does not find superclass methods, so it's implemented as a fallback. */ try { returnVal = targetClass . getDeclaredMethod ( methodName , args ) ; returnVal . setAccessible ( true ) ; // change the value type to suit mValueType = typeVariant ; return returnVal ; } catch ( NoSuchMethodException e2 ) { // Swallow the error and keep trying other variants } } } // If we got here, then no appropriate function was found Log . e ( "PropertyValuesHolder" , "Couldn't find setter/getter for property " + mPropertyName + " with value type " + mValueType ) ; } return returnVal ; }
Determine the setter or getter function using the JavaBeans convention of setFoo or getFoo for a property named foo . This function figures out what the name of the function should be and uses reflection to find the Method with that name on the target object .