Skip to content

cl_abap_structdescr: get_symbols implementation, included struct fix, renaming with suffix fix #935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 55 additions & 15 deletions src/rtti/cl_abap_structdescr.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,39 @@ CLASS cl_abap_structdescr DEFINITION PUBLIC INHERITING FROM cl_abap_complexdescr
METHODS update_components.

DATA mt_refs TYPE component_table.
DATA mt_refs_comp TYPE component_table.
ENDCLASS.

CLASS cl_abap_structdescr IMPLEMENTATION.

METHOD get_symbols.
ASSERT 1 = 'todo'.
DATA lt_components TYPE component_table.
DATA ls_symbol LIKE LINE OF p_result.
DATA lt_symbols TYPE symbol_table.
DATA lo_structdescr TYPE REF TO cl_abap_structdescr.

FIELD-SYMBOLS <ls_component> LIKE LINE OF lt_components.
FIELD-SYMBOLS <ls_symbol> LIKE LINE OF lt_symbols.

lt_components = get_components( ).
LOOP AT lt_components ASSIGNING <ls_component>.
IF <ls_component>-name IS NOT INITIAL.
ls_symbol-name = <ls_component>-name.
ls_symbol-type = <ls_component>-type.
INSERT ls_symbol INTO TABLE p_result.
ENDIF.
IF <ls_component>-as_include = abap_true.
lo_structdescr ?= <ls_component>-type.
lt_symbols = lo_structdescr->get_symbols( ).
LOOP AT lt_symbols ASSIGNING <ls_symbol>.
CONCATENATE <ls_symbol>-name <ls_component>-suffix INTO ls_symbol-name.
CONCATENATE <ls_symbol>-name <ls_component>-suffix INTO ls_symbol-name.
ls_symbol-type = <ls_symbol>-type.
INSERT ls_symbol INTO TABLE p_result.
ENDLOOP.
ENDIF.
ENDLOOP.

ENDMETHOD.

METHOD get.
Expand Down Expand Up @@ -113,21 +140,14 @@ CLASS cl_abap_structdescr IMPLEMENTATION.
ENDMETHOD.

METHOD get_included_view.
DATA ls_component LIKE LINE OF components.
DATA ls_view LIKE LINE OF p_result.
DATA ls_ref LIKE LINE OF mt_refs.

LOOP AT components INTO ls_component.
LOOP AT mt_refs INTO ls_ref WHERE as_include = abap_false.
CLEAR ls_view.

ls_view-name = ls_component-name.
READ TABLE mt_refs WITH KEY name = ls_component-name INTO ls_ref.
IF sy-subrc = 0.
ls_view-type = ls_ref-type.
ENDIF.
IF ls_ref-as_include = abap_true.
CONTINUE.
ENDIF.
ls_view-name = ls_ref-name.
ls_view-type = ls_ref-type.

INSERT ls_view INTO TABLE p_result.
ENDLOOP.
Expand Down Expand Up @@ -196,8 +216,9 @@ CLASS cl_abap_structdescr IMPLEMENTATION.
WRITE '@KERNEL }'.
ls_ref-as_include = lv_as_include.

WRITE '@KERNEL if (INPUT.data?.getSuffix) {'.
WRITE '@KERNEL lv_as_include.set(INPUT.data?.getSuffix()?.[name.toLowerCase()] || "");'.
WRITE '@KERNEL if (INPUT.data?.getRenamingSuffix) {'.
WRITE '@KERNEL lv_suffix.set(INPUT.data?.getRenamingSuffix()?.[name.toLowerCase()] || "");'.
TRANSLATE lv_suffix TO UPPER CASE.
WRITE '@KERNEL }'.
ls_ref-suffix = lv_suffix.

Expand All @@ -208,21 +229,40 @@ CLASS cl_abap_structdescr IMPLEMENTATION.
ENDMETHOD.

METHOD update_components.
DATA ls_component LIKE LINE OF components.
DATA ls_component LIKE LINE OF components.
DATA lo_structdescr TYPE REF TO cl_abap_structdescr.
DATA lt_components TYPE abap_component_tab.
DATA lv_name TYPE string.
FIELD-SYMBOLS <ls_ref> LIKE LINE OF mt_refs.
FIELD-SYMBOLS <ls_component> LIKE LINE OF lt_components.

