private static boolean isOnStack ( int moreThan , String canonicalName ) { StackTraceElement [ ] stackTrace = Thread . currentThread ( ) . getStackTrace ( ) ; int count = 0 ; for ( StackTraceElement element : stackTrace ) { if ( element . getClassName ( ) . startsWith ( canonicalName ) ) { count ++ ; } } return count > moreThan ; }
Checks if the given name is on stack more than the given number of times . This method uses startsWith to check if the given name is on stack so one can pass a package name too .