简体   繁体   English

使用 RTTS 创建类型时 CX_SY_STRUCT_COMP_NAME=>illegal_char_in_name

[英]CX_SY_STRUCT_COMP_NAME=>illegal_char_in_name while creating a type with RTTS

I'm trying to build a dynamic type using RTTS classes.我正在尝试使用 RTTS 类构建动态类型。 I've build a component table containing column names 'COL_1' , 'COL_2' and so on... The type creation fails inside the standard method CL_ABAP_STRUCTDESCR=>CREATE( ) on line 73:我已经构建了一个包含列名'COL_1''COL_2'等等的组件表......第 73 行的标准方法CL_ABAP_STRUCTDESCR=>CREATE( )中的类型创建失败:

       if comp-name+off(1) cn 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_' or
          comp-name+off(*) cn 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789'.
*        illegal character in component name
         raise exception type CX_SY_STRUCT_COMP_NAME
           exporting textid = CX_SY_STRUCT_COMP_NAME=>illegal_char_in_name
                     component_name = comp-name
                     component_number = comp_no.

The post-mortem value of comp-name is COL_1 . comp-name的验尸值是COL_1 As you see the characters are valid.如您所见,字符是有效的。 I don't understand how the IF condition can be true here.我不明白 IF 条件如何在这里成立。

I've tested the validity of the column name in my own module before calling this method in the exact same way and the IF condition returns FALSE there.在以完全相同的方式调用此方法之前,我已经在我自己的模块中测试了列名的有效性,并且 IF 条件在那里返回 FALSE。

Minimal code to reproduce this bug:重现此错误的最少代码:

  DATA: ty_output       TYPE REF TO CL_ABAP_STRUCTDESCR,
        it_output       TYPE REF TO DATA,    
        wa_comp         TYPE cl_abap_structdescr=>component,
        it_comp         TYPE cl_abap_structdescr=>component_table,
        c_index         TYPE string.

  DO 7 TIMES.
    c_index = sy-index.
    CONCATENATE 'COL_' c_index INTO wa_comp-name.
    IF wa_comp-name(*) cn 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890'.
      WRITE 'NO'. " <= This branch is entered.
    ENDIF.
    IF 'COL_1' cn 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890'.
      WRITE 'NO'. " <= This branch is NOT entered.
    ENDIF.
    wa_comp-type = CL_ABAP_ELEMDESCR=>GET_STRING( ).
    APPEND wa_comp TO it_comp.
  ENDDO.

  ty_output = cl_abap_structdescr=>create( it_comp ).
  CREATE DATA it_output TYPE HANDLE ty_output.

This was caused by the cast of sy-index (integer) to c_index (string) adding an invisible character at the end of wa_comp-name .这是由于将sy-index (整数)强制转换为c_index (字符串),在wa_comp-name的末尾添加了一个不可见字符。

Casting sy-index to a variable of type n instead of string solved the problem.sy-index转换为n类型的变量而不是string解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM