private Object extractValue ( Object aValue , String aName , String bName ) { Class aClass = aValue . getClass ( ) ; Object value ; // // a.isB() or a.getB() // String bNameCapped = Character . toUpperCase ( bName . charAt ( 0 ) ) + bName . substring ( 1 ) ; Method getMethod = null ; Class retType ; // // try a.isB() first, if found verify that a.isB() returns a boolean value, // and that there is not also a a.getB() method - if there is except // try { getMethod = aClass . getMethod ( "is" + bNameCapped , ( Class [ ] ) null ) ; retType = getMethod . getReturnType ( ) ; if ( ! ( retType . equals ( Boolean . class ) || retType . equals ( Boolean . TYPE ) ) ) { // only boolean returns can be isB() getMethod = null ; } else { /** * make sure ("get" + bNameCapped) does not exist as well * see CR216159 */ boolean getMethodFound = true ; try { aClass . getMethod ( "get" + bNameCapped , ( Class [ ] ) null ) ; } catch ( NoSuchMethodException e ) { getMethodFound = false ; } if ( getMethodFound ) { throw new ControlException ( "Colliding field accsessors in user defined class '" + aClass . getName ( ) + "' for field '" + bName + "'. Please use is<FieldName> for boolean fields and get<FieldName> name for other datatypes." ) ; } } } catch ( NoSuchMethodException e ) { // ignore } // // try a.getB() if a.isB() was not found. // if ( getMethod == null ) { try { getMethod = aClass . getMethod ( "get" + bNameCapped , ( Class [ ] ) null ) ; } catch ( NoSuchMethodException e ) { // ignore } } if ( getMethod != null ) { // OK- a.getB() try { value = getMethod . invoke ( aValue , ( Object [ ] ) null ) ; } catch ( IllegalAccessException e ) { throw new ControlException ( "Unable to access public method: " + e . toString ( ) ) ; } catch ( java . lang . reflect . InvocationTargetException e ) { throw new ControlException ( "Exception thrown when executing : " + getMethod . getName ( ) + "() to use as parameter" ) ; } return value ; } // // try a.b // try { value = aClass . getField ( bName ) . get ( aValue ) ; return value ; } catch ( NoSuchFieldException e ) { // ignore } catch ( IllegalAccessException e ) { // ignore } // // try a.get(b) // if ( aValue instanceof Map ) { try { value = TypeMappingsFactory . getInstance ( ) . lookupType ( aValue , new Object [ ] { bName } ) ; return value ; } catch ( Exception mapex ) { throw new ControlException ( "Exception thrown when executing Map.get() to resolve parameter" + mapex . toString ( ) ) ; } } // no other options... throw new ControlException ( "Illegal argument in SQL statement: " + _parameterName + "; unable to find suitable method of retrieving property " + bName + " out of object " + aName + "." ) ; }
Get the value from the referenced method parameter using java reflection