ABAP动态编程-动态生成子例程和本地类-GENERATE SUBROUTINE POOL
生活随笔
收集整理的這篇文章主要介紹了
ABAP动态编程-动态生成子例程和本地类-GENERATE SUBROUTINE POOL
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
前言
1. 動態生成子例程并調用
2.?動態生成本地類并調用
前言
????????本文為ABAP編程中動態生成子例程/本地類的簡單示例,依托語句GENERATE SUBROUTINE POOL實現。
1. 動態生成子例程并調用
????????代碼示例:? ? ?
DATA: lt_source TYPE TABLE OF string WITH EMPTY KEY,lv_source TYPE string,lv_program TYPE sy-repid,lv_message TYPE text240,lv_lines TYPE i,lv_word TYPE char72,lv_sid TYPE string.lt_source = VALUE #( ( `PROGRAM.` )( `FORM DYN_FORM.` )( ` DATA LS_T001 TYPE T001.` )( ` SELECT SINGLE * FROM T001 INTO LS_T001 WHERE bukrs = '0001'.` )( `WRITE: LS_T001-BUTXT.` )( `ENDFORM.` )).TRY .GENERATE SUBROUTINE POOL lt_source NAME lv_programMESSAGE lv_messageLINE lv_lines WORD lv_wordSHORTDUMP-ID lv_sid.CATCH cx_sy_generate_subpool_full .sy-subrc = 2.CATCH cx_sy_gen_source_too_wide .sy-subrc = 3. ENDTRY.IF sy-subrc = 0.PERFORM ('DYN_FORM') IN PROGRAM (lv_program) IF FOUND. ELSEIF sy-subrc = 4.MESSAGE lv_message TYPE 'I'. ELSEIF sy-subrc = 8.MESSAGE lv_sid TYPE 'I'. ELSE.MESSAGE 'GENERATE SUBROUTINE POOL EXCEPTION' TYPE 'I'. ENDIF.????????PS:根據代碼字符串生成對應的子例程,適用于根據配置表或其它條件動態生成對應邏輯的處理, 或者處理一些其它動態調用無法靈活實現的場景,和主程序的數據交互也較為方便。
2.?動態生成本地類并調用
? ? ? ? 代碼示例:
DATA: lt_source TYPE TABLE OF string WITH EMPTY KEY,lv_source TYPE string,lv_program TYPE sy-repid,lv_message TYPE text240,lv_lines TYPE i,lv_word TYPE char72,lv_sid TYPE string.DATA: lv_classname TYPE string,lo_object TYPE REF TO object.lt_source = VALUE #(( `PROGRAM.` )( `CLASS main DEFINITION.` )( ` PUBLIC SECTION.` )( ` CLASS-METHODS meth_static.` )( ` METHODS meth_instance.` )( `ENDCLASS.` )( `CLASS main IMPLEMENTATION.` )( ` METHOD meth_static.` )( ` WRITE: 'Static Method'.` )( ` ENDMETHOD.` )( ` METHOD meth_instance.` )( ` WRITE: 'Instance Method'.` )( ` ENDMETHOD.` )( `ENDCLASS.` ) ).TRY .GENERATE SUBROUTINE POOL lt_source NAME lv_programMESSAGE lv_messageLINE lv_lines WORD lv_wordSHORTDUMP-ID lv_sid.CATCH cx_sy_generate_subpool_full .sy-subrc = 2.CATCH cx_sy_gen_source_too_wide .sy-subrc = 3. ENDTRY.IF sy-subrc = 0.TRY." 動態調用 靜態方法lv_classname = `\PROGRAM=` && lv_program && `\CLASS=MAIN`.CALL METHOD (lv_classname)=>meth_static." 動態調用 實例方法CREATE OBJECT lo_object TYPE (lv_classname).CALL METHOD lo_object->('METH_INSTANCE').CATCH cx_sy_dyn_call_error.MESSAGE 'Dynamic call error' TYPE 'I'.ENDTRY.ELSEIF sy-subrc = 4.MESSAGE lv_message TYPE 'I'. ELSEIF sy-subrc = 8.MESSAGE lv_sid TYPE 'I'. ELSE.MESSAGE 'GENERATE SUBROUTINE POOL EXCEPTION' TYPE 'I'. ENDIF.? ? ? ? 結果展示:
????????PS: 和生成子例程使用同樣的語法,具體使用看個人喜好。
?關聯文章:
ABAP動態編程-動態數據對象_xiefireworks的博客-CSDN博客
ABAP動態編程-動態調用子例程&方法及動態SQL_xiefireworks的博客-CSDN博客
ABAP動態編程-動態生成子例程和本地類-GENERATE SUBROUTINE POOL_xiefireworks的博客-CSDN博客
ABAP動態編程-動態生成報表、屏幕_xiefireworks的博客-CSDN博客
總結
以上是生活随笔為你收集整理的ABAP动态编程-动态生成子例程和本地类-GENERATE SUBROUTINE POOL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 聊聊AIOps落地监控报警的应对之策
- 下一篇: JS 不可逆加密后半部分,去混淆还原代码