Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Help for RTTS

former_member207873
Participant
0 Likes
1,195

Hello Experts,

I am trying to learn RTTS. I am understanding certain concepts but still there are areas which I need more clarification. The below given link has been advised by many experts to learn RTTS. http://scn.sap.com/thread/1725739. Can anyone explain the correct answer mentioned in this thread.

Regards,

Marina.

9 REPLIES 9
Read only

former_member209120
Active Contributor
0 Likes
1,149
Read only

Former Member
0 Likes
1,149

Hi Marina,

RTTC (run-time type creation) definitely is not very handy in ABAP.

I have a commented example for you that shall illustrate the single steps necessary to create and use a fully dynamic structure.

So here we go:

   " this is a fully dynamic (RTTC) version to create the following code:

   " DATA:

   "   BEGIN OF ls_dynamic_struct,

   "     material TYPE matnr,

   "     mattext TYPE maktx,

   "   END OF ls_dynamic_struct.

   "

   " ls_dynamic_struct-material = '4711-0815'.

   " ls_dynamic_struct-mattext  = 'Dynamic Structure Demonstration Material'.

   "

   " WRITE: ls_dynamic_struct.

   DATA:

     gs_component      TYPE cl_abap_structdescr=>component,       " type inside class CL_ABAP_STRUCTDESCR

     gt_compontents    TYPE cl_abap_structdescr=>component_table, " type inside class CL_ABAP_STRUCTDESCR

     lo_dynamic_struct TYPE REF TO cl_abap_structdescr,           " the structure's type definition

     ls_dynamic_struct TYPE REF TO data.                          " the structure (created via CREATE DATA)

   FIELD-SYMBOLS:

     <dynamic_struct>  TYPE any, " associated with the dynamically created structure

     <struct_material> TYPE any, " associated with the structure's MATERIAL component

     <struct_mattext>  TYPE any. " associated with the structure's MATTEXT component

   " define the structure's first component MATERIAL

   gs_component-name = 'MATERIAL'.

   " create a type description for ABAP dictionary type MATNR (material number) - casting required

   gs_component-type ?= cl_abap_elemdescr=>describe_by_name( 'MATNR' ).

   APPEND gs_component TO gt_compontents.

   " define the structure's second component MATTEXT

   gs_component-name = 'MATTEXT'.

   " create a type description for ABAP dictionary type MAKTX (material short text) - casting required

   gs_component-type ?= cl_abap_elemdescr=>describe_by_name( 'MAKTX' ).

   APPEND gs_component TO gt_compontents.

   " create structure's type definition from the component table

   lo_dynamic_struct = cl_abap_structdescr=>create( gt_compontents ).

   " now create memory structure to actually store the data

   CREATE DATA:

     ls_dynamic_struct TYPE HANDLE lo_dynamic_struct.

   " assign field symbols to structure and all of its components

   " this is necessary to be able to access the structure and its components

   " since the classical struct_var-component = value doesn't work for RTTC

   ASSIGN ls_dynamic_struct->* TO <dynamic_struct>.

   ASSIGN COMPONENT 'MATERIAL' OF STRUCTURE <dynamic_struct> TO <struct_material>.

   ASSIGN COMPONENT 'MATTEXT'  OF STRUCTURE <dynamic_struct> TO <struct_mattext>.

   " assign values to both components

   <struct_material> = '4711-0815'.

   <struct_mattext>  = 'Dynamic Structure Demonstration Material'.

   " write structure

   WRITE: <dynamic_struct>.

Though this seems pretty weird it definitely works.

Regards,

  Chris

Read only

0 Likes
1,149

Hi,

I am quite new to ABAP (since 2006) but not to data processing (since 1973).

All my professional career was within manufacturing (Apparel , Chemicals) .

So my questions:

Are you using it a lot ?

What are the business circumstances where RTTC (RunTime Type Creation) is the most cost effective solution ?

Thanks for any hint.

Regards.

Read only

0 Likes
1,149

Hi Eitan,

as you have seen for yourself RTTC is not very handy in ABAP. The code becomes quite confusing for more complex use cases - just imagine to create an internal table that stores a more complex structure (> 10 elements) and everything has to be created via RTTC ...

I'm not using RTTC very often but there is however one use case where I find it particularly useful and beneficially to employ:

Imagine getting a structured record description in a text file like

01     CUSTOMER_ID     CHAR     10

02     CUST_NAME       CHAR     40

03     CUST_ADDR       CHAR     80

04     CUST_ACTIVE     NUM

05     DATA_CREATE     DAT

06     DATA_CHANGE     DAT

along with the real data - in that case you can parse the record description and create a suitable structure via RTTC to process the actual data for instance from a CSV file.

And the good news is that the code will work for every CSV file that comes with such a record description - extremely flexible!


