Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Attributes of Data Objects →  RTTS - Run Time Type Services 

Ascertaining Data Types

This example demonstrates how the attributes of data types can be determined at runtime.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: dref1 TYPE REF TO data,
          dref2 TYPE REF TO data.

    FIELD-SYMBOLS: <data1> TYPE ANY,
                   <data2> TYPE ANY.

    DATA: descr_ref1 TYPE REF TO cl_abap_typedescr,
          descr_ref2 TYPE REF TO cl_abap_typedescr,
          mess       TYPE string.

    TRY.
        CREATE DATA: dref1 TYPE (type1),
                     dref2 TYPE (type2).

        ASSIGN: dref1->* TO <data1>,
                dref2->* TO <data2>.

      CATCH cx_sy_create_data_error.
        MESSAGE 'Create data error!' TYPE 'I' DISPLAY LIKE 'E'.
        LEAVE PROGRAM.
    ENDTRY.

    descr_ref1 = cl_abap_typedescr=>describe_by_data( <data1> ).
    descr_ref2 = cl_abap_typedescr=>describe_by_data( <data2> ).

    TRY.
        IF descr_ref1 <> descr_ref2.
          RAISE EXCEPTION TYPE conv_exc.
        ELSE.
          <data2> = <data1>.
        ENDIF.
      CATCH conv_exc.
        mess = `Assignment from type `    &&
               descr_ref2->absolute_name  &&
               ` to `                    &&
                descr_ref1->absolute_name &&
                ` not allowed!`.
        MESSAGE mess TYPE 'I' DISPLAY LIKE 'E'.
    ENDTRY.

Description

This example expands on the example for DESCRIBE FIELD, on the correct way to check any data type.

When you enter complex types, such as SCARR and SPFLI, into the input fields, the RTTI determines the absolute type name.