Friday 14 October 2022

Reusing Class Methods Should Be Easier

One of the short comings of ABAP is the ability to find and reuse class methods. It is easy to find functions from within the ABAP editor by clicking the [Pattern] button. But to find a method you first need to know what class it is in.

One way to overcome this is to search the table VSEOCOMPDF from SE16 but that’s a bit awkward, so I have written the below program to search for and display a list of methods based on selection criteria. I have written the code as a report with local classes to make it easily portable.

To implement this you can simply copy and paste the code into a new report in SE38. Here it is called ZFIND_METHOD but you can choose any name that suits you. When you save the code you can make it a local object. There is no need for this program to be transported.

From the SE38 editor screen press [F5]* or click the [Text Symbols] button to go to Text Elements. Click the [Compare Texts Symbols] (Right most on the tool bar) then click [Edit] then [Insert text symbols]. Press [F11] to save then [F3] to return.

After that click the [Selection Texts] tab. Check every [DDIC Reference] and press enter and then press [Ctrl]+[F3] to activate the text.

* Some of the shortcuts mentioned above will not work on older versions of SAP.

SAP ABAP Certification, SAP ABAP Career, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Preparation, SAP ABAP Guides, SAP ABAP Central, SAP ABAP Method, SAP ABAP Certification

Watch out for my next post which will explain how to call this functionality from the ABAP editor and to return the call pattern similar to the ABAP Objects Patterns currently available.

If you find this program useful please let me know by clicking like below or if you feel there is room for improvement please leave a comment.

Have fun in your programming and remember: Keep it simple and keep it neat.

*&--------------------------------------------------------------------&*
*& Report         : ZFIND_METHOD                                       *
*& Description    : This report lists class attributes: methods data   *
*&                  and constants attributes to help find reusable     *
*&                  code.                                              *
*& Author         : Kevin Neale                                        *
*& Date           : 22/09/2022                                         *
*& Change Request : N/A                                                *
*&--------------------------------------------------------------------&*
*&              M O D I F I C A T I O N    L O G                      &*
*&--------------------------------------------------------------------&*
*& Description    :                                                    *
*& Author         :                                                    *
*& Date           :                                                    *
*& Change Request :                                                    *
*&--------------------------------------------------------------------&*
report zfind_method
message-id 0d.

types:
  begin of ty_input,
    clsname             type seoclsname,
    cmpname             type seocmpname,
    cmptype             type seocmptype,
    mtdtype             type seomtdtype,
    mtddecltyp          type seomtddecl,
    attdecltyp          type seoattdecl,
    expose              type seoexpose,
    state               type seostate,
    attvalue            type seovalue,
  end of ty_input,
  ty_input_tab          type standard table of ty_input,
  begin of ty_output,
    clsname             type seoclsname,
    cmpname             type seocmpname,
    cmptype             type val_text,
    mtdtype             type val_text,
    attdecltyp          type val_text,
    expose              type val_text,
    state               type val_text,
    attvalue            type seovalue,
    colour              type lvc_t_scol,
  end of ty_output,
  ty_output_tab         type standard table of ty_output.

constants:
  co_true               type flag                value 'X',
  co_allow_all          type i                   value 3.

data:
  gv_cls                type vseocompdf-clsname,
  gv_cmp                type vseocompdf-cmpname,
  gv_typ                type vseocompdf-cmptype,
  gv_mty                type vseocompdf-mtdtype,
  gv_dec                type vseocompdf-attdecltyp,
  gv_scp                type vseocompdf-exposure,
  gv_ste                type vseocompdf-state.

selection-screen:
  begin of block b1 with frame title text-001.
select-options:
    so_cls for gv_cls,
    so_cmp for gv_cmp,
    so_typ for gv_typ,
    so_mty for gv_mty,
    so_dec for gv_dec,
    so_scp for gv_scp,
    so_ste for gv_ste.
selection-screen:
  end of block b1,
  begin of block b2 with frame title text-002.
parameters:
    pa_vari type slis_vari.
selection-screen:
  end of block b2.