But again - there are only a few circumstances where the (considerable) effort is justified and even then it makes sense to encapsulate some of the steps to create tables and structures via RTTC in a class hierarchy to make the code more expressive and easier to maintain.

Example:

DATA:

  lo_container TYPE REF TO zcl_abap_rttc_container,

  lo_structure TYPE REF TO zcl_abap_struct_component.

lo_container = zcl_abap_rttc_container_factory=>create( ).

lo_structure = lo_container->create_structure( 'LINE_STRUCT' ).

lo_structure->add_element( 'MATERIAL', 'MATNR', 'material number' ).

lo_structure->add_element( 'MATTEXT''MAKTX', 'material short text' ).

Regards,

  Chris

PS: If you consider yourself new to ABAP because you're using it since 2006 then I'm an absolute rookie (using ABAP since 2010)

Read only

0 Likes
1,149

Hi Chris,

Thank you very much for your time and the gracious explanation .

regards .

Read only

0 Likes
1,149

   " define the structure's first component MATERIAL

   gs_component-name = 'MATERIAL'.

   " create a type description for ABAP dictionary type MATNR (material number) - casting required

   gs_component-type ?= cl_abap_elemdescr=>describe_by_name( 'MATNR' ).

   APPEND gs_component TO gt_compontents.

How can we write component name as Material and component type as MATNR when we do not the component name and type before hand.

   " define the structure's second component MATTEXT

   gs_component-name = 'MATTEXT'.

   " create a type description for ABAP dictionary type MAKTX (material short text) - casting required

   gs_component-type ?= cl_abap_elemdescr=>describe_by_name( 'MAKTX' ).

   APPEND gs_component TO gt_compontents.

   " create structure's type definition from the component table

How can we write component name as MATTEXT and component type as MAKTX when we do not the component name and type before hand.

Regards,

Marina.

Read only

0 Likes
1,149

Hi Christian,

Thanks for your reply. But I have some doubts as well. Below I have highlighted them.

   " define the structure's first component MATERIAL

   gs_component-name = 'MATERIAL'.

   " create a type description for ABAP dictionary type MATNR (material number) - casting required

   gs_component-type ?= cl_abap_elemdescr=>describe_by_name( 'MATNR' ).

   APPEND gs_component TO gt_compontents.

How can we write component name as 'MATERIAL' and component type as MATNR when do not know component name and type. Same applies to the below component type as well. We will come to know the components at run time.

   gs_component-name = 'MATTEXT'.

   " create a type description for ABAP dictionary type MAKTX (material short text) - casting required

   gs_component-type ?= cl_abap_elemdescr=>describe_by_name( 'MAKTX' ).

   APPEND gs_component TO gt_compo, ntents.

   " create structure's type definition from the component table

   lo_dynamic_struct = cl_abap_structdescr=>create( gt_compontents ).

   " now create memory structure to actually store the data

Read only

0 Likes
1,149

Hi Marina,

at some point in time you have to know something about the data that you actually want to store through RTTC objects.

For the element's names you can go with generic names like COLUMN1 ... COLUMNn although this somewhat contradicts the concept behind RTTC because if the data is that unspecific you could probably store everything in a STANDARD TABLE OF string or something similar.

If you want to have more general types you can simply achieve this by using the following snippet:

DATA:

     gv_field_type     TYPE char40,

...

gs_component-type ?= cl_abap_elemdescr=>describe_by_data( gv_field_type ).

In that instance the type is derived via RTTI (runtime type identification) from the pre-existing type you provided.


If you rather want it to be maximum generic (like you're getting a formal description of the structure that you want to build up - comparable to an XML schema) you can use this style:

gs_component-type ?= cl_abap_elemdescr=>get_c( 40 ).

This example creates a C(40) type - check out the other static methods the class cl_abap_elemdescr provides to see what's in there (get_i(), get_p(), get_string() etc.).


Regards,

   Chris

Read only

0 Likes
1,149

Hi Marina,

basically there are two prerequisites that have to be met once it comes to object creation via RTTC:

  1. You have to create a name - either prescribed or synthetic from scratch. This sure is the easy part.
  2. You have to devise a type
    1. if you want to reference existing ABAP dictionary objects - you're done
    2. if you want to reference existing variables - you're done as well
    3. if you want to create a type from scratch you need to know two things:
      1. the elementary data type suitable to hold the data
      2. the type's size (if type requires a size) - this can be a rough (and generous!) estimate or can be devised via methods like CL_ABAP_MEMORY_UTILITIES=>GET_MEMORY_SIZE_OF_OBJECT()

If you cannot devise a name and/or a suitable type (that fits into one of the three categories above) then RTTC probably is not the way to go.

Regards,

  Chris