Text Pools are great for creating language independent program. But, it create a very peculiar problem when used in ABAP objects
What is Text Symbols?
Text Symbols are part of the Text Pool. This allow you to create, your development object without hard coding the language specific literals.
For example ‘Good Morning’ in English would be ‘guten Morgen’ in German. If you don’t use Text Symbols, you need have to set these literals based on log on language. Ahh Bad design – Need to change every time a new language is required, too may conditions! Easy solution would be create your literals as Text Symbols and translate the text symbols in the required languages.
More on Text Symbols – Text Symbols on SAP Help
Problem when using in ABAP objects
In ABAP objects as well, you can use Text Symbols to distinguish your Literals out of the coding. The problem is when you create a text symbol in a Super Class, you can’t reuse that in your subclass – defeating the purpose of the ABAP objects.
Lets see by an example:
You have a ALV super class, where you set the Field catalog. This is a common and you are inheriting other subclasses for different ALVs. To make your program language independent, you have used the Text Symbols. So far so good. Now for some reason, you need to completely wipe off your Field catalog in your Inherited class. You have about bunch of columns which you want to reuse the text symbols created in Super class. Since Text symbols are embedded in Super Class, they are not accessible from any other class.
Class ZCL_TEST_NP_SUPER has the logic to build the Field Catalog and it has text symbol T01.
I created a small utility class. In this, you specify your current object (as me) when instantiating the object. This utility class has method to get access of the specific text symbol. The object actually collects all the classes in the class hierarchy for which you want to read text symbols. Once collected, it would get the text symbols by using READ
Whenever you need to get an access to the class, you include this object in your class. Call the method _TEXT( ) and you are done.
Class methods & Attributes
This class has these components:
- Method CONSTRUCTOR – Accept the current object as the object reference. Logic would determine the actual Class name using the RTTS Object descriptor. You use the RTTS to describe the type & Create the type to create dynamic tables are runtime. Once the class name is obtained, it would than look for class’s parent class from where it got inherited using the method GET_ALL_SUPER. It would collect all classes till it reached the point where there is no Super class. Using READ TEXTPOOL, method would determine all the text Symbols used in its Super Class.
- Method TEXT_ – I wanted to keep the name similar to kind TEXT- so, I used method name as TEXT_. In this method, you read the collected text from the internal table and return back the value.
- Attribute T_TEXT – This internal table collect the text elements from the text pool of all super classes in class hierarchy.
Code lines for ZCL_TEXT_SYMBOL definition and implementation
*&---------------------------------------------------------------------* *& Purpose : Utility to access Text Symbols from Super Class *& Author : Naimesh Patel *& URL : http://zevolving.com/?p=1769 *&---------------------------------------------------------------------* CLASS zcl_text_symbol DEFINITION. PUBLIC SECTION. METHODS: constructor IMPORTING io_obj TYPE REF TO object, text_ IMPORTING textid TYPE char3 RETURNING value(rv_val) TYPE string. PRIVATE SECTION. TYPES: BEGIN OF lty_super, name TYPE string, END OF lty_super. TYPES: tt_super TYPE STANDARD TABLE OF lty_super WITH DEFAULT KEY. DATA: t_text TYPE TABLE OF textpool. METHODS: get_all_super IMPORTING iv_class_name TYPE string RETURNING value(rt_super) TYPE tt_super. ENDCLASS. "zcl_text_symbol DEFINITION * * * CLASS zcl_text_symbol IMPLEMENTATION. METHOD constructor. DATA: lo_obj_desc TYPE REF TO cl_abap_objectdescr. DATA: lv_class_name TYPE string. DATA: lt_super TYPE tt_super. DATA: ls_super LIKE LINE OF lt_super. DATA: lv_class_inc TYPE char32. DATA: lt_text TYPE TABLE OF textpool. * get class name lo_obj_desc ?= cl_abap_objectdescr=>describe_by_object_ref( io_obj ). lv_class_name = lo_obj_desc->get_relative_name( ). * * get all super classes lt_super = me->get_all_super( lv_class_name ). * * For each supre class, build the class include name and read * Texts LOOP AT lt_super INTO ls_super. * lv_class_inc = ls_super-name. lv_class_inc+30(2) = 'CP'. TRANSLATE lv_class_inc USING ' ='. * READ TEXTPOOL lv_class_inc INTO lt_text LANGUAGE sy-langu. APPEND LINES OF lt_text TO t_text. ENDLOOP. SORT t_text BY id. ENDMETHOD. "constructor METHOD text_. DATA: ls_text LIKE LINE OF me->t_text. * get text of text_id READ TABLE me->t_text INTO ls_text WITH KEY key = textid BINARY SEARCH. CHECK sy-subrc EQ 0. * send back the value rv_val = ls_text-entry. ENDMETHOD. "_text * METHOD get_all_super. TYPE-POOLS: seor. DATA: lv_super TYPE vseoextend-refclsname. DATA: lv_first TYPE flag. DATA: ls_key TYPE seoclskey. DATA: ls_inher TYPE seor_inheritance_r. DATA: ls_super LIKE LINE OF rt_super. * when there is class determined CHECK iv_class_name IS NOT INITIAL. lv_first = 'X'. ls_key = iv_class_name. * till the top node or the first time WHILE lv_super IS NOT INITIAL OR lv_first = 'X'. CLEAR lv_first. " no more first time * get class inheritance CALL FUNCTION 'SEO_INHERITANC_READ' EXPORTING clskey = ls_key IMPORTING inheritance = ls_inher EXCEPTIONS class_not_existing = 1 OTHERS = 2. * exit when error IF sy-subrc <> 0. EXIT. ENDIF. * collect the super class in return table CLEAR lv_super. IF ls_inher-refclsname IS NOT INITIAL. ls_super-name = ls_inher-refclsname. APPEND ls_super TO rt_super. lv_super = ls_inher-refclsname. * super is now key ls_key = lv_super. ELSE. EXIT. " exit when no super class ENDIF. ENDWHILE. ENDMETHOD. "get_all_super ENDCLASS. "zcl_text_symbol IMPLEMENTATION
Here is the demo code which uses this utility class to access the text symbol from the Super class:
METHOD build_fcat. * no need to call super DATA: ct_fcat TYPE lvc_t_fcat. FIELD-SYMBOLS: <lfs_fcat> LIKE LINE OF ct_fcat. " WA for FCAT * * Utility to access text symbols DATA: lo_text TYPE REF TO zcl_text_symbol. CREATE OBJECT lo_text EXPORTING io_obj = me. * APPEND INITIAL LINE TO ct_fcat ASSIGNING <lfs_fcat>. <lfs_fcat>-col_pos = 5. <lfs_fcat>-fieldname = 'ZZWEIGHT'. <lfs_fcat>-outputlen = '11'. <lfs_fcat>-scrtext_l = lo_text->text_( 'T01' ). " Accessing Super's T01 <lfs_fcat>-scrtext_m = lo_text->text_( 'T01' ). ENDMETHOD.
Let me know what do you think – Text Symbols in Class Pool are Public or Private? Read Poll – Are Text Symbols Private or Public in OO ABAP? for full story.
Are Text Symbols Public or Private?
- Private - no body can use it (44%, 49 Votes)
- Public - Every client can use it (43%, 48 Votes)
- Not Sure - I can't decide (13%, 14 Votes)
Total Voters: 111
Do you have any other trick to access the Text Symbols of the Super class within the methods of the inherited class?