08-08-2015 9:55 PM
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.
08-08-2015 10:51 PM
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
08-08-2015 10:51 PM
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
08-09-2015 11:38 AM
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.
08-09-2015 11:57 AM
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
08-09-2015 12:44 PM
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.
08-09-2015 1:07 PM
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
08-09-2015 1:54 PM
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.
08-09-2015 2:32 PM
Got it!
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.
08-09-2015 3:12 PM
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
08-09-2015 3:28 PM
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.
08-09-2015 3:59 PM
Finally,
I have found the SAP MFs used by SAP for their components:
We can also specify the method parameters/exceptions/... with others MF:
Bye.
Rachid.
08-09-2015 4:28 PM
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
08-09-2015 5:13 PM
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 .
04-20-2016 1:04 PM