Data structures 使用includes获取DDIC结构的组件

Data structures 使用includes获取DDIC结构的组件,data-structures,abap,sap-data-dictionary,rtts,Data Structures,Abap,Sap Data Dictionary,Rtts,我正在使用cl\u abap\u structdescr->get\u components获取结构中的字段列表。当我在本地声明的结构类型上使用它时,它工作得很好,但是当我在DDIC结构上使用它时,它并没有给我预期的结果 可复制示例: TYPES: BEGIN OF gty_outtab, infty TYPE infty, uname TYPE uname, bdate TYPE datum, btime TYPE uzeit, pernr TYPE pernr_d,

我正在使用
cl\u abap\u structdescr->get\u components
获取结构中的字段列表。当我在本地声明的结构类型上使用它时,它工作得很好,但是当我在DDIC结构上使用它时,它并没有给我预期的结果

可复制示例:

TYPES: BEGIN OF gty_outtab,
  infty TYPE infty,
  uname TYPE uname,
  bdate TYPE datum,
  btime TYPE uzeit,
  pernr TYPE pernr_d,
  opera TYPE hr_opera,
  begda TYPE begda,
  endda TYPE endda,
END OF gty_outtab.

DATA: lr_infty_structdescr  TYPE REF TO cl_abap_structdescr,
      lr_outtab_structdescr TYPE REF TO cl_abap_structdescr,
      lt_outtab_components  TYPE STANDARD TABLE OF abap_componentdescr,
      lt_infty_components   TYPE STANDARD TABLE OF abap_componentdescr.

" works as expected
lr_outtab_structdescr ?= cl_abap_structdescr=>describe_by_name( 'GTY_OUTTAB' ).
lt_outtab_components = lr_outtab_structdescr->get_components( ).

" doesn't work as expected
lr_infty_structdescr ?= cl_abap_structdescr=>describe_by_name( 'P0008' ).
lt_infty_components = lr_infty_structdescr->get_components( ).

BREAK-POINT.
结果:

TYPES: BEGIN OF gty_outtab,
  infty TYPE infty,
  uname TYPE uname,
  bdate TYPE datum,
  btime TYPE uzeit,
  pernr TYPE pernr_d,
  opera TYPE hr_opera,
  begda TYPE begda,
  endda TYPE endda,
END OF gty_outtab.

DATA: lr_infty_structdescr  TYPE REF TO cl_abap_structdescr,
      lr_outtab_structdescr TYPE REF TO cl_abap_structdescr,
      lt_outtab_components  TYPE STANDARD TABLE OF abap_componentdescr,
      lt_infty_components   TYPE STANDARD TABLE OF abap_componentdescr.

" works as expected
lr_outtab_structdescr ?= cl_abap_structdescr=>describe_by_name( 'GTY_OUTTAB' ).
lt_outtab_components = lr_outtab_structdescr->get_components( ).

" doesn't work as expected
lr_infty_structdescr ?= cl_abap_structdescr=>describe_by_name( 'P0008' ).
lt_infty_components = lr_infty_structdescr->get_components( ).

BREAK-POINT.
这对GTY_OUTTAB来说没关系:

P0008
只有两个字段,但它包含更多字段(见下文):

我已经试过用
cl\u abap\u typedescr
来代替谷歌搜索,但我在网上找到的每一个代码都和我的一样

以下是
P0008
的定义,其中包含许多字段,如您所见:


当然,在发布这篇文章之后,我找到了原因。显然,如果给定的结构包含包含的结构,那么
get\u组件
将不会分解它们。在这篇文章中提出了三种解决方案,它们对我都很有效。因为我只需要结构的字段名,所以我将使用选项1

DATA: lt_infty_complist1 TYPE abap_compdescr_tab,
      lt_infty_complist2 TYPE STANDARD TABLE OF fieldname,
      lt_infty_complist3 TYPE abap_component_tab.

" 1 - get full fieldname list, but with barely any typedescription
lt_infty_complist1 = lr_infty_structdescr->components.

" 2 - get full fieldname list of DDIC structures, but without typedescription
SELECT fieldname
  FROM dd03l
  INTO TABLE lt_infty_complist2
 WHERE tabname = 'P0008'.

DELETE lt_infty_complist2 WHERE table_line = '.INCLU--AP'
                             OR table_line = '.INCLUDE'.

" 3 - get full component list 
" function code from: https://www.abapforum.com/forum/viewtopic.php?f=18&p=59840)
PERFORM return_components USING lr_infty_structdescr CHANGING lt_infty_complist3.

当然,在发布这篇文章之后,我找到了原因。显然,如果给定的结构包含包含的结构,那么
get\u组件
将不会分解它们。在这篇文章中提出了三种解决方案,它们对我都很有效。因为我只需要结构的字段名,所以我将使用选项1

DATA: lt_infty_complist1 TYPE abap_compdescr_tab,
      lt_infty_complist2 TYPE STANDARD TABLE OF fieldname,
      lt_infty_complist3 TYPE abap_component_tab.

" 1 - get full fieldname list, but with barely any typedescription
lt_infty_complist1 = lr_infty_structdescr->components.

" 2 - get full fieldname list of DDIC structures, but without typedescription
SELECT fieldname
  FROM dd03l
  INTO TABLE lt_infty_complist2
 WHERE tabname = 'P0008'.

DELETE lt_infty_complist2 WHERE table_line = '.INCLU--AP'
                             OR table_line = '.INCLUDE'.

" 3 - get full component list 
" function code from: https://www.abapforum.com/forum/viewtopic.php?f=18&p=59840)
PERFORM return_components USING lr_infty_structdescr CHANGING lt_infty_complist3.

helper类
CL\u CACS\u RTTS\u helper
的方法
GET\u RTTS\u FOR\u LOCAL\u TABLE
似乎正是您想要做的,并且缺少选项1

CALL METHOD cl_cacs_rtts_helper=>get_rtts_for_local_structure
  EXPORTING
    id_tabname = 'P0008'
  receiving
    ro_data    = DATA(ro_struct)
.
它将结构的所有组件提取到引用数据对象中,还包括绝对类型:


助手类的方法
GET\u RTTS\u FOR\u LOCAL\u TABLE
似乎正是您想要做的,并且缺少选项1

CALL METHOD cl_cacs_rtts_helper=>get_rtts_for_local_structure
  EXPORTING
    id_tabname = 'P0008'
  receiving
    ro_data    = DATA(ro_struct)
.
它将结构的所有组件提取到引用数据对象中,还包括绝对类型:


在同一个类中,有get field list方法,这可能就足够了

data: lo_incl_stru TYPE REF TO cl_abap_structdescr,
       lt_field_list TYPE ddfields.

    lt_field_list =  lo_incl_stru->get_ddic_field_list( p_including_substructres = abap_true ).
如果这还不够。。。。试一试

 METHODS recursive_get_components
      IMPORTING
        io_structdescr       TYPE REF TO cl_abap_structdescr
      RETURNING
        VALUE(rt_components) TYPE abap_component_tab.


METHOD recursive_get_components.
    DATA:
      lo_incl_stru TYPE REF TO cl_abap_structdescr,
      lt_incl_comp TYPE abap_component_tab,
      l_curr_index TYPE i.

    FIELD-SYMBOLS: <comp>      LIKE LINE OF rt_components,
                   <incl_comp> LIKE LINE OF lt_incl_comp.


    rt_components = io_structdescr->get_components( ).

    LOOP AT rt_components ASSIGNING <comp>.
      IF <comp>-as_include = 'X'.
        lo_incl_stru ?= <comp>-type.  " not the include struc type
        l_curr_index = sy-tabix.      " and the index it is to be included
        DELETE rt_components INDEX l_curr_index.

        lt_incl_comp = recursive_get_components( io_structdescr =  lo_incl_stru  ).
        LOOP AT lt_incl_comp ASSIGNING <incl_comp>.
          INSERT <incl_comp> INTO rt_components INDEX l_curr_index.
          l_curr_index = l_curr_index + 1.
        ENDLOOP.
      ENDIF.

    ENDLOOP.


  ENDMETHOD.
