Этот метод делает то же самое, но немного более просто и, возможно, немного более результативно, и в случае, если вы используете отражение, он автоматически пропускает эти кадры. Единственная проблема заключается в том, что он может отсутствовать в JVM, отличном от Sun, хотя он включен в классы времени выполнения JRockit 1.4 -> 1.6. (Точка не является классом public ).
sun.reflect.Reflection
/** Returns the class of the method <code>realFramesToSkip</code>
frames up the stack (zero-based), ignoring frames associated
with java.lang.reflect.Method.invoke() and its implementation.
The first frame is that associated with this method, so
<code>getCallerClass(0)</code> returns the Class object for
sun.reflect.Reflection. Frames associated with
java.lang.reflect.Method.invoke() and its implementation are
completely ignored and do not count toward the number of "real"
frames skipped. */
public static native Class getCallerClass(int realFramesToSkip);
Насколько известно значение realFramesToSkip
, версии Sun 1.5 и 1.6 VM java.lang.System
, существует метод защиты пакетов, называемый getCallerClass (), который вызывает sun.reflect.Reflection.getCallerClass(3)
, но в моем классе вспомогательной утилиты, который я использовал 4, поскольку есть добавленный фрейм вызова вспомогательного класса.