Determine the current class and method names in ABAP
In some situations, it can be useful to be able to determine the names of the current class and method dynamically instead of using hard-coded strings. I find this particularly useful for logging purposes. I have a helper class which writes to the system log when certain exceptions are triggered. As input to this helper class (or rather the method writing to the system log) I want to provide the names of the class and method where the exception was raised.
Determining the name of the current class
SAP provides the class
CL_ABAP_CLASSDESCR for determining some class attributes dynamically. To determine the name of the current class, the following code snippet can be used:
DATA(lv_class_name) = cl_abap_classdescr=>get_class_name( me ).
LV_CLASS_NAME will contain the class name in the following format:
Determining the name of the current method
The only approach I’ve found for determining the name of the current method is by using a function module to read the call stack. If you are aware of a better way of doing this, please leave a comment! The call stack approach looks like this:
DATA lt_callstack TYPE abap_callstack. CALL FUNCTION 'SYSTEM_CALLSTACK' EXPORTING max_level = 1 IMPORTING callstack = lt_callstack. DATA(lv_method_name) = lt_callstack[ 1 ]-blockname.
LV_METHOD_NAME will contain the method name in the following format:
This blog post first appeared on the Developer Voyage blog at https://www.developervoyage.com/2019/09/13/determine-the-current-class-and-method-names.html
Surely the call stack method looks something like this
I've not actually tried this yet.
Thanks for your comment. There is one small change needed to make your code work. You need to adjust the max_level like this:
Oh yes. I missed that bit.
You could also use class CL_ABAP_GET_CALL_STACK (with the methods like GET_CALL_STACK( ) or FORMAT_CALL_STACK_WITH_STRUCT()) instead of the function module.
Thanks for your suggestion. That is an interesting approach! So something like this:
Yes. Something like that 🙂
And generally I try to avoid to table[ 1 ] when it's not checked that it's not initial, otherwise it dumps.
because it should never not be initial.
I've made my code robust(er) now. (And in accordance with DSUG and SAP Guideline, and the clean code project - no pesky typing prefixes... 😉 ).
The class will always return all stack, but function has option to restrict by level of stack.
The code symbols for the function is less than for the class.
and this class is static... which is not recommended by abap-help.
CX_ROOT (and thus every exception) has method GET_SOURCE_POSITION which provides the current executed program, the name of the include and the line in the coding. This is not really the wanted class/method but as also non oo functions can raise class based exceptions it works in every situation.
In case the output is not working you could try catching the exception in question with the option "before unwind"