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: 

[ABAP-OO] How to create dynamically a Method for a class?

Former Member
0 Kudos

Hello everyone,

I would like to know, how to create a method dynamically for a class (in SE24) from a program?

I have checked this one out, but not working on SAP 7.40...

Thank you.

Rachid.

1 ACCEPTED SOLUTION

former_member184158
Active Contributor
0 Kudos

Hi Rachid,

you can create a method dynamically, so it means, you have to know if this method has parameters, or not, if this method exist, or not, otherwise, you will get dump.

DATA:
        lo_object     
TYPE REF TO object,
        lv_method     
TYPE string,
        lv_classname  
TYPE seoclsname.

   lv_classname
= 'ZIBO_CL_TEST'.
   lv_method
= 'DYNAMIC_METHOD'.

     
CREATE OBJECT lo_object TYPE (lv_classname).
     
IF lo_object IS NOT INITIAL.
        lv_method
=  lv_method.
       
TRY.
           
CALL METHOD lo_object->(lv_method).

         
CATCH cx_sy_dyn_call_error .
        ENDTRY.

       
CLEAR lv_method.
     
ENDIF.


Regards

Ibr

13 REPLIES 13

former_member184158
Active Contributor
0 Kudos

Hi Rachid,

you can create a method dynamically, so it means, you have to know if this method has parameters, or not, if this method exist, or not, otherwise, you will get dump.

DATA:
        lo_object     
TYPE REF TO object,
        lv_method     
TYPE string,
        lv_classname  
TYPE seoclsname.

   lv_classname
= 'ZIBO_CL_TEST'.
   lv_method
= 'DYNAMIC_METHOD'.

     
CREATE OBJECT lo_object TYPE (lv_classname).
     
IF lo_object IS NOT INITIAL.
        lv_method
=  lv_method.
       
TRY.
           
CALL METHOD lo_object->(lv_method).

         
CATCH cx_sy_dyn_call_error .
        ENDTRY.

       
CLEAR lv_method.
     
ENDIF.


Regards

Ibr

0 Kudos

Hello Ibrahim,

thank you for you answer. Unfortunately with this code, you do not create a method dynamically but you call an existing method dynamically.

My need consist to create a method that does not exist yet in the used class

Rachid.

0 Kudos

Hello Rachid,

thank you for this info, but actually, I have tried it out, but I got dump,

  "Method "DESCR" is not declared or inherited in class "ZIBO_CL_TEST". -"

  " "

I will try to create it , if can get a positive result, I will post it.

Regards

Ibr

0 Kudos

Hi Ibrahim,

yes of course, you can not call a method if this one does not exist at all in the definition of the class. But if you create the definition of your method in your class, your code will run well.


That's why in my case, I am looking for a way to create dynamiccaly/automatically a method before calling, to make sure that my program will not dump.

I am debugging the standard in SE24, the function group SEOD sounds good and contains all functionalities to perform my need. I am following up this trail. I will inform you if I get a solution.

Rachid.

0 Kudos

Hi

yes you are right, it does not working on SAP 7.40

could you try another FM, like

CALL FUNCTION 'SEO_CLASS_CREATE_SOURCE'

      EXPORTING

         class   = ls_class

         source  = lt_imp

*       LOCALS  =

         version = seoc_version_inactive

      EXCEPTIONS

          other   = 1

          OTHERS  = 2.

Regards

Ibr

0 Kudos

Hi,

yes, very good approach, thank you for your help Ibrahim. I check out this MF and it creates source-code into an existing method or an inherited existing method.

But in "SE37  with 'SEO_*', I will normally found something good.

Goal is close

Rachid.

0 Kudos

Got it!

  • MF: SIW_RFC_WRITE_CLASS_METHOD

I am developping a framework for several differents systems. I do not know, if this MF will be available for all others SAP 740 systems ?

Rachid.

0 Kudos

Hi Rachid,

thank you, I will try to create it, but I have looked for if this FM has been used by SAP, but, I did not find,

anyway, If I can create it and call a dynamic method, I will post it.

All in once

have a nice day.

Regards

Ibrahim

0 Kudos

Hi Ibrahim,

you're welcome, thank for your help too.

"All in once" is the best, yes! But time-consuming. I have also seen that if you want to create a simple method very quickly, juste:

1) Create a ZCL_TEST1 Class and put that code in your CONSTRUCTOR:


METHOD constructor.

     DATA i_clsname    TYPE  seoclsname                          .

     DATA i_methodname TYPE  seocpdname                          .

     DATA i_tab_code   TYPE  siw_tab_code                        .

     DATA i_str_code   TYPE  siw_dte_code                        .

     DATA lv_uzeit     TYPE  syuzeit                             .

     i_clsname     = 'ZCL_TEST1'                                 . "An Existing Persistance Class!

     CONCATENATE 'METHOD_' sy-uzeit          INTO  i_methodname  .

* Implementation

     CONCATENATE 'METHOD ' i_methodname '.'  INTO  i_str_code

                                             SEPARATED BY space  .

     APPEND        i_str_code                TO    i_tab_code    .

     i_str_code    = 'WRITE ''Implementation TEST'' .'           .

     APPEND        i_str_code                TO    i_tab_code    .

     i_str_code    = 'ENDMETHOD.'                                .

     APPEND        i_str_code                TO    i_tab_code    .

     CALL FUNCTION 'SIW_RFC_WRITE_CLASS_METHOD'

       EXPORTING

         i_clsname    = i_clsname

         i_methodname = i_methodname

         I_TAB_CODE   = I_TAB_CODE.

   ENDMETHOD.

2) Execute your class directly with F8 and make sure before, that you are in display mode. Get out of SE24 and go in again (to refresh ALV)...

This snippet creates quick methods without any parameters ...

Have a great day!

Rachid.

0 Kudos

Finally,

I have found the SAP MFs used by SAP for their components:

  • SEO_METHOD_CREATE

We can also specify the method parameters/exceptions/... with others MF:

  • SEO_PARAMETER_CREATE ect.

Bye.

Rachid.

0 Kudos

Hi,

Respect

why is not active?

it sounds great, but I have more questions, I will try to answer them by using the FMs . let us to see .

It is really interesting,


we have also to create a method to look if this method already exists, or not, and there are many open questions.,

we can do many thing during run time, or you can do a  Customizing table, which parameters you need, ..
what should be done, and it will be created. this sounds good.

in Customizing table, it could be done, for example, active as flag ( method) active or not,

delete, create, etc.

Regards

Ibr

0 Kudos

Hi Ibrahim,

you're welcome.

1) Concerning the creation of a new method to check if the dynamic method already exists... The exporting parameter "E_STR_EXCEPTION" of the MF can give us some usable details. But concretely, the MF does not re-create the method if this one has already been created before. Anyway, no dump, no error if the method has already been created

2) Regarding my point of view concerning the OO Utilities, I prefer oriented my work on dynamic framework based on several Design Pattern and let the database (Customizing table) far away from my code. I can not give you more and precise information about the context for the moment, because I am exactly working on a Workbench Framework, but if I may suggest you, try to store your method or some Generic Class Behaviors in a Dynamic Global Class instantiable with a combination of Interfaces and Heritances for the childs class.

I hope to finish up this part of the framework quickly and share it for the others. You will know exactly the purpose of this current topic at this time.

Wish you a great day.

Rachid.

ps: not active, because maybe you missed "ENDMETHOD."

  i_str_code    = 'ENDMETHOD.'                                .

  APPEND        i_str_code                TO    i_tab_code    .

0 Kudos

This message was moderated.