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

Invoke a method dynamically

raghavendra_subhash
Product and Topic Expert
Product and Topic Expert
0 Likes
2,495

Hi,

I want to invoke a method dynamically. I have the class name and method name in a variable and the list of import and export parameter for this method in an internal table. Now I need to invoke this method dynamically i.e. without having it to call in code like this classname->methodname(.....). I want something like reflection in java, where we can call the method using invoke method of method class ex:

try {

Method method = cls.getMethod( "setColor",

new Class[] {Color.class} );

method.invoke( obj, new Object[] );

}

It would be helpful if you can provide a simple example on how it can be implemented in ABAP.

Thanks,

Raghavendra

1 ACCEPTED SOLUTION
Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
1,190

Hi,

You can perform calls in that way as follows.

You need to pass the parameters usingthe following table of type ABAP_PARAMBIND_TAB.

DATA: ptab type ABAP_PARAMBIND_TAB,

ptab_line like line of ptab.

DATA: clas_name type stirng VALUE 'CLASS_NAME',

method_name type string VALUE 'METHOD_NAME'.

DATA: obj type ref to OBJECT.

create object obj type class_name.

ptab_line-name = 'PARAM_NAME'.

ptab_line-kind = CL_ABAP_OBJECTDESCR=>EXPORTING.

Then you can use

CALL METHOD obj->(method_name) PARAMETER-TABLE ptab.

Regards,

Sesh

2 REPLIES 2
Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
1,191

Hi,

You can perform calls in that way as follows.

You need to pass the parameters usingthe following table of type ABAP_PARAMBIND_TAB.

DATA: ptab type ABAP_PARAMBIND_TAB,

ptab_line like line of ptab.

DATA: clas_name type stirng VALUE 'CLASS_NAME',

method_name type string VALUE 'METHOD_NAME'.

DATA: obj type ref to OBJECT.

create object obj type class_name.

ptab_line-name = 'PARAM_NAME'.

ptab_line-kind = CL_ABAP_OBJECTDESCR=>EXPORTING.

Then you can use

CALL METHOD obj->(method_name) PARAMETER-TABLE ptab.

Regards,

Sesh

Read only

0 Likes
1,190

Hi Sesh,

I'm on SAP 4.7 and there isn't any type ABAP_PARAMBIND_TAB. Can you please describe how it is declare in order to recreate it here.

thanks in advanced,

Eric