使用 includes 获取 DDIC 结构的组件

Get components of DDIC structure with includes

我正在使用 cl_abap_structdescr->get_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.

结果:

没关系 GTY_OUTTAB:

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

我已经尝试使用 cl_abap_typedescr 并进行了谷歌搜索,但我在网上找到的每个代码看起来都和我的一样?

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

发了这个当然找到原因了(german thread)。显然,如果给定的结构包含包含的结构,那么 get_components 将不会分解它们。线程中提出了三种解决方案,它们都对我很有效。因为我只需要结构的字段名,所以我将使用选项 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-class CL_CACS_RTTS_HELPER 的方法 GET_RTTS_FOR_LOCAL_TABLE 似乎完全符合您的要求,但缺少选项 1

CALL METHOD cl_cacs_rtts_helper=>get_rtts_for_local_structure
  EXPORTING
    id_tabname = 'P0008'
  receiving
    ro_data    = DATA(ro_struct)
.

它将结构的所有组件提取到引用数据对象中,还包括绝对类型:

在同一个class中有获取字段列表的方法,这可能就足够了。

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.

cl_abap_structdescr 上有一个名为 get_included_view( ) 的方法可以扩展包含的结构