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

Example: Declaring Types Dynamically



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

PostPosted: Tue Sep 11, 2007 9:07 pm    Post subject: Example: Declaring Types Dynamically Reply with quote

 Since the introduction of ABAP Objects, there is now a system called the RTTI concept (Run Time Type Information) that you can use to find out type attributes at runtime. It is based on system classes. The concept includes all ABAP types, and so covers all of the functions of the statements DESCRIBE FIELD and DESCRIBE TABLE.
 There is a description class for each type with special attributes for special type attributes. The class hierarchy of the description classes corresponds to the hierarchy of the types in the ABAP type system. In addition, the description classes for complex types, references, classes, and interfaces have special methods used to specify references to sub-types. Using these methods, you can navigate through a compound type to all its sub-types.
 To obtain a reference to a description object of a type, you must use the static methods of the class CL_ABAP_TYPEDESCR or the navigation methods of the special description class. The description objects are then created from one of the subclasses. At runtime, exactly one description object exists for each type. The attributes of the description object contain information on the attributes of the type.

Code:

DATA:
  descr_ref TYPE REF TO cl_abap_structdescr,
  wa_comp TYPE abap_compdescr.

FIELD-SYMBOLS:
  <fs_wa> TYPE ANY.
...

START-OF-SELECTION.
...
* get reference to type description object by widening cast
  descr_ref ?= cl_abap_typedescr=>describy_by_data ( <fs_wa> ).
...
TOP-OF-PAGE.
  LOOP AT descr_ref->components INTO wa_comp.
    WRITE wa_comp-name.
  ENDLOOP.


 We can now enhance the example of dynamic type declarations so that the system also displays the column names of the transparent table in the list.
 Since we need the attributes of a structure, we first define a reference to the appropriate description class. Instances of this class possess a COMPONENTS attribute, which you use to describe the individual components of the relevant structure. This attribute is an internal table. Therefore you also need to define a work area with a compatible line type.
 The (functional) method call returns the reference to the description instance of the structure. (The system creates the structure dynamically, which is why it is accessed through a field symbol).
Only the abstract class CL_ABAP_TYPEDESCR contains the method DESCRIBE_BY_DATA. Its RETURNING parameter is typed as a reference to this superclass. However, since the actual parameter descr_ref has the type of the subclass CL_ABAP_STRUCTDESCR, we need to assign the object using a (widening) cast.
 You can then access the attributes of the description instance in any form. In this example, the component names are displayed as the column headers. (We have omitted the formatting options for the sake of clarity.)
 For more information and syntax examples, refer to the online documentation, either under the keyword RTTI or the class CL_ABAP_TYPEDESCR.

Example 2:

Code:
 
DATA:
    lcl_table_descr TYPE REF TO cl_abap_tabledescr,
    lcl_struc_descr TYPE REF TO cl_abap_structdescr
  .
  FIELD-SYMBOLS:
    <lt_output> TYPE STANDARD TABLE
  .
  ASSIGN me->output_tab->* TO <lt_output>.
  lcl_table_descr ?= cl_abap_typedescr=>describe_by_data( <lt_output> ).
  lcl_struc_descr ?= lcl_table_descr->get_table_line_type( ).
  it_components = lcl_struc_descr->components.
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.