Skip to Content

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_methodclsname = 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 = sylangu.
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_parameterclsname = 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_sourceCPDNAME = 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_sourceSOURCE  = l_tbl_code.

mtdkeyclsname = 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_sourceredefine
implementation_expanded
= l_wa_method_sourcesource
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.

To report this post you need to login first.

4 Comments

You must be Logged on to comment or reply to a post.

  1. Former Member

    What’s the version that this worked?

    I created a simple class ZCL_TEST without anything and non-final and it’s failing to create the method. I’m on SAP_ABA and SAP_BASIS release 702 SP 08

    (0) 
    1. Former Member Post author

      I try it on 701 and it works,

      Make sure the class is public. Activate the class, make sure it has some methods and it works.

      (0) 
  2. Former Member

    There is an open source project named SAPLINK that lets you upload/download SAP developments.

    The code for uploading entire class is present in class ZSAPLINK_CLASS method

    CREATEOBJECTFROMIXMLDOC, and it uses FM SEO_CLASS_CREATE_COMPLETE.

    For a more complex scenario, you can reuse SAPLINK project to download the existing class to nugget file (xml), manipulate the xml content and then upload modified class.

    (0) 

Leave a Reply