Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

I was in the need to make a dynamic call from within an ABAP class method to a functional method, which isn't known at compile time. The class can be subtyped, and from anywhere down the class hierarchy, this generic method should return the value of any functional method.

The short-cut statement would be great to use ..l like this:

   ev_value = me->(lv_dynamic_method_name)( ).

but the ABAP compiler only allows to use dynamic calls with the full statement

   CALL METHOD (lv_dynamic_method_name) RECEIVING rv_value = ev_value.

But in difference to the first short-cut solution, you have to know the name of the receiving parameter (likewise with a naming convention) or retrieve it somehow. As I disliked to create a development rule to name all returning parameter RV_VALUE, I was bound to use the runtime type description.

And here is the template to make such a dynamic call, without knowing the actual parameter name of the receiving element.

method GET_RECEIVING_PARAMETER.

    "Get the class description of myself
    DATA: lo_typeDescription                       TYPE REF TO CL_ABAP_TYPEDESCR.
    lo_typeDescription = CL_ABAP_TYPEDESCR=>DESCRIBE_BY_OBJECT_REF( me ).

    CHECK lo_typeDescription->type_kind = CL_ABAP_TYPEDESCR=>typekind_class.

           "Only ABAP OO Classes are allowed

    DATA: lo_classDescription                       TYPE REF TO CL_ABAP_CLASSDESCR.
    lo_classDescription ?= lo_typeDescription.

    FIELD-SYMBOLS: <ls_method_definition>           TYPE abap_methdescr.
    READ TABLE lo_classDescription->methods ASSIGNING <ls_method_definition>

                                            WITH KEY name = iv_methodName.
    CHECK sy-subrc = 0.       "Yes, we really need the method to be

                              "existing to deliver the receiving type

    "Retrieve the name of the receiving parameter
    FIELD-SYMBOLS: <ls_receiving_parameter>         TYPE LINE OF abap_parmdescr_tab.
    READ TABLE <ls_method_definition>-parameters ASSIGNING <ls_receiving_parameter>
                                                 WITH KEY parm_kind = CL_ABAP_CLASSDESCR=>RECEIVING.
    CHECK sy-subrc = 0.

    ls_receiving_parameter-name  = <ls_receiving_parameter>-name.
    ls_receiving_parameter-kind  = <ls_receiving_parameter>-parm_kind.

    "To-Be assigned by caller:  structure field  VALUE  filled with a type ref to data,
    "by using the statements:
    "  GET REFERENCE OF <your-variable-name> INTO ls_receiving_parameter-value.
    "  APPEND ls_receiving_parameter TO lt_parameter_table.
    "  CALL METHOD (lv_method_name) PARAMETER TABLE lt_parameter_table.
    "
    "  After that call you can find the result in the variable, that you've used with
    "  the GET REFERENCE statement.


endmethod.

And the dynamic call is simply something like this:

DATA: lt_parameter_table             TYPE ABAP_PARMBIND_TAB,
      ls_receiving_parameter         TYPE LINE OF ABAP_PARMBIND_TAB.
      ls_receiving_parameter = me->get_receiving_parameter( lv_methodname ).

IF ls_receiving_parameter IS INITIAL.
   "We have a problem, so we leave the returned value blank now
   EXIT.
ENDIF.

GET REFERENCE OF ev_value INTO ls_receiving_parameter-value.
INSERT ls_receiving_parameter INTO TABLE lt_parameter_table.


CALL METHOD (lv_methodname) PARAMETER-TABLE lt_parameter_table.

Take care

   Florin Wach

   Systems-Integration

   SAP Business Workflow Senior-Expert

1 Comment