Dynamic program generation
The ABAP language also allows the creation of ABAP programs at runtime. An INSERT REPORT
statement may be used for creating a new program specified by the a name in the statement. Dynamic program generation should only be used when there is no alternate option.
Also, you may generate a subroutine pool using a GENERATE
subroutine pool statement. A subroutine pool is generated, and is of a temporary nature. The subroutines in the pool may be called from within the generation program.
On the other hand, the programs created via the INSERT REPORT
statement are permanent and overwrite any prior existing programs with the same name. Within the generation program, the new program may be called via a SUBMIT
statement.
Getting ready
We will use the concepts of dynamic program generation in order to show how a simple requirement may be solved. We will take as input two program names. The code of the first program will be read and then any comments or unwanted blank lines will be removed and a new program will be created by the name specified on the input screen.
How to do it...
- Two parameters,
origprog
andnewprog
, are declared for taking the input names of the existing and converted program respectively. - Internal tables,
itab
anditab2
, are also declared for storing the source code of the original program and converted program respectively. These are based onstring
type. A work area ofstring
type is also declared along with a temporary variabletempwa
. - The
read report
is then used with the original program name and the internal table,itab
. We will check the value ofsy-subrc
in order to proceed further. - The
LOOP
statement is then added for readingitab
intowa
. TheAPPEND
statement is used in conjunction with theIF
andSPLIT
statements in order to process all non-blank lines without having asterisk at the beginning. TheSPLIT
statement splits the source code line under consideration at the occurrence of a quotation mark ("
). The work area,wa
, contents are then appended to the internal table,itab2
. - The
SELECT SINGLE
statement is then written in order to checknewprog
intrdir
table. - If the return code value is not equal to zero (
0
), theinsert report
statement is added and on success a message Program Converted by Name message is displayed. If the insert fails, a message Program Conversion failed message is displayed.If the return code value after the SELECT statement is equal to 0, the message saying that a program having the desired destination program name already exists, is displayed.
How it works...
The program statements collectively allow the conversion of a program. The new converted program is free from all types of comments and blank lines.
Let us see how the program works.
The selection screen displays two fields for taking input for Original Program that is to be converted and New Program that is to be generated.
For example, we can enter the Original Program value as YTEST_TO_BE_GENERATED
whereas the new converted program is YTEST_TO_BE_GENERATED3
.
The code of the YTEST_TO_BE_EXECUTED program is read into the itab internal table using the READ REPORT
statement.
The loop is then run, and any blank lines are removed. Also the lines with asterisk (*) are removed, and anything written after the quotation mark (") is ignored. The new lines are appended to the itab2 internal table.
The last step is to check the trdir
table's program directory. The SELECT
statement checks the table to see if a program already exists with the new program name entered by the user. If a program already exists, a message is displayed saying "Program not converted since destination program already exists". This is necessary to avoid overwriting of the existing program. If the name is unique, the INSERT REPORT
statement is called and the new program is generated.
There's more...
While using the insert
statement, the CX_SY_WRITE_SRC_LINE_TOO_LONG
exception may occur if the program line to be inserted is too long. We may catch the exception using the try
and catch
statements as shown in the following screenshot:
See also
- http://help.sap.com/abapdocu_731/en/abendynamic_prog_technique_guidl.htm
- http://help.sap.com/abapdocu_70/en/ABAPSYNTAX-CHECK_FOR_ITAB.htm
- ABAP Keyword Documentation available at http://help.sap.com/abapdocu_702/en/abenabap.htm
- SAP Advanced ABAP Course
bc402
- Determining the Attributes of Data Objects available at http://help.sap.com/saphelp_470/helpdata/en/fc/eb3145358411d1829f0000e829fbfe/content.htm