动态调用类方法(Dynamic Method Call)

时间:2024-05-19 13:53:47

参考文档

T-CODE: ABAPDOCU

动态调用类方法(Dynamic Method Call)

*&---------------------------------------------------------------------*
*& Report YTEST
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ytest.

CLASS cls DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS meth IMPORTING  p TYPE string
                       EXCEPTIONS e.
ENDCLASS.

CLASS cls IMPLEMENTATION.
  METHOD meth.
    cl_demo_output=>write( p ).
    RAISE e.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA(class) = `CLS`.
  DATA(meth) = `METH`.

  DATA(ptab) = VALUE abap_parmbind_tab(
    ( name  = 'P'
      kind  = cl_abap_objectdescr=>exporting
      value = REF #( `Hello method!` ) ) ).

  DATA(etab) = VALUE abap_excpbind_tab(
    ( name = 'E' value = 4 ) ).

  CALL METHOD (class)=>(meth)
    PARAMETER-TABLE ptab
    EXCEPTION-TABLE etab.
  IF sy-subrc <> 0.
    cl_demo_output=>display( `Exception caught ...` ).
  ENDIF.

结构 ABAP_PARMBIND_TAB 中kind字段对应如下几个值:

*    CL_ABAP_OBJECTDESCR=>EXPORTING for input parameter
*    CL_ABAP_OBJECTDESCR=>IMPORTING for output parameter
*    CL_ABAP_OBJECTDESCR=>CHANGING  for input/output parameter
*    CL_ABAP_OBJECTDESCR=>RECEIVING for returning parameter

执行结果如下:

动态调用类方法(Dynamic Method Call)

 

*********************************************************************

根据动态类方法的调用操作,

“ABAP PROXY - GET MESSAGE ID - OUTBOUND” ->  获取SXI_MONITOR 外发消息的MESSAGE_ID  代码进行通用性封装

  METHOD mt_outmsgid.
* 结构 ABAP_PARMBIND_TAB 中kind字段对应如下几个值:
*    CL_ABAP_OBJECTDESCR=>EXPORTING for input parameter
*    CL_ABAP_OBJECTDESCR=>IMPORTING for output parameter
*    CL_ABAP_OBJECTDESCR=>CHANGING  for input/output parameter
*    CL_ABAP_OBJECTDESCR=>RECEIVING for returning parameter
    DATA: lv_method TYPE string,
          lt_ptab   TYPE abap_parmbind_tab,
          lt_etab   TYPE abap_excpbind_tab.

    DATA: lo_protocol TYPE REF TO if_wsprotocol,
          lo_msg      TYPE REF TO if_wsprotocol_message_id.

    CHECK lr_req IS NOT INITIAL.

    "GET METHOD NAME
    lv_method = zcl_itf_common_2=>gc_protocol_method_name."GET_PROTOCOL

    "SET PARAMETER
    lt_ptab = VALUE #(
                        ( name = 'PROTOCOL_NAME'
                          kind = cl_abap_objectdescr=>exporting
                          value = REF #( if_wsprotocol=>message_id ) )
                        ( name = 'PROTOCOL'
                          kind = cl_abap_objectdescr=>receiving
                          value = REF #( lo_protocol ) )
                        ).

    TRY.
        CALL METHOD lr_req->(lv_method) PARAMETER-TABLE lt_ptab.

        lo_msg ?= lo_protocol.
        DATA(lv_pi_out_msgid_raw) = lo_msg->get_message_id( ).

      CATCH cx_sy_create_object_error INTO DATA(exc_ref).
        DATA(exc_text) = exc_ref->get_text( ).
        MESSAGE exc_text TYPE 'I'.
    ENDTRY.

  ENDMETHOD.

其中

LR_REQ

动态调用类方法(Dynamic Method Call)

 

GC_PROTOCOL_METHOD_NAME

动态调用类方法(Dynamic Method Call)