Skip to end of metadata
Go to start of metadata
REPORT z_anon_itab_writer
  NO STANDARD PAGE HEADING
  LINE-SIZE 1023.

*----------------------------------------------------------------------*
* Purpose:                                                             *
* This Report contains a local Class (LCL_ANON_ITAB_WRITER), which is  *
* able to write the Content of any flat internal Table to Standard     *
* List Output. To achive this, ABAP´s new Run Time Type Interface      *
* (RTTI) is used to handle the flat Structure of the internal Table    *
* dynamically without statically knowing the it.                       *
*----------------------------------------------------------------------*
* Author:         Mike Schernbeck                                      *
*                 mike.schernbeck@cubeserv.com                         *
* Date:           31/01/2008                                           *
*----------------------------------------------------------------------*

************************************************************************
* CLASS lcl_anon_itab_writer DEFINITION                                *
************************************************************************
* Definition of local Class LCL_ANON_ITAB_WRITER                       *
************************************************************************
CLASS lcl_anon_itab_writer DEFINITION.
  PUBLIC SECTION.
    TYPE-POOLS:
      abap.
    METHODS:
      constructor
        IMPORTING
          it_data       TYPE ANY TABLE,
      write.

  PROTECTED SECTION.
  PRIVATE SECTION.
    DATA:
*     structure of internal table to be written to STDOUT
      _r_structdescr    TYPE REF TO cl_abap_structdescr,

*     reference to internal table, that should be written
      _r_data           TYPE REF TO data.
ENDCLASS. "CLASS lcl_anon_itab_writer DEFINITION

************************************************************************
* CLASS lcl_anon_itab_writer IMPLEMENTATION                            *
************************************************************************
* Implementation of local Class LCL_ANON_ITAB_WRITER                   *
************************************************************************
CLASS lcl_anon_itab_writer IMPLEMENTATION.
* **********************************************************************
* * METHOD constructor                                                 *
* **********************************************************************
* * This Methods determines the Structure of the internal Table passed *
* * to IT_DATA as a RTTI-Descriptor (ME->_R_STRUCTDESCR) and stores a  *
* * Reference to the internal Table passed (ME->_R_DATA).              *
* **********************************************************************
  METHOD constructor.
    DATA:
      lr_tabledescr TYPE REF TO cl_abap_tabledescr.


    lr_tabledescr ?= cl_abap_tabledescr=>describe_by_data( it_data ).

    IF NOT lr_tabledescr IS INITIAL.
      me->_r_structdescr ?= lr_tabledescr->get_table_line_type( ).
      GET REFERENCE OF it_data INTO me->_r_data.
    ELSE. "IF NOT lr_tabledescr IS INITIAL
    ENDIF. "IF NOT lr_tabledescr IS INITIAL
  ENDMETHOD. "METHOD constructor

* **********************************************************************
* * METHOD write                                                       *
* **********************************************************************
* * Write Content of internal Table ME->_R_DATA->* to Standard List    *
* * Output.                                                            *
* **********************************************************************
  METHOD write.
    DATA:
      lr_datadescr    TYPE REF TO cl_abap_datadescr,
      lx_new_row      TYPE boolean.    FIELD-SYMBOLS:
      <lv_data>       TYPE ANY,
      <lw_component>  TYPE LINE OF abap_compdescr_tab,
      <lt_data>       TYPE ANY TABLE,
      <lw_data>       TYPE ANY.


    ASSIGN me->_r_data->* TO <lt_data>.

    LOOP AT <lt_data> ASSIGNING <lw_data>.
      LOOP AT me->_r_structdescr->components ASSIGNING <lw_component>.
        IF sy-tabix EQ 1.
          MOVE abap_true TO lx_new_row.
        ELSE. "IF sy-tabix EQ 1
          MOVE abap_false TO lx_new_row.
        ENDIF. "IF sy-tabix EQ 1

        lr_datadescr = me->_r_structdescr->get_component_type(
                                                  <lw_component>-name ).
        ASSIGN COMPONENT <lw_component>-name OF STRUCTURE <lw_data>
          TO <lv_data> CASTING TYPE HANDLE lr_datadescr.

        IF sy-subrc EQ 0.
          IF lx_new_row EQ abap_true.
            WRITE: / <lv_data>.
          ELSE. "IF lx_new_row EQ abap_true
            WRITE: <lv_data>.
          ENDIF. "IF lx_new_row EQ abap_true
        ELSE. "IF sy-subrc EQ 0
        ENDIF. "IF sy-subrc EQ 0
      ENDLOOP. "LOOP AT me->_r_structdescr->components ASSIGNING <lw_component>
    ENDLOOP. "LOOP AT <lt_data> ASSIGNING <lw_data>
  ENDMETHOD. "METHOD write
ENDCLASS. "CLASS lcl_anon_itab_writer IMPLEMENTATION



 * DATA *****************************************************************
DATA:
* internal table that should be written as list
  gt_tadir        TYPE STANDARD TABLE OF tadir,

* object reference to Anonymous Internal Table Writer
  gr_itab_writer  TYPE REF TO lcl_anon_itab_writer.



 * START-OF-SELECTION ***************************************************
START-OF-SELECTION.
* create instance of Anonymous Internal Table Writer -> GR_ITAB_WRITER
  CREATE OBJECT gr_itab_writer
    EXPORTING
      it_data = gt_tadir. * fill internal table GT_TADIR
  SELECT *
    FROM tadir
    INTO TABLE gt_tadir
    WHERE author EQ sy-uname. * write internal table GT_TADIR as list
  gr_itab_writer->write( ).