5 1 1 1 1 1 Rating 100% (6 Votes)
Please be aware that accessing private or protected data may have unpredictible consequences! Use it at your own risk.
 
From time to time, it happens that you want to access private data or methods from CL_GUI_ALV_GRID, which is normally not possible.
 
When you look into the class definition you'll notice that there is only one global friend - interface IF_ALV_RM_GRID_FRIEND which has no attributes or methods declared. Quite strange, isn't it?
 
 
When you'll search were this interface is used then you'll notice that it's helping to get access to private grid data. So why not to use it the same way?
 
To be honest, I'm not the fan of playing with private methods, but sometimes the designer haven't foreseen that the method or attribute will be needed to use also outside the class and then you have to look for a work around. And while SAP is doing the same then I don't feel guilty :-)
 
So what have to be done? You need to add interface IF_ALV_RM_GRID_FRIEND into the class where you want to use private methods of CL_GUI_ALV_GRID. Once you've done that and you have inside an attribute of type CL_GUI_ALV_GRID then you can access its private and protected elements.
 
If you inherit CL_GUI_ALV_GRID in your class, then you should cast your instance to an attribute of type CL_GUI_ALV_GRID and then using this attribute you'll be able to access those elements. If you'll try to access them directly in such class ( for example me->create_ex_result( ).  ) then you'll receive syntax error.
 
Here is an example of local class which can have access to grid private and protected data:

class lcl_alv_private definition
  public
  final
  create public .

  public section.

    interfaces if_alv_rm_grid_friend .

    methods constructor
      importing
        !io_grid type ref to cl_gui_alv_grid .
    methods create_ex_result
      returning
        value(rv_resultstype ref to cl_salv_ex_result_data_table .

  protected section.
  private section.

    data grid type ref to cl_gui_alv_grid .

endclass.


class lcl_alv_private implementation.

  method constructor.
    grid io_grid.
  endmethod.

  method create_ex_result.
    rv_results =  me->grid->create_ex_result).
  endmethod.

endclass.

 
Although the solution is provided, please use it wisely.
 
Cheers
Łukasz