11-18-2012 10:28 PM
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
11-19-2012 2: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
11-20-2012 4: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
11-20-2012 6:47 AM
Hi ,
check this link .
http://wiki.sdn.sap.com/wiki/display/ABAP/Calling+Interface+class+method+dynamically
hope it was helpful.