09-29-2020 1:57 PM
Hello All,
I came across a requirement where I need to change the source code of method, FM and report dynamically. For report I am able to do this using INSERT REPORT.
For Method I got a FM (SEO_METHOD_GET_SOURCE)which gives the source code of the method . Now I want to change the source code of that method For Eg : I want to append one more line in the existing method dynamically.
Can anybody suggest me how this can be achieved?
09-30-2020 10:55 AM
Hello,
Thanks a ton to Sandra who pointed me out to the right direction. Finally I am able to achieve what I was looking for.
09-29-2020 2:05 PM
You could more easily, create a report dynamicaly and execute a Form routine of this report.
But, this kind of code is highlighted by security audit ...
Could you explain why you think you need to do this action ?
09-29-2020 2:23 PM
As said that "code on the fly" is generally not allowed as it is a security hole (this goes wayyyy back to the old old old way SAP would allow you to pass in code to an RFC and execute it which was quickly discovered to be a bad bad security flaw).
Why not take advantage of OO and do everything through different implementations of a shared interface(s)? Then in your "main" code/report, you simply will check whatever business logic you need/have now and then decided which implementation to call. That will allow you to be very flexible/scalable and achieve your "dynamicness" too.
09-29-2020 2:55 PM
Thanks for the response Christopher & Frederic!!
I know this could be a security flaw if used unethically but mine intension is to compare the 2 version of the source and if any difference is found that has to added to that respective place in the development server itself like an automation of comparison of source code. So although Christopher you suggest a good approach by achieving this through creating multiple implementation but this wont help me out in my scenario.
I believe there must be something which could allow to add the entries to source code as it is there for REPORT also using FM :
we can create a method dynamically . so there must be some way to add/modify the existing method.
Please suggest !!
09-29-2020 2:58 PM
09-29-2020 3:56 PM
Its a customization which I am building for reducing the time, effort and cost. If successfully built this could be a gamechanger isn't it??
09-29-2020 6:01 PM
All classic ABAP code is stored in Source Code Units (reports, function modules, includes, classes, etc.) Methods of global classes are stored in source unit <class>====...CM<###> where ## is stored in table TMDIR, and must be converted to base 36 (examples 10-35 = CM00A-CM00Z and 36-45 = CM010-CM019). READ REPORT and INSERT REPORT are used to read and update Source Code Units.
09-29-2020 9:18 PM
I have written an application that generates methods and method code on the fly. The techniques were readily found in blogs and answers on this site. Have you searched much?
Dynamic abap is only a security issue if it allows arbitrary code injection. If what is does is specific and limited, then it's no more risky than putting any abap program into production.
09-30-2020 9:34 AM
Hi Sandra,
Thanks for sharing the information .Just a little bit of more help. So if for an example :
I have a class ZABC with a method name ONE, So as per you told me it would be like :
ZABC================CM###. So I can use this in READ REPORT and INSERT REPORT, Is there any table which stores the exact naming convention of all this? However I debugged one standard class from which I
get the attached screenshot consisting :
CL_DEMO_OUTPUT================CM01E.
Also please help me understand what it means "and must be converted to base 36 (examples 10-35 = CM00A-CM00Z and 36-45 = CM010-CM019)." How to convert it to base 36.
09-30-2020 10:55 AM
Hello,
Thanks a ton to Sandra who pointed me out to the right direction. Finally I am able to achieve what I was looking for.
02-24-2022 8:57 PM
09-30-2020 12:30 PM
10-12-2020 1:42 PM
Hi Raymond,
This FM SEO_CLASS_GET_METHOD_INCLUDES is Obsolete.
I have used the new approach provided by SAP.
data clskey TYPE SEOCLSKEY.
FIELD-SYMBOLS <MWI> TYPE any.
CLASS CL_OO_INCLUDE_NAMING DEFINITION LOAD.
DATA OREF TYPE REF TO IF_OO_CLASS_INCL_NAMING.
DATA MTDS_W_INCL TYPE SEOP_METHODS_W_INCLUDE.
clskey = <CLASS_NAME>
OREF ?= CL_OO_INCLUDE_NAMING=>GET_INSTANCE_BY_CIFKEY( CLSKEY ).
MTDS_W_INCL = OREF->GET_ALL_METHOD_INCLUDES( ).
LOOP AT MTDS_W_INCL
ASSIGNING <MWI>.
ENDLOOP.
10-12-2020 4:00 PM
I didn't receive an alert of your question: If you want to target someone, if this person has posted an Answer, use the button COMMENT, if this person is the Original Poster of the question he/she will be automatically informed, otherwise copy/paste their hyperlinked name (like this: jayesh.mudaliar) so that the person receives a warning (NB: @ doesn't work/but typing this character will suggest hyperlinked names).
10-12-2020 4:31 PM
EDIT 2023: I realize that I posted an answer which is not directly related to the question, so including here my 2020 comment which looks more like an answer, completed with few additions:
All classic ABAP code is stored in Source Code Units (reports, function modules, includes, classes, etc.)
The methods of global classes are stored in source unit <class>====CM<###> where ## is stored in table TMDIR, and must be converted to base 36. CM must always start after the 30 characters of class, which is padded with = to fill 30 characters. Examples: CL_AAAAA======================CM001, ZCL_AAAAAAAAAAAAAAAAAAAAA=====CM001, CL_AAAAAAAAAAAAAAAAAAAAAAAAAAACM001, etc.
The table TMDIR has these columns:
The ABAP statements READ REPORT prog .. and INSERT REPORT prog .. are to be used to read and update Source Code Units. For method source code units, "prog" must be <class>====CM<###> as described above.
Answer from 2020:
Here is the code + example for converting TMDIR-METHODINDX = "00035" into "00Z" (part of extension type after "CM"):
DATA(methodindx) = CONV tmdir-methodindx( 35 ).
DATA(aaa_of_cmaaa) = ' '.
PERFORM convert_methodindx_to_cmaaa USING methodindx CHANGING aaa_of_cmaaa.
ASSERT aaa_of_cmaaa = '00Z'.
FORM convert_methodindx_to_cmaaa
USING
VALUE(methodindx) TYPE tmdir-methodindx
CHANGING
aaa_of_cmaaa TYPE char3.
CONSTANTS base TYPE c LENGTH 36 VALUE '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
DATA indx TYPE i.
aaa_of_cmaaa = ''.
DO 3 TIMES.
indx = methodindx MOD 36.
aaa_of_cmaaa = base+indx(1) && aaa_of_cmaaa.
methodindx = methodindx DIV 36.
ENDDO.
ENDFORM.