public Object getFieldValue ( String fieldName ) throws ReflectorException { if ( object == null ) { logger . error ( "object is null: did you specify it using the 'inspect()' method?" ) ; throw new ReflectorException ( "object is null: did you specify it using the 'inspect()' method?" ) ; } if ( ! Strings . isValid ( fieldName ) ) { logger . error ( "field name is null or not valid" ) ; throw new ReflectorException ( "field name is null or not valid" ) ; } Object result = null ; String name = fieldName . trim ( ) ; if ( useGetters ) { logger . trace ( "accessing value using getter" ) ; String methodName = "get" + Character . toUpperCase ( name . charAt ( 0 ) ) + name . substring ( 1 ) ; result = invoke ( methodName ) ; } else { logger . trace ( "accessing value through direct field access" ) ; Field field = null ; boolean needReprotect = false ; try { field = object . getClass ( ) . getDeclaredField ( name ) ; if ( accessPrivateMembers ) { needReprotect = unprotect ( field ) ; } result = field . get ( object ) ; } catch ( Exception e ) { String message = "error accessing field '" + fieldName + "'" ; logger . error ( message , e ) ; throw new ReflectorException ( message , e ) ; } finally { if ( needReprotect ) { protect ( field ) ; } } } return result ; }
Retrieves the value of a field .