private static final boolean printFieldStackTrace ( PrintWriter p , Throwable t , String className , String fieldName ) { for ( Class < ? > c = t . getClass ( ) ; c != Object . class ; c = c . getSuperclass ( ) ) { if ( c . getName ( ) . equals ( className ) ) { try { Object value = c . getField ( fieldName ) . get ( t ) ; if ( value instanceof Throwable && value != t . getCause ( ) ) { p . append ( fieldName ) . append ( ": " ) ; printStackTrace ( p , ( Throwable ) value ) ; } return true ; } catch ( NoSuchFieldException e ) { } catch ( IllegalAccessException e ) { } } } return false ; }
Find a field value in the class hierarchy of an exception and if the field contains another Throwable then print its stack trace .