1

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.
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Cutter
  • 1,673
  • 7
  • 27
  • 43
  • 1
    Just from looking at the code I would guess the problem is a combination with your index being of type string, the concatenation and the second condition in the if 'comp-name+off(*)'. Concatenate help: If the target field result has a fixed length and this is greater than the length required, the field is filled on the right with blanks or hexadecimal 0. – futu May 27 '20 at 20:57
  • If I try to concatenate the index in integer form I get an error message about type incompatibility, hence the explicit cast to string (and you're right that's what causes the bug). – Cutter May 27 '20 at 21:01
  • Better use String Templates with implicit conversion instead of CONCATENATE: wa_comp-name = |COL_{ sy-index }|. – futu May 27 '20 at 21:07

1 Answers1

1

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.

Cutter
  • 1,673
  • 7
  • 27
  • 43
  • 1
    The "invisible character" is in fact just a trailing space, it's just you who didn't pay attention to it, which corresponds to the sign, space being for the positive numbers (see [implicit conversion target I <-- source STRING](https://help.sap.com/doc/abapdocu_753_index_htm/7.53/en-US/abenconversion_type_ibs.htm#@@ITOC@@ABENCONVERSION_TYPE_IBS_2) - "The character "-" is set in the last place for a negative value and a blank is set in the last place for a positive value."). Another solution is to use a string template without changing anything else: `c_index = |{ sy-index }|.`. – Sandra Rossi May 28 '20 at 07:08
  • It is not displayed in the value field of that variable, in the debugger. You have to check the length and hexadecimal value to notice it. – Cutter May 28 '20 at 10:13
  • Templates aren't supported on my system unfortunately. – Cutter May 28 '20 at 10:15