************************************************************************
*    CLASSES                                                           *
************************************************************************
*----------------------------------------------------------------------*
*   CLASS zcl_colour DEFINITION
*----------------------------------------------------------------------*
*   Colour chooser class.
*   This would be better implemented as a static class but is
*   local here for easy portability.
*----------------------------------------------------------------------*
class zcl_colour definition final.
  public section.
    data:
      none              type i                   value 0,
      blue              type i                   value 1,
      grey              type i                   value 2,
      yellow            type i                   value 3,
      darkblue          type i                   value 4,
      green             type i                   value 5,
      red               type i                   value 6,
      orange            type i                   value 7.
endclass.                   " zcl_colour

*----------------------------------------------------------------------*
*   CLASS local_main DEFINITION
*----------------------------------------------------------------------*
*   Main class for this report.
*----------------------------------------------------------------------*
class local_main definition final.
  public section.
    methods:
      constructor,
      main.

  private section.
    data:
      ao_alv            type ref to cl_salv_table,
      zcl_colour        type ref to zcl_colour,
      at_output         type ty_output_tab.

    methods:
      get_data
        exporting
          et_input        type ty_input_tab,
      process
        importing
          it_input        type ty_input_tab,
      output,
      set_column_properties
        changing  co_columns      type ref to  cl_salv_columns_table,
      set_header
        importing
          iv_header               type text40
        changing
          co_column               type ref to cl_salv_column.
endclass.                   " local_main

*----------------------------------------------------------------------*
*   CLASS local_main IMPLEMENTATION
*----------------------------------------------------------------------*
*   Main class for this report.
*----------------------------------------------------------------------*
class local_main implementation.
************************************************************************
*  Constructor                                                         *
*----------------------------------------------------------------------*
*  Constructor for this class
*  This can be ommited if ZCL_COLOUR is implemented as a static class.
************************************************************************
  method constructor.
    create object: zcl_colour.
  endmethod.                " constructor

************************************************************************
*  Main method for this program.                                       *
************************************************************************
  method main.
    data:
      lt_input          type ty_input_tab.

    get_data(
      importing
        et_input = lt_input ).
    process(
      exporting
        it_input = lt_input ).
    output( ).
  endmethod.                    " main

************************************************************************
*  Get data                                                            *
*----------------------------------------------------------------------*
*  <--  ET_INPUT       Table of data in the output format.             *
************************************************************************
  method get_data.
    select clsname, cmpname, cmptype, mtdtype, mtddecltyp,
           attdecltyp, exposure, state, attvalue
    from   vseocompdf
    where  clsname    in @so_cls
    and    cmpname    in @so_cmp
    and    cmptype    in @so_typ
    and    mtdtype    in @so_mty
    and  ( attdecltyp in @so_dec
    or     mtddecltyp in @so_dec )
    and    exposure   in @so_scp
    and    state      in @so_ste
    into   table @et_input.
  endmethod.                " get_data

