ABAP新语法CL_ABAP_CORRESPONDING

简介

系统提供的标准工具类,用于实现结构和内表之间的动态赋值操作,可以实现简单的赋值,默认值,lookup等功能,基本覆盖了move-corresponding和corresponding的功能

示例

简单赋值
参考F1关键字文档,可实现旧版corresponding的绝大部分功能,包括结构,内表,嵌套结构的赋值

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 = cl_abap_corresponding=>mapping_component srcname = 'a1' dstname = 'b3' )
      ( level = 0 kind = cl_abap_corresponding=>mapping_component srcname = 'a3' dstname = 'b1' )
      ( level = 0 kind = cl_abap_corresponding=>mapping_except_all ) "同名的a4,a5被排除
      )
      ).

"然后执行匹配操作
mapper->execute(
exporting
  source      = struct1
changing
  destination = struct2 ).

cl_demo_output=>display( struct2 ).

设置默认值

data:
  begin of struct1,
    a1 type string value 'a1',
    a2 type string value 'a2',
    a3 type string value '  ',
    a4 type string value 'a4',
  end of struct1,
  begin of struct2,
    b1 type string value 'b1',
    b2 type string value 'b2',
    b3 type string value 'b3',
    b4 type string value 'b4',
  end of struct2.

data(mapper) =
      cl_abap_corresponding=>create_with_value(
      source            = struct1
      destination       = struct2
      mapping           = value
      cl_abap_corresponding=>mapping_table_value(
          "正常匹配
          ( level = 0 kind = cl_abap_corresponding=>mapping_component  srcname = 'a1' dstname = 'b1' )
          "源结构无匹配字段,设置默认值xx
          ( level = 0 kind = cl_abap_corresponding=>mapping_value    dstname = 'b2' value = ref #( `xxx` ) )
          "源结构字段值为initial,故默认值设置起效,目标字段修改为yyy
          ( level = 0 kind = cl_abap_corresponding=>mapping_default_value srcname = 'a3' dstname = 'b3' value = ref #( `yyy` ) )
          "源结构字段有值,故默认值设置未起效,目标字段无修改,结果为a4
          ( level = 0 kind = cl_abap_corresponding=>mapping_default_value srcname = 'a4' dstname = 'b4' value = ref #( `zzz` ) )
        )
      ).

mapper->execute(
exporting
  source      = struct1
changing
  destination = struct2 ).

cl_demo_output=>display( struct2 ).

lookup表

types:
  begin of line1,
    value   type i,
    comment2 type string,
  end of line1,
BEGIN OF line2,
  VALUE   TYPE I,
  COMMENT2 TYPE string,
END OF line2,
  itab1 type standard table of line1 with empty key,
  itab2 type hashed table of line2 with unique key value.

data(itab1) = value itab1( for i = 1 until i >= 10 ( value = i ) ).
data(itab2) = value itab2(
      ( value = 2 comment2 = `...` )
      ( value = 3 comment2 = `...` )
      ( value = 5 comment2 = `...` )
      ( value = 8 comment2 = `...` ) ).

data(mapping_tab) = value cl_abap_corresponding=>mapping_table(
"主键查询
( level = 0 kind = cl_abap_corresponding=>using_key srcname = 'PRIMARY_KEY' )
"指定主键字段
( level = 0 kind = cl_abap_corresponding=>mapping_using_component srcname = 'VALUE' dstname = 'VALUE' )
"指定匹配规则
( level = 0 kind = cl_abap_corresponding=>mapping_component srcname = 'comment2' dstname = 'comment1' )
).

"将Itab2与itab1按value字段进行匹配,然后将itab2中的comment2赋值给itab1中的comment1
cl_abap_corresponding=>create_using(
destination       = itab1
using             = itab2
      mapping               = mapping_tab
)->execute_using( exporting using       = itab2
changing  destination = itab1 ).

cl_demo_output=>display( itab1 ).

备注

本人才疏学浅,如有错误之处麻烦指出
也可添加QQ 2212332116交流

猜你喜欢

转载自blog.csdn.net/u012232542/article/details/105501822