ABAP - Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Assignments →  Assigning Structure Components → 

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 of the components in the structure or row structure. The value 0 stands for the top level.
Mapping type. The possible values are:
Component of the source structure.
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

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.

Notes

dest = CORRESPONDING  #( struct|itab MAPPING ... EXCEPT ... ).
Here, the mapping rule is specified dynamically, however, as the content of a special internal table.

Example

Uses the class CL_ABAP_CORRESPONDING for assignments of components to a simple structure. The mapping rule dictates that the components a3 are assigned to b1 and a1 to b3. The component a2 is ignored since there are no identically named components in the target structure and b2 keeps its value. a4 and a5 in the target structure also keep their values, however, even though the source structure contains identically named components. This is because the value of CL_ABAP_CORRESPONDING=>MAPPING_EXCEPT_ALL is specified for the mapping type for all non-specified components. The executable example for simple structures enables interactive input of the component names that are mapped to each other.

DATA:
  BEGIN OF struct1,
    a1 TYPE string VALUE 'a1',
    a2 TYPE string VALUE 'a2',
    a3 TYPE string VALUE 'a3',
    a4 TYPE string VALUE 'a4',
    a5 TYPE string VALUE 'a5',
  END OF struct1,
  BEGIN OF struct2,
    b1 TYPE string VALUE 'b1',
    b2 TYPE string VALUE 'b2',
    b3 TYPE string VALUE 'b3',
    a4 TYPE string VALUE 'b4',
    a5 TYPE string VALUE 'b5',
  END OF struct2.

DATA(mapper) =
  cl_abap_corresponding=>create(
    source            = struct1
    destination       = struct2
    mapping           = VALUE cl_abap_corresponding=>mapping_table(
      ( level = 0 kind = 1 srcname = 'a1' dstname = 'b3' )
      ( level = 0 kind = 1 srcname = 'a3' dstname = 'b1' )
      ( level = 0 kind = 3 ) ) ).

mapper->execute( EXPORTING source      = struct1
                 CHANGING  destination = struct2 ).

cl_demo_output=>display( struct2 ).

Further Examples