方法递归\u获取\u组件
进口
io_structdescr类型参考至cl_abap_structdescr
返回
值(rt_组件)类型abap_组件选项卡。
方法递归获取组件。
数据:
lo_包括cl_abap_结构说明的结构类型参考,
lt_包括组件类型abap_组件选项卡,
l_curr_索引类型i。
字段符号:如rt_组件线,
类似于lt_incl_comp的生产线。
rt\u components=io\u structdescr->get\u components()。
在rt_组件分配时循环。
IF-as_include='X'。
lo_incl_stru?=-类型。“不包括结构类型
l_curr_index=sy tabix.”以及要包含的索引
删除rt_组件索引l_curr_索引。
lt_incl_comp=递归获取组件(io_structdescr=lo_incl_stru)。
在lt_处循环,包括组件分配。
插入rt_组件索引l_curr_索引。
货币指数=货币指数+1。
结束循环。
恩迪夫。
结束循环。
ENDMETHOD。

在同一类中,有get field list方法,这可能就足够了

data: lo_incl_stru TYPE REF TO cl_abap_structdescr,
       lt_field_list TYPE ddfields.

    lt_field_list =  lo_incl_stru->get_ddic_field_list( p_including_substructres = abap_true ).
如果这还不够。。。。试一试

 METHODS recursive_get_components
      IMPORTING
        io_structdescr       TYPE REF TO cl_abap_structdescr
      RETURNING
        VALUE(rt_components) TYPE abap_component_tab.


METHOD recursive_get_components.
    DATA:
      lo_incl_stru TYPE REF TO cl_abap_structdescr,
      lt_incl_comp TYPE abap_component_tab,
      l_curr_index TYPE i.

    FIELD-SYMBOLS: <comp>      LIKE LINE OF rt_components,
                   <incl_comp> LIKE LINE OF lt_incl_comp.


    rt_components = io_structdescr->get_components( ).

    LOOP AT rt_components ASSIGNING <comp>.
      IF <comp>-as_include = 'X'.
        lo_incl_stru ?= <comp>-type.  " not the include struc type
        l_curr_index = sy-tabix.      " and the index it is to be included
        DELETE rt_components INDEX l_curr_index.

        lt_incl_comp = recursive_get_components( io_structdescr =  lo_incl_stru  ).
        LOOP AT lt_incl_comp ASSIGNING <incl_comp>.
          INSERT <incl_comp> INTO rt_components INDEX l_curr_index.
          l_curr_index = l_curr_index + 1.
        ENDLOOP.
      ENDIF.

    ENDLOOP.


  ENDMETHOD.
方法递归\u获取\u组件
进口
io_structdescr类型参考至cl_abap_structdescr
返回
值(rt_组件)类型abap_组件选项卡。
方法递归获取组件。
数据:
lo_包括cl_abap_结构说明的结构类型参考,
lt_包括组件类型abap_组件选项卡,
l_curr_索引类型i。
字段符号:如rt_组件线,
类似于lt_incl_comp的生产线。
rt\u components=io\u structdescr->get\u components()。
在rt_组件分配时循环。
IF-as_include='X'。
lo_incl_stru?=-类型。“不包括结构类型
l_curr_index=sy tabix.”以及要包含的索引
删除rt_组件索引l_curr_索引。
lt_incl_comp=递归获取组件(io_structdescr=lo_incl_stru)。
在lt_处循环,包括组件分配。
插入rt_组件索引l_curr_索引。
货币指数=货币指数+1。
结束循环。
恩迪夫。
结束循环。
ENDMETHOD。

在cl\u abap\u structdescr上有一个名为get\u included\u view()的方法可以扩展包含的结构

在cl\u abap\u structdescr上有一个名为get\u included\u view()的方法可以扩展包含的结构

谢谢,看起来很方便。我会将此标记为答案,但我不能这样做,因为这个类在我的发行版上不存在(ABAP 700,没有增强包)。是的,在我的裸机700 SP05上也不存在,但在740 EHP7 SP04上存在。好像是在7点31分到7点40分之间介绍的。谢谢,看起来很方便。我会将此标记为答案,但我不能这样做,因为这个类在我的发行版上不存在(ABAP 700,没有增强包)。是的,在我的裸机700 SP05上也不存在,但在740 EHP7 SP04上存在。似乎它是在7.31和7.40之间引入的。感谢您提供了这个漂亮的递归方法!谢谢你那漂亮的递归方法!谢谢你,这正是我想要的,一直就在我眼前,哈哈谢谢你,这正是我想要的,一直就在我眼前,哈哈