Start of Content Area

Determining the Attributes of Data Objects  Locate the document in its SAP Library structure

You may sometimes need to find out the attributes of a data object during runtime that were not statically available. For example, you may need to find out the type of a generic interface parameter in a subroutine. To do this, you would use the statement:

DESCRIBE FIELD dobj [LENGTH len] [TYPE type [COMPONENTS n]]
                    [OUTPUT-LENGTH out] [DECIMALS dec]
                    [EDIT MASK mask] [HELP-ID help].

The attributes of the data object dobj specified by the parameters of the statement are written to the variables following the parameters. You can use any number of the additions in the same statement.

DESCRIBE FIELD DISTANCE BETWEEN dobj1 AND dobj2 INTO dec.

This statement returns the distance between the data objects dobj1und dobj2.

For a description of the syntax of the individual additions see the keyword documentation.

ExampleHere is an example using the statement DESCRIBE FIELD.
REPORT ...

 

CLASS conv_exc DEFINITION INHERITING FROM cx_static_check.

ENDCLASS.

 

PARAMETERS:  type1 TYPE c LENGTH 30,

             type2 TYPE c LENGTH 30.

 

DATA: dref1 TYPE REF TO data,

      dref2 TYPE REF TO data.

 

FIELD-SYMBOLS: <data1> TYPE ANY,

               <data2> TYPE ANY.

 

DATA: tdescr1 TYPE c LENGTH 1,

      tdescr2 TYPE c LENGTH 1,

      mess    TYPE string.

 

START-OF-SELECTION.

 

 

  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.

 

  ...

 

  DESCRIBE FIELD: <data1> TYPE tdescr1,

                  <data2> TYPE tdescr2.

  TRY.

      IF tdescr1 <> tdescr2.

        RAISE EXCEPTION TYPE conv_exc.

      ELSE.

        <data2> = <data1>.

      ENDIF.

    CATCH conv_exc.

      CONCATENATE `Assignment from type ` tdescr2 ` to ` tdescr1 ` not allowed!` INTO mess.

      MESSAGE mess TYPE 'I' DISPLAY LIKE 'E'.

Run Time Type Identification

Abbreviation RTTI. Determining data types during program run time.

For more information, see keyword documentation.

ExampleHere is an example of RTTI. Check whether two data types are identical.
REPORT ...

 

CLASS conv_exc DEFINITION INHERITING FROM cx_static_check.

ENDCLASS.

 

PARAMETERS:  type1 TYPE c LENGTH 30,

             type2 TYPE c LENGTH 30.

 

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.

 

START-OF-SELECTION.

 

  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_ref1 = cl_abap_typedescr=>describe_by_data( <data1> ).

 

  TRY.

      IF descr_ref1 <> descr_ref2.

        RAISE EXCEPTION TYPE conv_exc.

      ELSE.

        <data2> = <data1>.

      ENDIF.

    CATCH conv_exc.

      CONCATENATE `Assignment from type `
                  descr_ref2->absolute_name
                  ` to `
                  descr_ref1->absolute_name
                  ` not allowed!`
         INTO mess.

      MESSAGE mess TYPE 'I' DISPLAY LIKE 'E'.

  ENDTRY.

 

ExampleExamples of RTTI and Objects
REPORT ...

 

CLASS conv_exc DEFINITION INHERITING FROM cx_static_check.

ENDCLASS.

 

PARAMETERS:  otype1 TYPE c LENGTH 30,

             otype2 TYPE c LENGTH 30.

 

DATA: oref1 TYPE REF TO object,

      oref2 TYPE REF TO object.

 

 

DATA: descr_ref1 TYPE REF TO cl_abap_typedescr,

      descr_ref2 TYPE REF TO cl_abap_typedescr,

      mess    TYPE string.

 

START-OF-SELECTION.

 

  TRY.

      CREATE OBJECT: oref1 TYPE (otype1),

                     oref2 TYPE (otype2).

 

    CATCH cx_sy_create_object_error.

      MESSAGE 'Create object error!' TYPE 'I' DISPLAY LIKE 'E'.

        LEAVE PROGRAM.

    CATCH cx_root.

      MESSAGE 'Other error!' TYPE 'I' DISPLAY LIKE 'E'.

        LEAVE PROGRAM.

  ENDTRY.

 

  ...

 

  descr_ref1 =
    cl_abap_typedescr=>DESCRIBE_BY_OBJECT_REF( oref1 ).

  descr_ref2 =
    cl_abap_typedescr=>DESCRIBE_BY_OBJECT_REF( oref2 ).

 

  TRY.

      IF descr_ref1 <> descr_ref2.

        RAISE EXCEPTION TYPE conv_exc.

      ELSE.

        oref1 = oref2.

      ENDIF.

    CATCH conv_exc.

      CONCATENATE `Assignment from type `
                  descr_ref2->absolute_name
                  ` to `
                  descr_ref1->absolute_name
                  ` not allowed!`
         INTO mess.

      MESSAGE mess TYPE 'I' DISPLAY LIKE 'E'.

  ENDTRY.

 

 

 

End of Content Area