Application Development 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: 

Call Method of interface at runtime

Former Member
0 Kudos

Hello

     I'm trying to call a method of a class that implements a custom interface. I receive the class name as a parameter in my method so i have to create the object at runtime.

This is currently my code:

try.

         " Call the Interface Method to Get Additional

         create object generic_class type (gs_class_name).

         if_xxx_gs_mapping ?= generic_class.

         call method if_xxx_gs_mapping->map_gs_fields

           exporting

             it_form_values              = it_form_values

           importing

             it_keys                  = it_keys

           changing

             it_fields = it_fields

           exceptions

             error_mapping_fields        = 1

             others                      = 2.

         if sy-subrc <> 0.

           raise error_mapping_gs_fields.

         endif.

       " Catch - Possible Errors

       catch cx_sy_create_object_error .

         raise error_calling_interface.

       catch cx_sy_dyn_call_param_missing .

         raise error_calling_interface.

       catch  cx_sy_dyn_call_illegal_method.

         " Class doesnt implement the method

         return.

     endtry.

The problem is this code always enter's in the cx_sy_dyn_call_illegal_method.

I have checked the gs_class_name implements the interface and both the class and the method are active.

i have also tried to do this:

     create object if_xxx_gs_mappingtype (gs_class_name).

In the method call the exception is the same

What could be the problem?

Thanks

3 REPLIES 3

alex_cook
Active Participant
0 Kudos

Howdy,

If the method is a method of the interface, then you need to include the interface name in the method call.

You don't need to cast the object back to the interface type as per this line here:

   if_xxx_gs_mapping ?= generic_class.

So assuming your interface name is if_xxx_gs_mapping, your method call should look something like:

     generic_class->if_xxx_gs_mapping~map_gs_fields(

...

You can get around this by defining an alias for the method name in each of your classes but the above is probably simpler.

Cheers

Alex

SuhaSaha
Advisor
Advisor
0 Kudos

Hello Filipe,

What is the exception text you're getting with the exception CX_SY_DYN_CALL_ILLEGAL_METHOD? May be that'll help us identifying the root-cause

BR,

Suhas

former_member193464
Contributor
0 Kudos