Skip to content

Instantly share code, notes, and snippets.

@mbtools
Created April 20, 2023 15:40
Show Gist options
  • Save mbtools/a0a1ca9391ee69d1b0bc333b590ed7ea to your computer and use it in GitHub Desktop.
Save mbtools/a0a1ca9391ee69d1b0bc333b590ed7ea to your computer and use it in GitHub Desktop.
Adjust method implementations for abapGit serializer classes
*&---------------------------------------------------------------------*
*& Report ZABAPGIT_ADJUST_CODE
*&---------------------------------------------------------------------*
*& Adjust method implementations for abapGit serializer classes
*&---------------------------------------------------------------------*
REPORT zabapgit_adjust_code.
PARAMETERS p_test AS CHECKBOX DEFAULT 'X'.
CLASS lcl_code DEFINITION.
PUBLIC SECTION.
CLASS-METHODS adjust
IMPORTING
!iv_program TYPE progname
!it_source TYPE seop_source_string OPTIONAL
RETURNING
VALUE(rt_source) TYPE seop_source_string
RAISING
zcx_abapgit_exception.
PRIVATE SECTION.
CLASS-METHODS pretty_code
IMPORTING
VALUE(it_source) TYPE seop_source_string
RETURNING
VALUE(rt_source) TYPE seop_source_string
RAISING
zcx_abapgit_exception.
CLASS-METHODS new_code_1
RETURNING
VALUE(rt_source) TYPE seop_source_string.
CLASS-METHODS new_code_2
RETURNING
VALUE(rt_source) TYPE seop_source_string.
ENDCLASS.
CLASS lcl_code IMPLEMENTATION.
METHOD adjust.
rt_source = it_source.
FIND 'zif_abapgit_object~get_deserialize_order' IN TABLE rt_source MATCH LINE DATA(idx).
IF idx + 12 < lines( rt_source ).
RETURN.
ENDIF.
WRITE: 'Adjusting method order' COLOR COL_POSITIVE.
" Remove unwanted code
DELETE rt_source FROM idx - 1 TO lines( rt_source ) - 1.
" Find place to insert first block
FIND 'zif_abapgit_object~get_deserialize_steps.' IN TABLE rt_source MATCH LINE idx.
ASSERT sy-subrc = 0.
DATA(lt_source) = new_code_1( ).
INSERT LINES OF lt_source INTO rt_source INDEX idx - 1.
" Find place to insert second block
FIND 'zif_abapgit_object~serialize.' IN TABLE rt_source MATCH LINE idx.
ASSERT sy-subrc = 0.
lt_source = new_code_2( ).
INSERT LINES OF lt_source INTO rt_source INDEX idx - 1.
" Pretty print
rt_source = pretty_code( rt_source ).
ENDMETHOD.
METHOD new_code_1.
APPEND `` TO rt_source.
APPEND ` METHOD zif_abapgit_object~get_deserialize_order.` TO rt_source.
APPEND ` RETURN.` TO rt_source.
APPEND ` ENDMETHOD.` TO rt_source.
APPEND `` TO rt_source.
ENDMETHOD.
METHOD new_code_2.
APPEND `` TO rt_source.
APPEND ` METHOD zif_abapgit_object~map_filename_to_object.` TO rt_source.
APPEND ` RETURN.` TO rt_source.
APPEND ` ENDMETHOD.` TO rt_source.
APPEND `` TO rt_source.
APPEND `` TO rt_source.
APPEND ` METHOD zif_abapgit_object~map_object_to_filename.` TO rt_source.
APPEND ` RETURN.` TO rt_source.
APPEND ` ENDMETHOD.` TO rt_source.
APPEND `` TO rt_source.
ENDMETHOD.
METHOD pretty_code.
CALL FUNCTION 'PRETTY_PRINTER'
EXPORTING
inctoo = abap_false
TABLES
ntext = rt_source
otext = it_source
EXCEPTIONS
enqueue_table_full = 1
include_enqueued = 2
include_readerror = 3
include_writeerror = 4
OTHERS = 5.
IF sy-subrc <> 0.
zcx_abapgit_exception=>raise( |Error pretty printing| ).
ENDIF.
ENDMETHOD.
ENDCLASS.
CLASS lcl_class DEFINITION INHERITING FROM zcl_abapgit_oo_class.
PUBLIC SECTION.
METHODS adjust
IMPORTING
!iv_class TYPE seoclsname
RAISING
zcx_abapgit_exception.
PRIVATE SECTION.
METHODS source
IMPORTING
!iv_class TYPE seoclsname
RETURNING
VALUE(rt_source) TYPE seop_source_string
RAISING
zcx_abapgit_exception.
ENDCLASS.
CLASS lcl_class IMPLEMENTATION.
METHOD adjust.
DATA:
ls_clskey TYPE seoclskey,
ls_vseoclass TYPE vseoclass,
lv_program TYPE program,
lt_source TYPE seop_source_string,
lt_local_definitions TYPE seop_source_string,
lt_local_implementations TYPE seop_source_string,
lt_local_macros TYPE seop_source_string,
lt_test_classes TYPE seop_source_string.
ls_clskey-clsname = iv_class.
ls_vseoclass = zif_abapgit_oo_object_fnc~get_class_properties( ls_clskey ).
IF ls_vseoclass IS INITIAL.
zcx_abapgit_exception=>raise( 'Not found' ).
ENDIF.
" Get code, adjust it, and save it back
lt_source = source( iv_class ).
lt_source = lcl_code=>adjust(
iv_program = cl_oo_classname_service=>get_classpool_name( iv_class )
it_source = lt_source ).
IF p_test IS INITIAL.
zif_abapgit_oo_object_fnc~deserialize_source(
is_key = ls_clskey
it_source = lt_source ).
ENDIF.
" Code for adjusting local parts and test classes
* lt_local_definitions = lcl_code=>adjust(
* iv_program = cl_oo_classname_service=>get_ccdef_name( iv_class )
* it_source = it_source ).
*
* lt_local_implementations = lcl_code=>adjust(
* iv_program = cl_oo_classname_service=>get_ccimp_name( iv_class )
* it_source = it_source ).
*
* lt_local_macros = lcl_code=>adjust(
* iv_program = cl_oo_classname_service=>get_ccmac_name( iv_class )
* it_source = it_source ).
*
* lt_test_classes = lcl_code=>adjust(
* iv_program = cl_oo_classname_service=>get_ccau_name( iv_class )
* it_source = it_source ).
*
* zif_abapgit_oo_object_fnc~generate_locals(
* is_key = ls_clskey
* it_local_definitions = lt_local_definitions
* it_local_implementations = lt_local_implementations
* it_local_macros = lt_local_macros
* it_local_test_classes = lt_test_classes ).
ENDMETHOD.
METHOD source.
DATA:
lo_source TYPE REF TO if_oo_clif_source,
lo_instance TYPE REF TO cl_oo_factory.
TRY.
CALL METHOD cl_oo_factory=>create_instance
RECEIVING
result = lo_instance.
CALL METHOD lo_instance->create_clif_source
EXPORTING
clif_name = iv_class
version = 'A'
RECEIVING
result = lo_source.
CALL METHOD lo_source->get_source
IMPORTING
source = rt_source.
CATCH cx_root INTO DATA(lx_error).
zcx_abapgit_exception=>raise( lx_error->get_text( ) ).
ENDTRY.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA:
go_class TYPE REF TO lcl_class.
TRY.
SELECT * FROM seometarel INTO TABLE @DATA(gt_classes) WHERE refclsname LIKE 'ZIF#_ABAPGIT#_OBJECT' ESCAPE '#'.
LOOP AT gt_classes INTO DATA(gs_class).
WRITE: / 'ADJUST', gs_class-clsname.
CREATE OBJECT go_class.
go_class->adjust( gs_class-clsname ).
ENDLOOP.
CATCH zcx_abapgit_exception INTO DATA(gx_error).
MESSAGE gx_error TYPE 'S' DISPLAY LIKE 'E'.
ENDTRY.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment