Skip to Content
author's profile photo Former Member
Former Member

Call Method of interface at runtime

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Posted on Nov 19, 2012 at 02:21 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Nov 20, 2012 at 04:42 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Nov 20, 2012 at 06:47 AM
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.