Provides methods to get the calling class and method.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String
Constant for the system property 'unsafe.calls.main'.
If this property istrue
, the methods in this class can be called from themain
method.private static final String
Constant for the system property 'unsafe.offset.base'.
The base offset for the stack trace elements is3
.
Used internally ingetStackTrace(int)
to get the correct stack trace element.private static final String
Constant for the system property 'unsafe.offset.default'. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Private constructor to prevent instantiation.
This is a static helper class. -
Method Summary
Modifier and TypeMethodDescriptionstatic Class
<?> Gets the calling class from the stack trace with an offset of:static Class
<?> getCallingClass
(int callsBefore) Gets the calling class from the stack trace with an offset of:static @NotNull Method
Gets the calling method from the stack trace with an offset of:static @NotNull Method
getCallingMethod
(int callsBefore) Gets the calling method from the stack trace with an offset of:private static @Nullable Method
getCallingMethod
(@NotNull StackTraceElement element) Gets the calling method from the specified stack trace element.
The calling method will be identified by the class name and the method name.
If there are multiple methods with the same name in the same class, the method will not be identified.Gets the calling method safe from the stack trace with an offset of:getCallingMethodSafe
(int callsBefore) Gets the calling method safe from the stack trace with an offset of:private static int
Gets the default offset for the stack trace elements.private static @NotNull StackTraceElement
getStackTrace
(int callsBefore) Gets the stack trace element at the specified position.
This method is used internally to get the correct stack trace element.
-
Field Details
-
UNSAFE_CALLS_MAIN
Constant for the system property 'unsafe.calls.main'.
If this property istrue
, the methods in this class can be called from themain
method.- See Also:
-
UNSAFE_OFFSET_BASE
Constant for the system property 'unsafe.offset.base'.
The base offset for the stack trace elements is3
.
Used internally ingetStackTrace(int)
to get the correct stack trace element.- See Also:
-
UNSAFE_OFFSET_DEFAULT
Constant for the system property 'unsafe.offset.default'.
The default offset for the stack trace elements is
1
.
The value must be greater than or equal to1
.
The value will be added to the base offset to get the correct stack trace element.
- See Also:
-
-
Constructor Details
-
StackTraceUtils
private StackTraceUtils()Private constructor to prevent instantiation.
This is a static helper class.
-
-
Method Details
-
getCallingClass
Gets the calling class from the stack trace with an offset of:
base + default
The base offset will be get from the system property 'unsafe.offset.base'.
If the property is not set, the default value3
will be used.
The default offset will be get from the system property 'unsafe.offset.default'.
If the property is not set, the default value1
will be used.
- ...
- 2: This method
- 3: The method which requests the calling class
- 4: The calling class
- Returns:
- The calling class
- Throws:
IndexOutOfBoundsException
- If the specified position is out of boundsIllegalCallerException
- If called from themain
method and 'unsafe.calls.main' isfalse
- See Also:
-
getCallingClass
Gets the calling class from the stack trace with an offset of:
base + default + callsBefore
The base offset will be get from the system property 'unsafe.offset.base'.
If the property is not set, the default value3
will be used.
The default offset will be get from the system property 'unsafe.offset.default'.
If the property is not set, the default value1
will be used.
- ...
- 2: This method
- 3: The class with the method which calls this method
- 4: The calling class
- ...
- Parameters:
callsBefore
- The number of calls before the method which requests the calling class- Returns:
- The calling class at the specified position
- Throws:
IllegalArgumentException
- If the specified calls before value is less than or equal to 0IndexOutOfBoundsException
- If the specified position is out of boundsIllegalCallerException
- If called from themain
method and 'unsafe.calls.main' isfalse
- See Also:
-
getCallingMethod
Gets the calling method from the stack trace with an offset of:
base + default
The base offset will be get from the system property 'unsafe.offset.base'.
If the property is not set, the default value3
will be used.
The default offset will be get from the system property 'unsafe.offset.default'.
If the property is not set, the default value1
will be used.
- ...
- 2: This method
- 3: The method which requests the calling method
- 4: The calling method
- Returns:
- The calling method
- Throws:
IndexOutOfBoundsException
- If the specified position is out of boundsIllegalStateException
- If the calling method could not be identifiedIllegalCallerException
- If called from themain
method and 'unsafe.calls.main' isfalse
- See Also:
-
getCallingMethod
Gets the calling method from the stack trace with an offset of:
base + default + callsBefore
The base offset will be get from the system property 'unsafe.offset.base'.
If the property is not set, the default value3
will be used.
The default offset will be get from the system property 'unsafe.offset.default'.
If the property is not set, the default value1
will be used.
- ...
- 2: This method
- 3: The method which calls this method
- 4: The calling method
- ...
- Parameters:
callsBefore
- The number of calls before the method which requests the calling method- Returns:
- The calling method at the specified position
- Throws:
IllegalArgumentException
- If the specified calls before value is less than or equal to 0IndexOutOfBoundsException
- If the specified position is out of boundsIllegalStateException
- If the calling method could not be identifiedIllegalCallerException
- If called from themain
method and 'unsafe.calls.main' isfalse
- See Also:
-
getCallingMethodSafe
Gets the calling method safe from the stack trace with an offset of:
base + default
The base offset will be get from the system property 'unsafe.offset.base'.
If the property is not set, the default value3
will be used.
The default offset will be get from the system property 'unsafe.offset.default'.
If the property is not set, the default value1
will be used.
- ...
- 2: This method
- 3: The method which requests the calling method
- 4: The calling method
- ...
- Returns:
- An optional containing the calling method or an empty optional if the calling method could not be identified or an error occurred
- Throws:
IndexOutOfBoundsException
- If the specified position is out of boundsIllegalCallerException
- If called from themain
method and 'unsafe.calls.main' isfalse
- See Also:
-
getCallingMethodSafe
Gets the calling method safe from the stack trace with an offset of:
base + default + callsBefore
The base offset will be get from the system property 'unsafe.offset.base'.
If the property is not set, the default value3
will be used.
The default offset will be get from the system property 'unsafe.offset.default'.
If the property is not set, the default value1
will be used.
- ...
- 2: This method
- 3: The method which calls this method
- 4: The calling method
- 5: The method which calls the method which requests the calling method
- ...
- Parameters:
callsBefore
- The number of calls before the method which requests the calling method- Returns:
- An optional containing the calling method or an empty optional if the calling method could not be identified or an error occurred
- Throws:
IllegalArgumentException
- If the specified calls before value is less than or equal to 0IndexOutOfBoundsException
- If the specified position is out of boundsIllegalCallerException
- If called from themain
method and 'unsafe.calls.main' isfalse
- See Also:
-
getDefaultOffset
private static int getDefaultOffset()Gets the default offset for the stack trace elements.
The default offset will be get from the system property 'unsafe.offset.default'.
If the property is not set, the default value1
will be used.
- Returns:
- The default offset
- See Also:
-
getStackTrace
Gets the stack trace element at the specified position.
This method is used internally to get the correct stack trace element.
The base offset will be get from the system property 'unsafe.offset.base'.
If the property is not set, the default value3
will be used.
- 0:
Thread.getStackTrace()
- 2: This method
- 2: The overloading method in this class
- 3: The method which requests the calling stack trace element
- 4: The calling method or class
- Parameters:
callsBefore
- The number of calls before the overloading method in this class- Returns:
- The stack trace element at the specified position
- Throws:
IllegalArgumentException
- If the specified calls before value is less than or equal to 0IndexOutOfBoundsException
- If the specified position is out of boundsIllegalCallerException
- If called from themain
method and 'unsafe.calls.main' isfalse
- 0:
-
getCallingMethod
@Nullable private static @Nullable Method getCallingMethod(@NotNull @NotNull StackTraceElement element) Gets the calling method from the specified stack trace element.
The calling method will be identified by the class name and the method name.
If there are multiple methods with the same name in the same class, the method will not be identified.- Parameters:
element
- The stack trace element- Returns:
- The calling method or
null
if the method could not be identified - Throws:
NullPointerException
- If the specified stack trace element isnull
-