protected final Object getProperty ( final Object obj , final String property ) { if ( ( obj == null ) || ( property == null ) || ( property . trim ( ) . length ( ) == 0 ) ) { return null ; } final String [ ] getterNames = createGetterNames ( property ) ; for ( int i = 0 ; i < getterNames . length ; i ++ ) { final String getter = getterNames [ i ] ; try { final Class cl = obj . getClass ( ) ; final Method m = cl . getMethod ( getter , new Class [ ] { } ) ; return m . invoke ( obj , new Object [ ] { } ) ; } catch ( final IllegalAccessException e ) { throw new RuntimeException ( "Accessing " + getter + " method of property '" + property + "' failed (private? protected?)! [" + obj . getClass ( ) + "]" , e ) ; } catch ( final InvocationTargetException e ) { throw new RuntimeException ( "Exception within " + getter + " method of property '" + property + "'! [" + obj . getClass ( ) + "]" , e . getCause ( ) ) ; } catch ( final NoSuchMethodException e ) { if ( i == getter . length ( ) - 1 ) { throw new RuntimeException ( "No " + getter + " method found for property! '" + property + "'! [" + obj . getClass ( ) + "]" , e ) ; } } } throw new IllegalStateException ( "No getters defined in 'createGetterNames()'!" ) ; }
Return the value of a property via reflection .