简体   繁体   English

使用包含获取 DDIC 结构的组件

[英]Get components of DDIC structure with includes

I am using cl_abap_structdescr->get_components to get a list of fields in a structure.我正在使用cl_abap_structdescr->get_components来获取结构中的字段列表 It works fine when I use it on locally declared structure types, but when I use it on DDIC structures, it doesn't give me the results I expect.当我在本地声明的结构类型上使用它时它工作得很好,但是当我在 DDIC 结构上使用它时,它并没有给我我期望的结果。

Reproducable example:可重现的例子:

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.

Results:结果:

That's okay for GTY_OUTTAB: GTY_OUTTAB 没问题:

在此处输入图像描述

There are only two fields for P0008 although it contains many more fields (see below): P0008只有两个字段,尽管它包含更多字段(见下文):

在此处输入图像描述

I already tried using cl_abap_typedescr instead and googled, but every code I find online looks just like mine?我已经尝试改用cl_abap_typedescr并用谷歌搜索,但我在网上找到的每个代码看起来都和我的一样?

Here is the definition of P0008 which contains many fields as you can see:这是P0008的定义,其中包含许多字段,如您所见:

SE11 Hierarchy 显示中的 DDIC 结构 P0008

Of course after posting this, I found the reason why (german thread) .当然,在发布这个之后,我找到了原因 (德国线程) Apparently, if the given structure contains included structures, then get_components won't break them up.显然,如果给定结构包含包含结构,则get_components不会分解它们。 Three solutions have been suggested in the thread and they all work great for me.线程中提出了三种解决方案,它们都对我很有用。 Since I only need the structures' fieldnames, I will use option 1.由于我只需要结构的字段名,因此我将使用选项 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.

Method GET_RTTS_FOR_LOCAL_TABLE of helper-class CL_CACS_RTTS_HELPER seems to do exactly what you want and what lacks your option 1 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)
.

It fetches all components of a structure into reference data object and also includes absolute types:它将结构的所有组件提取到参考数据 object 中,还包括绝对类型:

在此处输入图像描述

In the same class there is get field list method, which may be sufficient.在同一个 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 ).

If that isnt enough.... try如果这还不够......试试

 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.

There is a method on cl_abap_structdescr called get_included_view( ) that will expand the included structures cl_abap_structdescr 上有一个名为 get_included_view( ) 的方法,它将扩展包含的结构

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM