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: 

Change source code of Method dynamically

jayesh_mudaliar
Explorer

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?

1 ACCEPTED SOLUTION

jayesh_mudaliar
Explorer
0 Kudos

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.

14 REPLIES 14

FredericGirod
Active Contributor
0 Kudos

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 ?

ChrisSolomon
Active Contributor
0 Kudos

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.

jayesh_mudaliar
Explorer
0 Kudos

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 :

  1. SEO_METHOD_CREATE
  2. SEO_METHOD_GENERATE_INCLUDE
  3. SEO_CLASS_GENERATE_SECTIONS

we can create a method dynamically . so there must be some way to add/modify the existing method.
Please suggest !!

FredericGirod
Active Contributor
0 Kudos

Are you recreating Abap Git ?

jayesh_mudaliar
Explorer
0 Kudos

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??

Sandra_Rossi
Active Contributor

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.

matt
Active Contributor
0 Kudos

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.

jayesh_mudaliar
Explorer
0 Kudos

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.

jayesh_mudaliar
Explorer
0 Kudos

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.

0 Kudos

Note sure that you pasted the right screenshot 😉

raymond_giuseppi
Active Contributor

Did you use a FM such as SEO_CLASS_GET_METHOD_INCLUDES?

jayesh_mudaliar
Explorer
0 Kudos

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.

Sandra_Rossi
Active Contributor
0 Kudos

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).

Sandra_Rossi
Active Contributor
0 Kudos

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:

  • CLASSNAME
  • METHODINDX (NUMC 5) :
    • 0 : to be ignored (may exist for unknown reason, METHODNAME is empty)
    • 1 .. 9 : CM001 .. CM009
    • 10 .. 35 : CM00A .. CM00Z
    • 36 .. 45 : CM010 .. CM019
    • 46 .. 71 : CM01A .. CM01Z
    • 72 .. 81 : CM020 .. CM02Z
    • etc.
  • METHODNAME

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.