************************************************************************
*  Process.                                                            *
*  Loop through the input data and populate the output table with more *
*  meaningfull information.                                            *
*----------------------------------------------------------------------*
*  -->  IT_INPUT       Table of data to be reported.                   *
************************************************************************
  method process.
    field-symbols:
      <ls_input>        type zcas_find_method_input,
      <ls_output>       type zcas_find_method_output,
      <ls_colour>       type lvc_s_scol.

    loop at it_input assigning <ls_input>.
      append initial line to at_output assigning <ls_output>.
      move: <ls_input>-clsname  to <ls_output>-clsname,
            <ls_input>-cmpname  to <ls_output>-cmpname,
            <ls_input>-attvalue to <ls_output>-attvalue,
            <ls_input>-state    to <ls_output>-state.
      if <ls_input>-cmptype eq 1.
        move <ls_input>-mtddecltyp to <ls_output>-attdecltyp.
      endif.
      if <ls_input>-state eq '2'.
        move 'Obsolete'(d13) to <ls_output>-state.
      else.
        clear <ls_output>-state.
      endif.
      case <ls_input>-cmptype.
        when 0.
          move 'Data'(d01)          to <ls_output>-cmptype.
          append initial line       to <ls_output>-colour assigning <ls_colour>.
          move: 'CMPTYPE'           to <ls_colour>-fname,
                zcl_colour->yellow  to <ls_colour>-color-col.
        when 1.
          move 'Method'(d02)        to <ls_output>-cmptype.
          append initial line       to <ls_output>-colour assigning <ls_colour>.
          move: 'CMPTYPE'           to <ls_colour>-fname,
                zcl_colour->green   to <ls_colour>-color-col.
        when 2.
          move 'Event'(d03)         to <ls_output>-cmptype.
          append initial line       to <ls_output>-colour assigning <ls_colour>.
          move: 'CMPTYPE'           to <ls_colour>-fname,
                zcl_colour->orange  to <ls_colour>-color-col.
        when 3.
          move 'Type'(d04)          to <ls_output>-cmptype.
          append initial line       to <ls_output>-colour assigning <ls_colour>.
          move: 'CMPTYPE'           to <ls_colour>-fname,
                zcl_colour->blue    to <ls_colour>-color-col.
      endcase.
      if <ls_input>-cmptype eq 1.
        case <ls_input>-mtdtype.
          when 0.
            move 'Method'(d02)        to <ls_output>-mtdtype.
            append initial line       to <ls_output>-colour assigning <ls_colour>.
            move: 'MTDTYPE'           to <ls_colour>-fname,
                  zcl_colour->green   to <ls_colour>-color-col.
          when 1.
            move 'Handler'(d07)       to <ls_output>-mtdtype.
            append initial line       to <ls_output>-colour assigning <ls_colour>.
            move: 'MTDTYPE'           to <ls_colour>-fname,
                  zcl_colour->blue    to <ls_colour>-color-col.
          when 2.
            move 'Constructor'(d08)   to <ls_output>-mtdtype.
            append initial line       to <ls_output>-colour assigning <ls_colour>.
            move: 'MTDTYPE'           to <ls_colour>-fname,
                  zcl_colour->orange  to <ls_colour>-color-col.
          when 3.
            move 'Destructor'(d09)    to <ls_output>-mtdtype.
            append initial line       to <ls_output>-colour assigning <ls_colour>.
            move: 'MTDTYPE'           to <ls_colour>-fname,
                  zcl_colour->red     to <ls_colour>-color-col.
        endcase.
      endif.
      case <ls_input>-attdecltyp.
        when 0.
          move 'Instance'(d14)      to <ls_output>-attdecltyp.
          append initial line       to <ls_output>-colour assigning <ls_colour>.
          move: 'ATTDECLTYP'        to <ls_colour>-fname,
                zcl_colour->green   to <ls_colour>-color-col.
        when 1.
          move 'Static'(d15)        to <ls_output>-attdecltyp.
          append initial line       to <ls_output>-colour assigning <ls_colour>.
          move: 'ATTDECLTYP'        to <ls_colour>-fname,
                zcl_colour->yellow  to <ls_colour>-color-col.
        when 2.
          move 'Constant'(d16)      to <ls_output>-attdecltyp.
          append initial line       to <ls_output>-colour assigning <ls_colour>.
          move: 'ATTDECLTYP'        to <ls_colour>-fname,
                zcl_colour->blue    to <ls_colour>-color-col.
      endcase.
      case <ls_input>-expose.
        when 0.
          move 'Private'(d10)       to <ls_output>-expose.
          append initial line       to <ls_output>-colour assigning <ls_colour>.
          move: 'EXPOSURE'          to <ls_colour>-fname,
                zcl_colour->red     to <ls_colour>-color-col.
        when 1.
          move 'Protected'(d11)     to <ls_output>-expose.
          append initial line       to <ls_output>-colour assigning <ls_colour>.
          move: 'EXPOSURE'          to <ls_colour>-fname,
                zcl_colour->yellow  to <ls_colour>-color-col.
        when 2.
          move 'Public'(d12)        to <ls_output>-expose.
          append initial line       to <ls_output>-colour assigning <ls_colour>.
          move: 'EXPOSURE'          to <ls_colour>-fname,
                zcl_colour->green   to <ls_colour>-color-col.
      endcase.
    endloop.
  endmethod.                " process

