Quantcast
Channel: ABAP Development
Viewing all articles
Browse latest Browse all 948

Creating Method Dynamically

$
0
0

This is my first post in this blog, well playing with ABAP I thought it could be cool to add methods to a class dynamically.  Am sharing with you a sample coding that just do that.

 

After searching the internet there are some reference to SBMIG_CREATE_CLASS_AND_TTYPES progam. Ideally the program creates a class/type dynamically. Tracing the code I found the following functions

 

 

  • SEO_METHOD_CREATE_F_DATA: that creates a method definition in a class.
  • SEO_PARAMETER_CREATE_F_DATA: that creates the method parameter
  • SEO_METHOD_GENERATE_INCLUDE: that creates the code implantation.

 

 

 

There isn’t much of a documentation (or there is and I couldn't find it) so I play with the coding and wrote a simple program that just adds a method to a static class. The program (see the code below) does not save the method, after executing the code the method does not exist. It creates a public static method call “Descr” on an existing class call ZCL_TEST. The new method returns a string ‘Test’.

 


Data: l_wa_method type vseomethod,
l_wa_method_source
type seo_method_source,
seox_false
type c
value ' ',
seox_true
type c
value 'X',
l_wa_method_parameter
type VSEOPARAM,
l_tbl_code
type  RSWSOURCET,
l_wa_code
type string,
mtdkey
TYPE seocpdkey,
l_v_r
(10) type c,
l_v_method_name
type string VALUE 'DESCR',
l_o
type REF TO object,
l_v_classname
(60) type c value 'ZCL_TEST',
cfkey
type SEOCLSKEY,
l_v_c
type currkey,
l_v_genflag
type c VALUE ' '
.


START-OF-SELECTION.

l_wa_method
-clsname = l_v_classname.
l_wa_method
-CMPNAME = l_v_method_name.
l_wa_method
-DESCRIPT = 'Testing Method'.
l_wa_method
-version '1'.
l_wa_method
-langu = sy-langu.
l_wa_method
-MTDTYPE = '0'.
l_wa_method
-MTDDECLTYP = '1'. "static method = 1- instance = 0
l_wa_method
-EXPOSURE = '2'. "public = 2, private = 0, protected = 1
l_wa_method
-state = '1'.
l_wa_method
-REFCLSNAME = l_v_classname.
l_wa_method
-REFINTNAME = l_v_classname.
l_wa_method
-REFCMPNAME = l_v_method_name.


l_wa_method_parameter
-clsname = l_v_classname.
l_wa_method_parameter
-CMPNAME = l_v_method_name.
l_wa_method_parameter
-SCONAME = 'r_v_desc'.
l_wa_method_parameter
-VERSION = '1'.
l_wa_method_parameter
-PARDECLTYP = '3'.
l_wa_method_parameter
-PARPASSTYP = '0'.
l_wa_method_parameter
-TYPTYPE = '1'. " like =0,  type=1, type ref=2
l_wa_method_parameter
-TYPE = 'char10'.

l_wa_method_source
-CPDNAME = l_v_method_name.
l_wa_method_source
-REDEFINE = '0'.
l_wa_code 
= ' r_v_desc = ''Test 12121''.' .
append l_wa_code to l_tbl_code.

l_wa_method_source
-SOURCE  = l_tbl_code.

mtdkey
-clsname = l_v_classname.
mtdkey
-cpdname = l_v_method_name.


move-CORRESPONDING l_wa_method to  cfkey.
CALL FUNCTION 'SEO_BUFFER_REFRESH'
EXPORTING
cifkey 
= cfkey
version
= '0'.

CALL FUNCTION 'SEO_METHOD_CREATE_F_DATA'
EXPORTING
save  
= seox_false
CHANGING
method = l_wa_method
EXCEPTIONS
OTHERS = 1.

CALL FUNCTION 'SEO_PARAMETER_CREATE_F_DATA'
EXPORTING
save     
= seox_false
CHANGING
parameter = l_wa_method_parameter
EXCEPTIONS
OTHERS    = 1.


CALL FUNCTION 'SEO_METHOD_GENERATE_INCLUDE'
EXPORTING
mtdkey                 
= mtdkey
force                  
= 'X'
redefine               
= l_wa_method_source-redefine
implementation_expanded
= l_wa_method_source-source
suppress_index_update  
= 'X'
suppress_corr          
= 'X'
EXCEPTIONS
OTHERS                  = 1.






call method ZCL_TEST=>(l_v_method_name)
RECEIVING
r_v_desc
= l_v_r
.

write l_v_r.


Viewing all articles
Browse latest Browse all 948

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>