SAP 另类创建动态对象的方法+RTTS(GENERATE SUBROUTINE POOL不建议使用)

=>建议使用RTTS

  • subroutine最大调用36次:64201 - Maximum number of temporary subroutine pools

1. 使用subroutine(不建议使用)

SAP 另类创建动态对象的方法+RTTS(GENERATE SUBROUTINE POOL不建议使用)_第1张图片

    DATA: lt_source TYPE TABLE OF string.
    DATA: lv_name     TYPE sy-repid VALUE 'ZTEMP_GET_DYNAMIC_OBJECT',
          lv_form(30) TYPE c VALUE 'FRM_GENERATE_OBJECT',
          lv_message  TYPE string.
    FIELD-SYMBOLS:  TYPE any.

    APPEND 'REPORT' && ` ` && lv_name && '.' TO lt_source.
    APPEND 'FORM' && ` ` && lv_form && ` ` && 'USING ref_fs TYPE any.' TO lt_source.
    APPEND 'DATA: lo_pointer TYPE REF TO data.' TO lt_source.                   "定义一个指针对象
    APPEND 'CREATE DATA lo_pointer TYPE' && ` ` && iv_ref_field && '.' TO lt_source.   "实例化对象
    APPEND 'ref_fs = lo_pointer.' TO lt_source.
    APPEND 'ENDFORM.' TO lt_source.

    "生成子程序
    TRY.
        GENERATE SUBROUTINE POOL lt_source NAME lv_name.
      CATCH cx_sy_generate_subpool_full INTO DATA(lo_subpool_full).
        lv_message = lo_subpool_full->get_text( ).
        MESSAGE e000(0k) WITH lv_message.
    ENDTRY.

    "assign object
    ASSIGN cv_object TO .
    CHECK  IS ASSIGNED.

    "call subroutine
    PERFORM (lv_form) IN PROGRAM (lv_name) USING .

2. RTTS

  • 常规方法使用类:cl_alv_table_create=>create_dynamic_table
  • 其他方法类:
    • cl_abap_elemdescr
    • cl_abap_structdescr
    • cl_abap_tabledescr

SAP 另类创建动态对象的方法+RTTS(GENERATE SUBROUTINE POOL不建议使用)_第2张图片

    DATA: lt_component TYPE cl_abap_structdescr=>component_table,
          ls_component LIKE LINE OF lt_component.

    DATA: lo_structdescr TYPE REF TO cl_abap_structdescr,
          lo_tabledescr  TYPE REF TO cl_abap_tabledescr.

    DATA: lv_ref_field TYPE string,
          lv_table     TYPE string,
          lv_field     TYPE string.

    lv_ref_field = iv_ref_field.  "Value ACDOCA-RACCT

    SPLIT lv_ref_field AT '-' INTO lv_table lv_field.

    "Fill Component
    ls_component-name = lv_field.
    ls_component-type ?= cl_abap_typedescr=>describe_by_name( iv_ref_field ).      "Get DDIC Element Info
    APPEND ls_component TO lt_component.

    "Create a New Type
    lo_structdescr = cl_abap_structdescr=>create( lt_component ).

*    "Create dynamic internal Table type
*    lo_tabledescr = cl_abap_tabledescr=>create( p_line_type  = lo_structdescr
*                                                p_table_kind = cl_abap_tabledescr=>tablekind_std
*                                                p_unique     = abap_false ).

    "Dynamic Structure
    CREATE DATA cv_object TYPE HANDLE lo_structdescr.

*    "Dynamic Table
*    CREATE DATA cv_object TYPE HANDLE lo_tabledescr.

3.效果

  • subroutine
    SAP 另类创建动态对象的方法+RTTS(GENERATE SUBROUTINE POOL不建议使用)_第3张图片
    SAP 另类创建动态对象的方法+RTTS(GENERATE SUBROUTINE POOL不建议使用)_第4张图片
  • RTTS
    SAP 另类创建动态对象的方法+RTTS(GENERATE SUBROUTINE POOL不建议使用)_第5张图片
    SAP 另类创建动态对象的方法+RTTS(GENERATE SUBROUTINE POOL不建议使用)_第6张图片

你可能感兴趣的:(ABAP,实用程序记录,ABAP开发,ABAP,SAP)