简体   繁体   中英

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. 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:

       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 . As you see the characters are valid. I don't understand how the IF condition can be true here.

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.

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 .

Casting sy-index to a variable of type n instead of string solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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