如何用程式碼動態生成ABAP型別
In one of my project the data type of the variable to hold service consumption result is not known in design time so I have to generate the data type dynamically via code, using ABAP RTTC( runtime type creation ). For detail of RTTC and dynamic programming, please refer to sap help.
This blog will introduce a handy RTTC tool to generate any kind of complex data type in the runtime and demonstrate how to access the variable with that data type dynamically using field symbol. one example: I need to define a variable with the following data type. The structure of the data type is only known in the runtime:
Figure1: an example of data type definition
which needs to be generate in the runtime
The variable could have the component named “PRODUCTS” which is a table type, the table line type is a structure with five fields: PROD_ID, PROD_TXT, VALID_FROM, VALID_TO and COMPONENTS. The last field COMPONENTS is again a table type, with the table line type consisting of four fields COMP_ID, COMP_TXT, OPERATOR and again a table type for last field NOTES.
The row in the picture above marked with green seems a little strange and not necessary at a first glance, but it really makes sense. Consider the setting for table type in ABAP Dictionary.
How to use the RTTC tool
I write a simple report to create the data type and define a variable with that type using the RTTC tool zcl_rttc_tool( source code in attachment ). To use the tool, you just need to put into the definition of the data type to be generated, and the exporting parameter er_ref contains the reference pointing to the variable with the very data type.
To facilitate the filling of importing it_des_tab, I create three simple macro in report:
DEFINE define_table_type.APPEND INITIAL LINE TO lt_definition ASSIGNING <type>.
<type> = VALUE #( level = &1 field = &2 kind = ZCL_RTTC_TOOL=>c_table parent = &3 ).END-OF-DEFINITION.DEFINE define_tab_line_type.
APPEND INITIAL LINE TO lt_definition ASSIGNING <type>.
<type> = VALUE #( level = &1 kind = ZCL_RTTC_TOOL=>c_structure parent = &2 ).END-OF-DEFINITION.DEFINE define_element.APPEND INITIAL LINE TO lt_definition ASSIGNING <type>.
<type> = VALUE #( level = &1 field = &2 kind = ZCL_RTTC_TOOL=>c_element type = &3 parent = &4 ).END-OF-DEFINITION.
How to access the variable created by RTTC tool
We get the variable from tool which is a reference points to a structure which has a transient technical type. The type is only valid withing current session.
Although you can find the component PRODUCTS in debugger, you cannot use lr_data->products to access it in your code, since the structure is not known by compiler in design time.
Instead, you have to access it via so called dynamic programming via field symbol:
FIELD-SYMBOLS: <data> TYPE any,
<prod_tab> TYPE STANDARD TABLE,
<comp_tab> TYPE STANDARD TABLE,
<note_tab> TYPE STANDARD TABLE.ASSIGN lr_data->* TO <data>.CHECK sy-subrc = 0.ASSIGN COMPONENT 'PRODUCTS' OF STRUCTURE <data> TO <prod_tab>.CHECK sy-subrc = 0." create a new empty line to component PRODUCTSAPPEND INITIAL LINE TO <prod_tab> ASSIGNING FIELD-SYMBOL(<prod_line>).CHECK sy-subrc = 0."fill the field PROD_ID for the first table lineASSIGN COMPONENT 'PROD_ID' OF STRUCTURE <prod_line> TO FIELD-SYMBOL(<prod_id>).CHECK sy-subrc = 0.<prod_id> = 'MCF-0001'.ASSIGN COMPONENT 'COMPONENTS' OF STRUCTURE <prod_line> TO <comp_tab>.CHECK sy-subrc = 0." create a new empty line to component COMPONENTSAPPEND INITIAL LINE TO <comp_tab> ASSIGNING FIELD-SYMBOL(<comp_line>).CHECK sy-subrc = 0." fill COMP_ID for the first table lineASSIGN COMPONENT 'COMP_ID' OF STRUCTURE <comp_line> TO FIELD-SYMBOL(<comp_id>).CHECK sy-subrc = 0.<comp_id> = 'COMP_0001'.ASSIGN COMPONENT 'NOTES' OF STRUCTURE <comp_line> TO <note_tab>.CHECK sy-subrc = 0." create a new empty line to component NOTESAPPEND INITIAL LINE TO <note_tab> ASSIGNING FIELD-SYMBOL(<note_line>).CHECK sy-subrc = 0." fill NOTE_ID for the first table lineASSIGN COMPONENT 'NOTE_ID' OF STRUCTURE <note_line> TO FIELD-SYMBOL(<note_id>).CHECK sy-subrc = 0.<note_id> = 'NOTE_0001'.
the filled data is displayed in the debugger as below, let’s compare it with figure 1:
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2717306/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 淺談程式語言型別的強型別,弱型別,動態型別,靜態型別型別
- 動態型別一時爽,程式碼重構火葬場?型別
- 使用ABAP程式碼生成二維碼(QR Code)
- SAP ABAP關鍵字語法圖和ABAP程式碼自動生成工具Code Composer
- 如何用ABAP程式碼讀取CDS view association的資料View
- iOS使用指令碼跟隨工程程式碼動態生成FrameworkiOS指令碼Framework
- 動態圖和靜態圖的程式碼區別
- Unity學習-程式碼中動態生成預製體Unity
- 鴻蒙開發Hvigor外掛動態生成程式碼鴻蒙Go
- ABAP動態內表
- ABAP程式碼靜態分析工具SQF - Support Query FrameworkFramework
- 程式碼如人
- ABAP 動態where語句
- Python進階09 動態型別Python型別
- C++ 動態型別轉換C++型別
- C# 中的動態型別C#型別
- ABAP和Hybris的原始碼生成工具原始碼
- 程式碼靜態掃描規則——型別轉換檢查型別
- java使用bytebuddy動態生成帶泛型的DTOJava泛型
- SAP SEGW 事物碼裡的 ABAP 型別和 EDM 型別對映的一個具體例子型別
- 型別 列表生成 解包型別
- JavaScript 程式碼報錯型別JavaScript型別
- chagpt自動生成PPT程式碼GPT
- Android JNI 程式碼自動生成Android
- 程式碼自動生成外掛:
- 淺談Java和SAP ABAP的靜態代理和動態代理,以及ABAP面向切面程式設計的嘗試Java程式設計
- 概念區別 【編譯型語言與解釋型語言、動態型別語言與靜態型別語言、強型別語言與弱型別語言】編譯型別
- Python生成動態二維碼,MyQR介紹Python
- Python學習筆記6——動態型別Python筆記型別
- ABAP文件生成工具
- 深挖JDK動態代理(二):JDK動態生成後的位元組碼分析JDK
- beego 程式碼自動生成器Go
- phpqrcode生成動態二維碼簡單例項PHP單例
- Jquery Datatables (2) 動態載入資料型別jQuery資料型別
- 計算機語言:編譯型/解釋型、動態語言/靜態語言、強型別語言/弱型別語言計算機編譯型別
- 《程式碼大全》程式碼生成
- SAP ABAP 的兩種記憶體物件型別記憶體物件型別
- midjourney 生成相似型別圖片型別