Advertisement
rplantiko

Class Implementation Skeleton from Definition

Jun 28th, 2012
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 7.23 KB | None | 0 0
  1. *&---------------------------------------------------------------------*
  2. *& Report  Z_IMPL_FROM_DEF
  3. *&---------------------------------------------------------------------*
  4. *& Makes a class implementation skeleton from its definition
  5. *&---------------------------------------------------------------------*
  6.  
  7. report  z_impl_from_def.
  8.  
  9.  
  10. types: ty_methname_tab type standard table of seocpdname
  11.          with non-unique default key,
  12.        ty_mtdkey_tab type standard table of seomtdkey
  13.          with non-unique key clsname.
  14.  
  15. start-of-selection.
  16.   perform start.
  17.  
  18. * ---
  19. form start.
  20.  
  21.   data: lt_def type stringtab,
  22.         lt_impl type stringtab,
  23.         lo_ex type ref to cx_abap_error_analyze.
  24.  
  25.   try.
  26.  
  27.       perform :
  28.        input_def changing lt_def,
  29.        convert using lt_def changing lt_impl,
  30.        output_impl using lt_impl.
  31.  
  32.     catch cx_abap_error_analyze into lo_ex.
  33.       message lo_ex type 'I'.
  34.  
  35.   endtry.
  36.  
  37.  
  38. endform.                    "start
  39.  
  40. * ---
  41. form input_def changing et_def type stringtab.
  42.   perform edit_text
  43.     using 'Section oder Klassen-Definition'(001)
  44.     changing et_def.
  45. endform.                    "input_def
  46.  
  47. * ---
  48. form convert using it_def type stringtab
  49.              changing et_impl type stringtab.
  50.   data: lv_classname type seoclsname,
  51.         lt_methods type ty_mtdkey_tab.
  52.   perform analyze_def using it_def changing lt_methods.
  53.   perform make_impl using lt_methods
  54.                     changing et_impl.
  55. endform.                    "convert
  56.  
  57. * ---
  58. form make_impl
  59.   using    it_methods type ty_mtdkey_tab
  60.   changing et_impl    type stringtab.
  61.  
  62.   field-symbols: <ls_methods> type seomtdkey.
  63.  
  64.   clear et_impl.
  65.  
  66.   loop at it_methods assigning <ls_methods>.
  67.  
  68.     at new clsname.
  69.       perform make_class using <ls_methods>-clsname
  70.                          changing et_impl.
  71.     endat.
  72.  
  73.     perform make_method using <ls_methods>
  74.                         changing et_impl.
  75.  
  76.     at end of clsname.
  77.       perform make_endclass using <ls_methods>-clsname
  78.                             changing et_impl.
  79.     endat.
  80.  
  81.   endloop.
  82.  
  83. endform.                    "make_impl
  84.  
  85. * ---
  86. form make_class using iv_classname type seoclsname
  87.                 changing ct_impl type stringtab.
  88.  
  89.   data: lv_classname type string,
  90.         lv_line type string.
  91.  
  92.   lv_classname = iv_classname.
  93.   check lv_classname is not initial.
  94.  
  95.   concatenate
  96.     'class ' lv_classname ' implementation.'
  97.     into lv_line
  98.     respecting blanks.
  99.   append lv_line to ct_impl.
  100.  
  101. endform.                    "make_class
  102.  
  103. * ---
  104. form make_endclass using iv_classname type seoclsname
  105.                    changing ct_impl type stringtab.
  106.  
  107.   data: lv_classname type string,
  108.         lv_line type string.
  109.  
  110.   lv_classname = iv_classname.
  111.   check lv_classname is not initial.
  112.  
  113.   concatenate 'endclass.  " ' lv_classname
  114.     into lv_line
  115.     respecting blanks.
  116.   append lv_line to ct_impl.
  117.  
  118. endform.                    "make_endclass
  119.  
  120. * ---
  121. form make_method using is_methods type seomtdkey
  122.                  changing ct_impl type stringtab.
  123.  
  124.   data: lv_mtdname type string,
  125.         lv_line type string.
  126.  
  127.   lv_mtdname = is_methods-mtdname.
  128.   check lv_mtdname is not initial.
  129.  
  130.   concatenate '  method ' lv_mtdname '.' into lv_line respecting blanks.
  131.   append lv_line to ct_impl.
  132.   append '  endmethod.' to ct_impl.
  133.   append initial line to ct_impl.
  134.  
  135. endform.                    "make_endclass
  136. * ---
  137. form wrap_class_impl
  138.   using iv_classname type seoclsname
  139.   changing et_impl type stringtab.
  140.  
  141.   data: lv_line type string,
  142.         lv_clsname type string.
  143.  
  144.   lv_clsname = iv_classname.
  145.   translate lv_clsname to lower case.
  146.  
  147.   concatenate 'class ' lv_clsname ' implementation.' into lv_line respecting blanks.
  148.   insert lv_line into et_impl index 1.
  149.  
  150. * Clear last initial line, for esthetical reasons
  151.   describe table et_impl lines sy-tfill.
  152.   read table et_impl into lv_line index sy-tfill.
  153.   if lv_line is initial.
  154.     delete et_impl index sy-tfill.
  155.   endif.
  156.  
  157.   append 'endclass.' to et_impl.
  158.  
  159. endform.                    "wrap_class_impl
  160.  
  161. * ---
  162. form output_impl using it_impl type stringtab.
  163.   if it_impl is not initial.
  164.     perform edit_text
  165.       using 'Implementierungs-Rumpf'(002)
  166.       changing it_impl.
  167.   else.
  168.     message 'Nichts zu übernehmen'(003) type 'S'.
  169.   endif.
  170. endform.                    "output_impl
  171.  
  172. * ---
  173. form analyze_def
  174.     using
  175.       it_def type stringtab
  176.     changing
  177.       et_methods type ty_mtdkey_tab
  178.     raising cx_abap_error_analyze.
  179.  
  180.   data: lo_scanner     type ref to zcl_scanner,
  181.         ls_methods     type seomtdkey,
  182.         lv_interface   type seoclsname,
  183.         lv_first_token type string.
  184.  
  185.   field-symbols: <ls_stmnt> type sstmnt.
  186.  
  187.   clear et_methods.
  188.  
  189.   check it_def is not initial.
  190.  
  191.   create object lo_scanner
  192.     exporting
  193.       it_source = it_def.
  194.  
  195.   loop at lo_scanner->gt_stmnt assigning <ls_stmnt>.
  196.     lv_first_token = lo_scanner->get_first_token( <ls_stmnt> ).
  197.     case lv_first_token.
  198.       when 'CLASS'.
  199.         ls_methods-clsname = lo_scanner->get_second_token( <ls_stmnt> ).
  200.         translate ls_methods-clsname to lower case.
  201.       when 'METHODS' or 'CLASS-METHODS'.
  202.         ls_methods-mtdname = lo_scanner->get_second_token( <ls_stmnt> ).
  203.         translate ls_methods-mtdname to lower case.
  204.         insert ls_methods into table et_methods.
  205.       when 'INTERFACES'.
  206.         lv_interface = lo_scanner->get_second_token( <ls_stmnt> ).
  207.         perform add_methods_from_interface
  208.           using ls_methods-clsname
  209.                 lv_interface
  210.           changing et_methods.
  211.       when 'ENDCLASS'.
  212.         clear ls_methods-clsname.
  213.     endcase.
  214.   endloop.
  215.  
  216. endform.                    "analyze_def
  217.  
  218. * ---
  219. form add_methods_from_interface using iv_class_name type seoclsname
  220.                                       iv_interface_name type csequence
  221.                                 changing ct_methods type ty_mtdkey_tab.
  222.  
  223.  
  224.   data: ls_key type seoclskey,
  225.         ls_methods type seomtdkey,
  226.         lt_methods type seoo_methods_r,
  227.         lv_methname type seocpdname.
  228.  
  229.   field-symbols: <ls_method> type seoo_method_r.
  230.  
  231.  
  232.   ls_methods-clsname = iv_class_name.
  233.  
  234.   ls_key-clsname = iv_interface_name.
  235.   call function 'SEO_METHOD_READ_ALL'
  236.     exporting
  237.       cifkey            = ls_key
  238.     importing
  239.       methods           = lt_methods
  240.     exceptions
  241.       clif_not_existing = 1
  242.       others            = 2.
  243.  
  244.   if sy-subrc eq 0.
  245.     loop at lt_methods assigning <ls_method>.
  246.       concatenate <ls_method>-clsname '~' <ls_method>-cmpname into ls_methods-mtdname.
  247.       translate ls_methods-mtdname to lower case.
  248.       insert ls_methods into table ct_methods.
  249.     endloop.
  250.   endif.
  251.  
  252.  
  253. endform.                    "add_methods_from_interface
  254.  
  255. * ---
  256. form edit_text using iv_title
  257.                changing ct_text type stringtab.
  258.   call function 'TERM_CONTROL_EDIT'
  259.     exporting
  260.       titel          = iv_title
  261.     tables
  262.       textlines      = ct_text
  263.     exceptions
  264.       user_cancelled = 1
  265.       others         = 2.
  266.   if sy-subrc eq 1.
  267.     leave to screen 0.
  268.   elseif sy-subrc ne 0.
  269.     message id sy-msgid type 'A' number sy-msgno
  270.       with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  271.   endif.
  272. endform.                    "edit_text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement