public static StackTraceElement getCaller ( int deep , boolean ignoreSameClass ) { // index 0 is Thread.getStackTrace // index 1 is ReflectUtil.getCallerClassName StackTraceElement [ ] stElements = Thread . currentThread ( ) . getStackTrace ( ) ; StackTraceElement currentStack = stElements [ 1 ] ; int found = deep + 1 ; for ( int i = 1 ; i < stElements . length ; i ++ ) { StackTraceElement nextStack = stElements [ i ] ; if ( nextStack . getClassName ( ) . equals ( ReflectUtil . class . getName ( ) ) ) { continue ; } if ( ! ignoreSameClass || ! currentStack . getClassName ( ) . equals ( nextStack . getClassName ( ) ) ) { currentStack = nextStack ; found -- ; } if ( found == 0 ) { return currentStack ; } } throw new IllegalArgumentException ( "Stack don't have such deep." ) ; }
Get caller stack info .