public static Object getProperty ( Object obj , String name ) { if ( obj == null ) throw new NullPointerException ( "Object cannot be null" ) ; if ( name == null ) throw new NullPointerException ( "Property name cannot be null" ) ; Class < ? > objClass = obj . getClass ( ) ; // Search in fields for ( Field field : objClass . getFields ( ) ) { try { if ( matchField ( field , name ) ) return field . get ( obj ) ; } catch ( Throwable t ) { // Ignore exceptions } } // Search in properties name = "get" + name ; for ( Method method : objClass . getMethods ( ) ) { try { if ( matchPropertyGetter ( method , name ) ) return method . invoke ( obj ) ; } catch ( Throwable t ) { // Ignore exceptions } } return null ; }
Gets value of object property specified by its name .