SAP NetWeaver AS ABAP Release 750, ©Copyright 2016 SAP AG. All rights reserved.
ABAP - Keyword Documentation → ABAP - Reference → Program Editing → Dynamic Program Editing → Source Code →
GENERATE SUBROUTINE POOL
Syntax
GENERATE SUBROUTINE POOL itab NAME prog [error_handling].
Effect
This statement generates a temporary subroutine pool. The source code of the subroutine pool is taken from the internal table itab. The generated subroutine pool is stored internally in the current internal session. The eight-character name of the temporary subroutine pool is assigned to the variable prog. The following can be specified for prog:
For itab, only a standard table without secondary table keys is permitted. The row type of itab must be character-like. A line of source code in itab can have no more than 255 characters (if the row type has a fixed length, trailing blanks are ignored). In an internal session, a maximum of 36 temporary subroutine pools can be created.
If the source code in itab has a syntax error, the subroutine pool is not generated and initialized using prog. The addition error_handling can be used to analyze syntax errors and generation errors. For the syntax check, the switch configuration of Switch Framework is used in the state it had when the current transaction was called.
If an exception is raised when the subroutine pool is generated, the runtime error is handled internally so that no programs are terminated. Instead, sy-subrc is set to the value 8. However, there is still a database rollback and the corresponding short dump is saved as normal. The addition SHORTDUMP-ID can be used to determine the ID of the runtime error.
In the source code of the subroutine pool, subroutines can be called from all programs that are loaded
in the same internal session by specifying the program name prog using the
statement PERFORM. When a subroutine
is called for the first time in the subroutine pool, this is loaded into the internal session, and the
event LOAD-OF-PROGRAM is triggered.
System Fields
sy-subrc | Meaning |
0 | Generation was successful. |
4 | The source code contains a syntax error. |
8 | A generation error occurred The resulting runtime error was handled internally. |
If a runtime error occurs during the generation process (sy-subrc has the value 8), a database rollback is executed as usual.
Programming Guideline
Security Note
If used wrongly, dynamic programming techniques can present a serious security risk. Any dynamic content that is passed to a program from the outside must be checked thoroughly or escaped before being used in dynamic statements. This can be done using the system class CL_ABAP_DYN_PRG or the predefined function escape. See ABAP Command Injections.
Notes
Example
Creates and generates (dynamically) a subroutine pool that implements the event block LOAD-OF-PROGRAM and two subroutines. Depending on the return code sy-subrc, a subroutine is called or a message is issued.
DATA tab TYPE STANDARD TABLE OF string WITH EMPTY KEY.
tab = VALUE #(
( `PROGRAM subpool.`
)
( `DATA spfli_tab TYPE TABLE OF spfli.` )
( `LOAD-OF-PROGRAM.`
)
( ` SELECT *` &
` FROM spfli` &
` INTO TABLE @spfli_tab.` )
( `FORM loop_at_tab.` )
( ` DATA spfli_wa TYPE spfli.` )
( ` LOOP AT spfli_tab INTO spfli_wa.` )
( ` PERFORM evaluate_wa USING spfli_wa.` )
( ` ENDLOOP.`
)
( `ENDFORM.`
)
( `FORM evaluate_wa USING l_wa TYPE spfli.` )
( ` cl_demo_output=>write_data( l_wa ).` )
( `ENDFORM.`
) ).
GENERATE SUBROUTINE POOL tab NAME DATA(prog)
MESSAGE
DATA(mess)
SHORTDUMP-ID DATA(sid).
IF sy-subrc = 0.
PERFORM ('LOOP_AT_TAB') IN PROGRAM (prog) IF FOUND.
cl_demo_output=>display( ).
ELSEIF sy-subrc = 4.
MESSAGE mess TYPE 'I'.
ELSEIF sy-subrc = 8.
MESSAGE sid TYPE 'I'.
ENDIF.
Example
Creates and generates (dynamically) a subroutine pool that implements a local class. The static method meth of the class can be called using the absolute type name of the class.
DATA itab TYPE TABLE OF string.
DATA class TYPE string.
itab = VALUE #(
( `program.`
)
( `class main definition.` )
( ` public section.` )
( ` class-methods meth.` )
( `endclass.` )
( `class main implementation.` )
( ` method meth.` )
( ` message 'Test' type 'I'.` )
( ` endmethod.`
)
( `endclass.`
) ).
GENERATE SUBROUTINE POOL itab NAME DATA(prog).
class = `\PROGRAM=` && prog && `\CLASS=MAIN`.
CALL METHOD (class)=>meth.
Example
Creates and generates (dynamically) a subroutine pool that implements a local class. The class is instantiated using its absolute type name, and the instance method meth is called dynamically.
DATA itab TYPE TABLE OF string.
DATA class TYPE string.
DATA oref TYPE REF TO object.
itab = VALUE #(
( `program.` )
( `class main definition.` )
( ` public section.` )
( ` methods meth.` )
( `endclass.` )
( `class main implementation.` )
( ` method meth.` )
( ` message 'Test' type 'I'.` )
( ` endmethod.`
)
( `endclass.`
) ).
GENERATE SUBROUTINE POOL itab NAME DATA(prog).
class = `\PROGRAM=` && prog && `\CLASS=MAIN`.
CREATE OBJECT oref TYPE (class).
CALL METHOD oref->('METH').
Example
See also Program Generation.
Handleable Exceptions
CX_SY_GENERATE_SUBPOOL_FULL
CX_SY_GEN_SOURCE_TOO_WIDE