ABAP RTTS – Get Type of an Attribute from Class

By | Last Updated on November 26, 2013 | 4,273

Code snippet to demonstrate the use of ABAP RTTS Runtime Type Services to get type of an Attribute available in the class.

Preface

RTTS is very powerful for dynamic programming. This code snippet, lets you get to the type of an attribute. This demo is to get the type of an internal table attribute. Also, shows the use case – get the type to create a dynamic table. Once you get the Component names, you can do other tasks like dynamic populating the values, get the specific columns etc.

RTTS using Class Name

 
*
CLASS lcl_data DEFINITION.
  PUBLIC SECTION.
    DATA: t_data TYPE STANDARD TABLE OF t100.
ENDCLASS.                    "LCL_DATA DEFINITION
*
START-OF-SELECTION.
  DATA: lo_main TYPE REF TO lcl_data.
  CREATE OBJECT lo_main.
  SELECT * FROM t100 INTO TABLE lo_main->t_data
    UP TO 10 ROWS .
*
 
  DATA: lo_cls_desc TYPE REF TO cl_abap_classdescr.
  DATA: lo_table    TYPE REF TO cl_abap_tabledescr.
  DATA: lo_struc    TYPE REF TO cl_abap_structdescr.
  DATA: lt_comp     TYPE cl_abap_structdescr=>component_table.
  DATA: ls_comp     LIKE LINE OF lt_comp.
 
  DATA: lo_new_tab  TYPE REF TO cl_abap_tabledescr.
  DATA: lo_dyn_tab  TYPE REF TO data.
  FIELD-SYMBOLS: <f_tab>   TYPE ANY TABLE.
 
  DATA: lv_lines TYPE i.
 
*=======
  lo_cls_desc ?= cl_abap_classdescr=>describe_by_name( p_name = 'LCL_DATA' ).
  lo_table ?= lo_cls_desc->get_attribute_type( 'T_DATA' ).
  lo_struc ?= lo_table->get_table_line_type( ).
  lt_comp = lo_struc->get_components( ).
*=======
 
" Use case - Create dynamic Itab from the type
  lo_new_tab = cl_abap_tabledescr=>create(
                  p_line_type  = lo_struc
                  p_table_kind = cl_abap_tabledescr=>tablekind_std
                  p_unique     = abap_false ).
  CREATE DATA lo_dyn_tab TYPE HANDLE lo_new_tab.
  ASSIGN lo_dyn_tab->* TO <f_tab>.
 
  <f_tab> = lo_main->t_data.
  lv_lines = lines( <f_tab> ).
  WRITE: lv_lines.
 

RTTS using Object Reference

 
  DATA: lo_obj_desc TYPE REF TO cl_abap_objectdescr.
  DATA: lo_table    TYPE REF TO cl_abap_tabledescr.
  DATA: lo_struc    TYPE REF TO cl_abap_structdescr.
  DATA: lt_comp     TYPE cl_abap_structdescr=>component_table.
  DATA: ls_comp     LIKE LINE OF lt_comp.
 
  DATA: lo_new_tab  TYPE REF TO cl_abap_tabledescr.
  DATA: lo_dyn_tab  TYPE REF TO data.
  FIELD-SYMBOLS: <f_tab>   TYPE ANY TABLE.
 
  DATA: lv_lines TYPE i.
 
*=======
  lo_cls_desc ?= cl_abap_classdescr=>describe_by_name( p_name = 'LCL_DATA' ).
  lo_table ?= lo_cls_desc->get_attribute_type( 'T_DATA' ).
  lo_struc ?= lo_table->get_table_line_type( ).
  lt_comp = lo_struc->get_components( ).
*=======
 
" Use case
  lo_new_tab = cl_abap_tabledescr=>create(
                  p_line_type  = lo_struc
                  p_table_kind = cl_abap_tabledescr=>tablekind_std
                  p_unique     = abap_false ).
  CREATE DATA lo_dyn_tab TYPE HANDLE lo_new_tab.
  ASSIGN lo_dyn_tab->* TO <f_tab>.
 
  <f_tab> = lo_main->t_data.
  lv_lines = lines( <f_tab> ).
  WRITE: lv_lines.
 
 

More on RTTS
More on Dynamic ITAB creation

Do you have a Code Snippet which you want to share, Submit Code Snippet here

Share It!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{272 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 272 articles.

Load comments

1 Comment

  • sriram

    perfect reference to follow

Comments on this Post are now closed. If you have something important to share, you can always contact me.