Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to call method dynamically?

Former Member
0 Likes
7,629

we called the static method CL_ABAP_CLASSDESCR=>DESCRIBE_BY_NAME( '<class_name>' ).

The method returns an instance of CL_ABAP_CLASSDESCR, the instance name is l_object.

l_object = cl_abap_classdescr=>describe_by_name( class_name ).

We had already know that in this class(class_name), it has a Public method GET_INSTANCE(), then

call method l_object->get_instance

RECEIVING

return = l_objectXX.

but error shows: Method "GET_INSTANCE" is unknown or PROTECTED or PRIVATE. then how to use the l_object to call this method dynamically??

2 REPLIES 2
Read only

Former Member
0 Likes
1,572

The class_name here is dynamic, the value is passed by other program. so the methods of the different class_name are dynamic as well. that is why we can not use create object obj type class_name here.

Read only

Former Member
0 Likes
1,572

currently, we use below codes to realize this, but

DATA:

l_parameters TYPE abap_parmbind_tab,

l_parameter_wa TYPE abap_parmbind,

l_class_param_wa TYPE swcont,

l_descr_ref TYPE REF TO cl_abap_classdescr,

l_constructor TYPE abap_methdescr,

l_constructor_param TYPE abap_parmdescr.

FIELD-SYMBOLS: <g>.

DATA: lv_ref1 TYPE REF TO cl_abap_classdescr.

DATA: lv_ref TYPE REF TO object.

DATA: lv_ref2 TYPE REF TO object.

gv_class = "class_name ".

lv_ref1 ?= cl_abap_typedescr=>describe_by_name( gv_class ).

l_parameter_wa-name = 'RETURN'.

l_parameter_wa-kind = cl_abap_classdescr=>exporting.

INSERT l_parameter_wa INTO TABLE l_parameters.

CREATE OBJECT lv_ref

TYPE

(gv_class)

PARAMETER-TABLE

l_parameters.

CALL METHOD lv_ref->('GET_INSTANCE')

receiving

return = lv_ref2.

but when create the object, error shows:

CREATE OBJECT is not permitted for this class at this point. It was tried to create an instance of the class "CL_HRRCF_CANDIDATE_BUPA_BL".

This class is declared as 'CREATE PRIVATE', which means that the 'CREATE' for this class is 'private', that means, creating intances of this class is only allowed within the class (in the methods of the class), but not outside.

Can anyone help me on this? thnak very much!