************************************************************************
*  Output the data to an ALV grid.                                     *
************************************************************************
  method output.
    data:
      lo_functions      type ref to cl_salv_functions_list,
      lo_columns        type ref to cl_salv_columns_table,
      lo_display        type ref to cl_salv_display_settings,
      lo_ref            type ref to cx_root,
      lo_layout         type ref to cl_salv_layout,
      ls_layout_key     type salv_s_layout_key,
      lv_text           type text132.

    try.
        cl_salv_table=>factory(
          importing
            r_salv_table = ao_alv
          changing
            t_table      = at_output ).
      catch cx_salv_msg into lo_ref.
        lv_text = lo_ref->get_longtext( ).
        message e000 with lv_text.
    endtry.
    lo_functions = ao_alv->get_functions( ).
    lo_functions->set_all( co_true ).
    lo_layout = ao_alv->get_layout( ).
    move sy-cprog to ls_layout_key-report.
    lo_layout->set_key( ls_layout_key ).
    lo_layout->set_default( co_true ).
    lo_layout->set_save_restriction( co_allow_all ).
    lo_columns = ao_alv->get_columns( ).
    lo_columns->set_key_fixation( co_true ).
    lo_columns->set_optimize( ).
    set_column_properties( changing co_columns = lo_columns ).
    lo_display = ao_alv->get_display_settings( ).
    lo_display->set_striped_pattern( co_true ).
    try.
        lo_columns->set_color_column( 'COLOUR' ).
      catch cx_salv_data_error into lo_ref.
        lv_text = lo_ref->get_longtext( ).
        message e000 with lv_text.
    endtry.
    ao_alv->set_screen_popup(
        start_column = 5
        end_column   = 160
        start_line   = 5
        end_line     = 26 ).
    ao_alv->display( ).
  endmethod.                " output

**********************************************************************
*  Set column properties.                                            *
*--------------------------------------------------------------------*
*  <->  CO_COLUMNS    ALV Columns Object                             *
**********************************************************************
  method set_column_properties.
    data:
      lo_column         type ref to cl_salv_column,
      lo_ref            type ref to cx_root,
      lt_fieldcat       type salv_t_column_ref,
      lv_text           type text132.

    field-symbols:
      <ls_fieldcat>     type salv_s_column_ref.

    lt_fieldcat = co_columns->get( ).
    loop at lt_fieldcat assigning <ls_fieldcat>.
      try.
         lo_column = co_columns->get_column( <ls_fieldcat>-columnname ).
        catch cx_salv_not_found into lo_ref.
          lv_text = lo_ref->get_longtext( ).
          message e000 with lv_text.
      endtry.
      case <ls_fieldcat>-columnname.
        when 'CLSNAME'.
          set_header(
            exporting
              iv_header = 'Class'(h00)
            changing
              co_column = lo_column ).
        when 'CMPNAME'.
          set_header(
            exporting
              iv_header = 'Attribute'(h05)
            changing
              co_column = lo_column ).
        when 'CMPTYPE'.
          set_header(
            exporting
              iv_header = 'Attrib typ'(h01)
            changing
              co_column = lo_column ).
        when 'MTDTYPE'.
          set_header(
            exporting
              iv_header = 'Method Type'(h02)
            changing
              co_column = lo_column ).
        when 'EXPOSE'.
          set_header(
            exporting
              iv_header = 'Scope'(h03)
            changing
              co_column = lo_column ).
        when 'ATTVALUE'.
          set_header(
            exporting
              iv_header = 'Value'(h04)
            changing
              co_column = lo_column ).
        when 'ATTDECLTYP'.
          set_header(
            exporting
              iv_header = 'Declaration'(h07)
            changing
              co_column = lo_column ).
        when 'STATE'.
          set_header(
            exporting
              iv_header = 'State'(h06)
            changing
              co_column = lo_column ).
        when others.
      endcase.
    endloop.
  endmethod.                " set_column_properties

***********************************************************************
*   Set the header on the ALV list output.                            *
*---------------------------------------------------------------------*
*  --> IV_HEADER      Text to be placed in the column header          *
*  <-> CO_COLUMN      ALV Column Object                               *
***********************************************************************
  method set_header.
    data:
      lv_s_header       type text10,
      lv_m_header       type text20,
      lv_l_header       type text40.

    move iv_header to: lv_s_header,
                       lv_m_header,
                       lv_l_header.
    co_column->set_short_text( lv_s_header ).
    co_column->set_medium_text( lv_m_header ).
    co_column->set_long_text( lv_l_header ).
  endmethod.                " set_header.
endclass.                   " local main

************************************************************************
*  OBJECT DECLARATION                                                  *
************************************************************************
data:
  go_main               type ref to local_main                 ##NEEDED.

************************************************************************
*  EVENTS                                                              *
************************************************************************
load-of-program.
  create object go_main.

************************************************************************
*  START                                                               *
************************************************************************
start-of-selection.
  go_main->main( ).

No comments:

Post a Comment