SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Determining the Attributes of Data Objects



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Dynamic Programming | Динамическое программирование
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Tue Sep 11, 2007 8:43 pm    Post subject: Determining the Attributes of Data Objects Reply with quote

Determining the Attributes of Data Objects
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.

Here is an example using the statement DESCRIBE FIELD.
Code:
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.

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

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


Examples of RTTI and Objects

Code:
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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Dynamic Programming | Динамическое программирование All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.