본문 바로가기
ABAP/Function|Class

[Class] CL_ABAP_CORRESPONDING - Dynamic Corresponding

by name_text 2023. 10. 30.

CL_ABAP_CORRESPONDING

Dynamic Corresponding

 

Create Method의 Mapping 파라미터를 이용하여 Source > Destination으로 Corresponding시 서로 다른 필드끼리 값을 복사 할 수 있습니다.

{Destination} = CORRESPONDING #( {Source} MAPPING col1 = col2 } ) 와 유사하며, MAPPING 규칙을 좀더 세부적으로 제어할 수 있다고 생각하면 됩니다.

 

아래 내용은 ABAP 7.50 버전 기준의 사용 방법입니다.

https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/index.htm?file=abencl_abap_corresponding.htm

CL_ABAP_CORRESPONDING - System Class

The system class CL_ABAP_CORRESPONDING enables assignments of components between structures or between internal tables with dynamically specified mapping rules. The factory method CREATE of a class must be used to create a mapping object before the class can be used:

DATA(mapper) = cl_abap_corresponding=>create( source      = struct|itab
                                              destination = struct|itab
                                              mapping     = mapping_tab ).

Structures struct or internal tables itab of the assigned data types must be passed to the parameters source and destination. An internal table of the type CL_ABAP_CORRESPONDING=>MAPPING_TABLE, containing the mapping rule, must be passed to the parameter mapping. If an initial mapping table is passed, only the identically named components are assigned. The mapping table has the following components:

  • LEVEL

Level of the components in the structure or row structure. The value 0 stands for the top level.

  • KIND

Mapping type. The possible values are:

  • CL_ABAP_CORRESPONDING=>MAPPING_COMPONENT (1) (The components specified in this row are mapped to each other.)
  • CL_ABAP_CORRESPONDING=>MAPPING_EXCEPT_COMPONENT (2) (The component of the source structure specified in this row is excluded from the mapping of identically named components.)
  • CL_ABAP_CORRESPONDING=>MAPPING_EXCEPT_ALL (3) (All components of the current source structure are excluded from the mapping of identically name components.)
  • SRCNAME

Component of the source structure.

  • DSTNAME

Component of the target (destination) structure.

The rows of the internal table must be constructed so that they produce a mapping rule in the correct order. Components of the source structure for which no mapping is defined and that were not excluded are assigned to identically named components of the target structure.

The method EXECUTE of a mapping object can be used to perform any number of assignments between structures or internal tables src and dst whose data type matches the source type or target type specified when the object was created:

mapper->execute( EXPORTING source      = src
                 CHANGING  destination = dst ).

The assignment is performed component by component

  • between the components specified in the mapping rule
  • between the remaining identically named components at the same level (if not excluded in the mapping rule).

In assignments between structures, components of the target structure to which no components of the source structure are assigned keep their previous value, like the statement MOVE-CORRESPONDING but unlike the operator CORRESPONDING without the addition BASE. Any nested internal tables are always resolved, as when the addition EXPANDING NESTED TABLES is specified in MOVE-CORRESPONDING or the addition DEEP for the operator CORRESPONDING. In assignments between internal tables, the target table is always initialized first. There is no matching addition for the addition KEEPING TARGET LINES in MOVE-CORRESPONDING or BASE in CORRESPONDING.

Source and target must not be the same otherwise the runtime error CORRESPONDING_SELF can occur. Incorrect parameters passed to the methods of the class CL_ABAP_CORRESPONDING raised exceptions of the class CX_CORR_DYN_ERROR.

 

 # 사용예시

"Dynamic Corresponding
TYPES: BEGIN OF LINE1,
         COL1 TYPE I,
         COL2 TYPE I,
       END OF LINE1,
       BEGIN OF LINE2,
         COL2 TYPE I,
         COL3 TYPE I,
       END OF LINE2.

DATA: ITAB1 TYPE TABLE OF LINE1 WITH EMPTY KEY,
      ITAB2 TYPE TABLE OF LINE2 WITH EMPTY KEY.

DATA(OUT) = CL_DEMO_OUTPUT=>NEW( ).

ITAB1 = VALUE #(
  ( COL1 = 11 COL2 = 12 )
  ( COL1 = 21 COL2 = 22 ) ).

ITAB2 = VALUE #(
  ( COL2 = 212 COL3 = 312 )
  ( COL2 = 222 COL3 = 322 ) ).

OUT->NEXT_SECTION( 'Original Data' )->WRITE( ITAB1 )->WRITE( ITAB2 ).

CL_ABAP_CORRESPONDING=>CREATE(
   SOURCE            = ITAB1
   DESTINATION       = ITAB2
   MAPPING           = VALUE CL_ABAP_CORRESPONDING=>MAPPING_TABLE(  )
   )->EXECUTE( EXPORTING SOURCE      = ITAB1
               CHANGING  DESTINATION = ITAB2 ).
OUT->NEXT_SECTION( 'Corresponding ITAB1 to ITAB2 - No Mapping' )->WRITE( ITAB2 ).

CL_ABAP_CORRESPONDING=>CREATE(
  SOURCE            = ITAB1
  DESTINATION       = ITAB2
  MAPPING           = VALUE CL_ABAP_CORRESPONDING=>MAPPING_TABLE(
   ( LEVEL = 0 KIND = 1 SRCNAME = 'COL1' DSTNAME = 'COL2' )
   ( LEVEL = 0 KIND = 1 SRCNAME = 'COL2' DSTNAME = 'COL3' ) )
  )->EXECUTE( EXPORTING SOURCE      = ITAB1
              CHANGING  DESTINATION = ITAB2 ).

OUT->NEXT_SECTION( 'Corresponding ITAB1 to ITAB2 - Mapping'
                  )->WRITE( 'COL1 -> COL2 , COL2 -> COL3 ' )->WRITE( ITAB2 ).

OUT->DISPLAY( ).

 

댓글