@@ -43,12 +43,44 @@ private CallingClassUtils() {
4343 }
4444
4545 /**
46+ * Inspects the stack trace to return the name of the class that calls
47+ * this method, but ignores every class annotated with @IgnoreAsCallingClass.
48+ * <p>
49+ * If every class on the stack trace is annotated, then the class at the
50+ * root of the stack trace is returned.
51+ */
52+ public static String getCallingClassName () {
53+ StackTraceElement [] stackTrace = Thread .currentThread ().getStackTrace ();
54+ for (int i = 1 ; i < stackTrace .length - 2 ; i ++) {
55+ String className = stackTrace [i ].getClassName ();
56+ if (!hasIgnoreAsCallingClassAnnotation (className )) return className ;
57+ }
58+ return stackTrace [stackTrace .length - 1 ].getClassName ();
59+ }
60+
61+ private static boolean hasIgnoreAsCallingClassAnnotation (String className ) {
62+ try {
63+ Class < ? > clazz = Class .forName (className );
64+ return clazz .isAnnotationPresent (IgnoreAsCallingClass .class );
65+ }
66+ catch (ClassNotFoundException ignore ) {
67+ return false ;
68+ }
69+ }
70+
71+ /**
72+ * @deprecated Use {@link #getCallingClassName()} instead.
73+ *
74+ * Warning: This method throws a IllegalStateException as soon as it comes
75+ * across a class that can't be loaded with the default class loader.
76+ *
4677 * Inspects the stack trace to return the class that calls this method, but
4778 * ignores every class annotated with @IgnoreAsCallingClass.
4879 *
4980 * @throws IllegalStateException if every method on the stack, is in a class
5081 * annotated with @IgnoreAsCallingClass.
5182 */
83+ @ Deprecated
5284 public static Class <?> getCallingClass () {
5385 try {
5486 StackTraceElement [] stackTrace = Thread .currentThread ().getStackTrace ();
0 commit comments