Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
One of the outstanding features of ABAP is the capability of dynamic programming. In particular, you can use

• Generic types
• Dynamic types
• Field symbols
• References
• Dynamic tokens in statements
• Dynamic invoke of procedures
• Run Time Type Services
• Program generation

What about dynamic ABAP Objects? Well, apart from program generation all dynamic features listed above are readily available in ABAP Objects too. Especially, you can use:

• Dynamic attribute access via field symbols:

ASSIGN { oref->(attr_name) }
| { {class|(class_name)}=>{attr|(attr_name)} } TO ...


• Dynamic method invoke:

CALL METHOD {(meth_name)
|oref-> (meth_name)
|(class_name)=>(meth_name)
|class=>(meth_name)
|(class_name)=>meth}
[PARAMETER-TABLE ptab]
[EXCEPTION-TABLE etab].


But what about the crown of dynamic programming, what about program generation? Isn't there a statement GENERATE CLASS POOL missing?

In the following I will show you how to circumvent this gap with two existing ingredients.

Ingredient 1 – Dynamic Subroutine Pools

The first ingredient is the well known statement

GENERATE SUBROUTINE POOL itab NAME prog ...

This 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 . But, uh, oh, subroutines? Of course we do not want to program with subroutines in ABAP Objects any longer! But don’t be blinded by the name "subroutine pool" – remember ABAP Geek 2 - ABAP Program Types : ”subroutine pools can also contain local classes and interfaces“. Why not generate subroutine pools containing fully dynamic classes and interfaces?

Remains only the question how to access the local classes or interfaces of a dynamic subroutine pool. One feasable way would be to create exactly one subroutine (as a factory) in the subroutine pool that wraps the access to ABAP Objects. Another way includes ingredient 2. Then, there is no need for subroutines at all. The following example shows the use of a wrapper subroutine.

Example 1 – Static Invoke of Static Method from Subroutine

Dynamic subroutine pool with a local class. The static method meth of the class is invoked from subroutine invoke .

DATA itab TYPE TABLE OF string.
DATA prog TYPE string.

APPEND `program .` TO itab.
APPEND `class main definition .` TO itab.
APPEND ` public section.` TO itab.
APPEND ` class-methods meth .` TO itab.
APPEND `endclass .` TO itab.
APPEND `class main implementation .` TO itab.
APPEND ` method meth.` TO itab.
APPEND ` message 'Test' type 'I' .` TO itab.
APPEND ` endmethod.` TO itab.
APPEND `endclass .` TO itab.
APPEND `form invoke .` TO itab.
APPEND ` main=>meth( ).` TO itab.
APPEND `endform .` TO itab.

GENERATE SUBROUTINE POOL itab NAME prog.

PERFORM ('INVOKE') IN PROGRAM (prog).

Ingredient 2 – Absolute Type Names

The second ingredient might be not as well known as the first ingredient. Absolute type names are structured like a path entry that specifies the context of a data type, a class or an interface. Absolute type names can be made up from following components:

• TYPE=name
• CLASS=name
• INTERFACE=name
• PROGRAM=name
• CLASS-POOL=name
• FUNCTION-POOL=name
• TYPE-POOL=name
• METHOD=name
• FORM=name
• FUNCTION=name

They can be used in all ABAP statements, where dynamic type specifications are possible. The following examples show, how you can use them to access dynamic classes.

Note
The usage of absolute type names for accessing local classes must be restricted to dynamic classes in subroutine pools. Of course the main feature of a static local class is the fact that it is not global.

Example 2 – Dynamic Invoke of Static Method

Dynamic subroutine pool with a local class. The static method meth of the class is dynamically invoked via the absolute class name.

DATA itab TYPE TABLE OF string.
DATA prog TYPE string.
DATA class TYPE string.

APPEND `program .` TO itab.
APPEND `class main definition .` TO itab.
APPEND ` public section.` TO itab.
APPEND ` class-methods meth .` TO itab.
APPEND `endclass .` TO itab.
APPEND `class main implementation .` TO itab.
APPEND ` method meth.` TO itab.
APPEND ` message 'Test' type 'I' .` TO itab.
APPEND ` endmethod.` TO itab.
APPEND `endclass .` TO itab.

GENERATE SUBROUTINE POOL itab NAME prog.

CONCATENATE `PROGRAM=` prog `CLASS=MAIN` INTO class.

CALL METHOD (class )=>meth.

Example 3 – Dynamic Invoke of Instance Method

Dynamic subroutine pool with a local class. The class main is instantiated via the absolute class name and the instance method meth of the class is dynamically invoked.

DATA itab TYPE TABLE OF string.
DATA prog TYPE string.
DATA class TYPE string.
DATA oref TYPE REF TO object.

APPEND `program .` TO itab.
APPEND `class main definition .` TO itab.
APPEND ` public section.` TO itab.
APPEND ` methods meth .` TO itab.
APPEND `endclass .` TO itab.
APPEND `class main implementation .` TO itab.
APPEND ` method meth.` TO itab.
APPEND ` message 'Test' type 'I' .` TO itab.
APPEND ` endmethod.` TO itab.
APPEND `endclass .` TO itab.

GENERATE SUBROUTINE POOL itab NAME prog.

CONCATENATE `PROGRAM=` prog `CLASS=MAIN` INTO class.

CREATE OBJECT oref TYPE (class).

CALL METHOD oref- >('METH').

Example 4 – Static Invoke of Dynamic Method

Dynamic subroutine pool with a local class. The dynamic class implements a global interface that is used to type the reference variable oref. Instead of an interface you could also inherit from a global super class to achieve static typing of the reference variable.

DATA itab TYPE TABLE OF string.
DATA prog TYPE string.
DATA class TYPE string.
DATA oref TYPE REF TO if_dynamic_frame.

APPEND `program .` TO itab.
APPEND `class main definition .` TO itab.
APPEND ` public section.` TO itab.
APPEND ` interfaces if_dynamic_frame .` TO itab.
APPEND `endclass .` TO itab.
APPEND `class main implementation .` TO itab.
APPEND ` method if_dynamic_frame~meth.` TO itab.
APPEND ` message 'Test' type 'I' .` TO itab.
APPEND ` endmethod.` TO itab.
APPEND `endclass .` TO itab.

GENERATE SUBROUTINE POOL itab NAME prog.

CONCATENATE `PROGRAM=` prog `CLASS=MAIN` INTO class.

CREATE OBJECT oref TYPE (class).

oref->meth( ).
6 Comments