CLEAR components.

mt_refs_comp = mt_refs.

LOOP AT mt_refs ASSIGNING <ls_ref>.
ls_component-name = <ls_ref>-name.
ls_component-type_kind = <ls_ref>-type->type_kind.
ls_component-length = <ls_ref>-type->length.
ls_component-decimals = <ls_ref>-type->decimals.
APPEND ls_component TO components.
ENDLOOP.

* the mt_refs contain really everything available in runtime
* the mt_ref_comp contains only the components that are not added as "as include" (needed for get_components)
LOOP AT mt_refs ASSIGNING <ls_ref> WHERE as_include = abap_true.
lo_structdescr ?= <ls_ref>-type.
lt_components = lo_structdescr->get_components( ).
LOOP AT lt_components ASSIGNING <ls_component>.
CONCATENATE <ls_component>-name <ls_ref>-suffix INTO lv_name.
DELETE mt_refs_comp WHERE name = lv_name.
ENDLOOP.
ENDLOOP.

ENDMETHOD.

METHOD get_components.
rt_components = mt_refs.
rt_components = mt_refs_comp.
ENDMETHOD.

METHOD get_component_type.
Expand Down
62 changes: 62 additions & 0 deletions src/rtti/cl_abap_structdescr.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ CLASS ltcl_test DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT FINAL.
METHODS get_included_view2 FOR TESTING RAISING cx_static_check.
METHODS get_included_view3 FOR TESTING RAISING cx_static_check.
METHODS length FOR TESTING RAISING cx_static_check.
METHODS get_components_include FOR TESTING RAISING cx_static_check.
METHODS get_symbols FOR TESTING RAISING cx_static_check.

ENDCLASS.

Expand Down Expand Up @@ -355,4 +357,64 @@ CLASS ltcl_test IMPLEMENTATION.

ENDMETHOD.

METHOD get_components_include.

TYPES: BEGIN OF ty_struc,
b TYPE c LENGTH 1,
END OF ty_struc.

TYPES BEGIN OF ty_struc_root.
INCLUDE TYPE ty_struc AS nest RENAMING WITH SUFFIX _n.
TYPES:
a TYPE c LENGTH 1,
END OF ty_struc_root.

DATA ls_data TYPE ty_struc_root.
DATA lo_struct TYPE REF TO cl_abap_structdescr.
DATA lt_components TYPE cl_abap_structdescr=>component_table.

lo_struct ?= cl_abap_structdescr=>describe_by_data( ls_data ).
lt_components = lo_struct->get_components( ).

cl_abap_unit_assert=>assert_equals(
exp = 2
act = lines( lt_components ) ).

ENDMETHOD.

METHOD get_symbols.

TYPES: BEGIN OF ty_struc,
b TYPE c LENGTH 1,
END OF ty_struc.

TYPES BEGIN OF ty_struc_root.
INCLUDE TYPE ty_struc AS nest RENAMING WITH SUFFIX _n.
TYPES:
a TYPE c LENGTH 1,
END OF ty_struc_root.

DATA ls_data TYPE ty_struc_root.
DATA lo_struct TYPE REF TO cl_abap_structdescr.
DATA lt_symbols TYPE cl_abap_structdescr=>symbol_table.

lo_struct ?= cl_abap_structdescr=>describe_by_data( ls_data ).
lt_symbols = lo_struct->get_symbols( ).

cl_abap_unit_assert=>assert_equals(
exp = 3
act = lines( lt_symbols ) ).

READ TABLE lt_symbols WITH KEY name = 'A' TRANSPORTING NO FIELDS.
cl_abap_unit_assert=>assert_subrc(
exp = 0
act = sy-subrc ).

READ TABLE lt_symbols WITH KEY name = 'B_N' TRANSPORTING NO FIELDS.
cl_abap_unit_assert=>assert_subrc(
exp = 0
act = sy-subrc ).

ENDMETHOD.

ENDCLASS.