如何用代码动态生成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 PRODUCTS
APPEND INITIAL LINE TO <prod_tab> ASSIGNING FIELD-SYMBOL(<prod_line>).
CHECK sy-subrc = 0.
"fill the field PROD_ID for the first table line
ASSIGN 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 COMPONENTS
APPEND INITIAL LINE TO <comp_tab> ASSIGNING FIELD-SYMBOL(<comp_line>).
CHECK sy-subrc = 0.
" fill COMP_ID for the first table line
ASSIGN 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 NOTES
APPEND INITIAL LINE TO <note_tab> ASSIGNING FIELD-SYMBOL(<note_line>).
CHECK sy-subrc = 0.
" fill NOTE_ID for the first table line
ASSIGN 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:
- Spring Cloud(八)高可用的分布式配置中心 Spring Cloud Config
- 用Raspberry Pi Zero打造「即插即用」的Web服务器
- Spring Cloud(七)服务网关 Zuul Filter 使用
- 基于Metronic的Bootstrap开发框架经验总结(1)-框架总览及菜单模块的处理
- Spring Cloud(六)服务网关 zuul 快速入门
- Docker Registry Server 搭建,配置免费HTTPS证书,及拥有权限认证、TLS 的私有仓库
- Ubuntu 17.04 编译安装 Nginx 1.9.9 配置 https 免费证书
- 基于Metronic的Bootstrap开发框架经验总结(2)--列表分页处理和插件JSTree的使用
- Docker Image 解决镜像无法删除的问题
- Docker Hub 仓库使用,及搭建 Docker Registry
- 基于Metronic的Bootstrap开发框架经验总结(3)--下拉列表Select2插件的使用
- Docker 容器操作
- Ubuntu 17.04 x64 安装 Docker CE 初窥 Dockerfile 部署 Nginx
- 基于Metronic的Bootstrap开发框架经验总结(4)--Bootstrap图标的提取和利用
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- C语言 | 每日基础(37)
- 《求求大厂给个Offer》Map面试题
- C语言 | 每日基础(40)
- kubernete编排技术七:secret
- springcloud+eureka整合seata-tcc模式
- 【源码】手把手教你用Python实现Vivado和ModelSim仿真自动化
- typescript基础篇(7):类型检查机制
- 说来惭愧,我被ulimit摔了一跤...
- Kubectl 备忘录
- 手把手教你移动端AI应用开发(二)——将AI模型集成到安卓应用中
- Kubernetes v1.19.0 正式发布!
- 七夕节脱单“神助攻”!AI教你写情话
- 手把手教你移动端AI应用开发(三)——部署环节关键代码最详解读
- 一次完整的JVM堆外内存泄漏故障排查记录
- Python 爬虫进阶必备 | 某视频数据分析平台加密参数分析(终于我还是手把手扣了代码)