RTTI, RTTC and Dynamic Programming
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
Most ABAP code is static: the names of the variables, the columns of a table and the table a SELECT reads are all
written in the source and checked when the program is compiled. Sometimes that is not enough. A program that lists any
table the user names, that writes a generic export for every structure, or that picks a class from a configuration
entry does not know the names until it runs. Dynamic programming is the set of ABAP techniques that decide those
things at runtime. This page explains them in plain words: how a field symbol or a data reference points at data whose
type you do not know, how the runtime type services read a type (RTTI) and build a new one (RTTC), how statements take
a name from a variable, and why input from outside must never go into such a statement unchecked. It is written against
Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20 (2608). Field symbols, data references, the type
description classes and the dynamic forms of ASSIGN, SORT, SELECT and CALL METHOD are part of the language in
ABAP Cloud and in classic Standard ABAP, on premise and in the Public Edition alike. DESCRIBE FIELD and the program
generation statements belong to classic Standard ABAP only; the ABAP Cloud Keyword Documentation has no entry for them.
Before you read
-
Data Types and Data Objects: structures, table types, generic types such as
anyand the reference types. -
Internal Tables: table kinds and keys; the examples create tables at runtime.
-
Constructor Expressions:
VALUE,REFandCASTare used throughout. -
Classes and Objects and Exceptions: the type description classes are ordinary classes, and the dynamic statements raise exceptions that you catch.
Key terms
-
Dynamic programming: using names, types or whole statement parts that are only known while the program runs.
-
Field symbol: a placeholder written
<name>that you point at a memory area withASSIGN; using it works on the data it points to. -
Data reference: a variable that holds the address of a data object;
ref->*reaches the data. -
Anonymous data object: a data object created with
CREATE DATAorNEWthat has no name, only a reference to it. -
RTTI: runtime type identification, reading the properties of a type while the program runs.
-
RTTC: runtime type creation, building a new data type while the program runs.
-
Type description object: the one object per type that carries its properties, created by the static methods of the type description classes.
-
Dynamic token: a part of a statement given as the content of a variable in parentheses, such as the table name in
SELECT * FROM (name).
Static and dynamic
A data object declared with DATA claims TYPE STANDARD TABLE OF zexp_claim has its name and its type fixed at compile
time. The compiler knows every column, so it rejects a misspelt component before the program ever runs. A dynamic
statement gives up that help. SELECT * FROM (table_name) reads whichever table the variable table_name holds when the
statement runs, and the compiler can neither check the name nor know the shape of the result.
The Keyword Documentation lists the costs of this freedom, and they are worth remembering before you reach for it:
-
errors that the compiler would catch, such as a wrong column name, surface as runtime errors or exceptions;
-
the checks happen at runtime and take time;
-
code with dynamic parts is harder to test, because the possible inputs multiply;
-
if the dynamic part comes from outside the program, it is a security risk (see below).
So the rule of thumb is to stay static as long as the problem allows it, and to go dynamic only for the part that really varies, such as a generic tool that must handle any structure.
Field symbols and data references
Both let you work with data without naming it in the source.
A field symbol is a name in angle brackets, for example <row>. You declare it with FIELD-SYMBOLS or inline, and you
point it at a data object with ASSIGN. After a successful ASSIGN, writing <row>-amount or <row> = ... reads or
changes the data it points to. It does not copy. Typing a field symbol with a generic type such as any lets it point at
a variable of any type. LOOP AT itab ASSIGNING FIELD-SYMBOL(<row>) points the field symbol at each row in turn, so
changing it changes the table row itself.
A data reference is an ordinary variable that holds an address. REF #( var ) creates one, NEW type( ) creates an
anonymous data object and returns a reference to it, and ref->* (the dereferencing operator) reaches the data. A
reference typed TYPE REF TO data can point at data of any type, which is how a method returns "a table whose shape I
decided at runtime". To work on the data behind such a reference you assign it to a field symbol:
ASSIGN ref->* TO <fs>.
The two are used together: the reference carries the data around, and the field symbol is the handle you use to read and change it.
The following example uses both. The class lcl_dump has one method that prints every component of a structure it has
never seen, by asking RTTI for the component names (explained in the next section) and reading each one with
ASSIGN COMPONENT. The last part shows the difference between a field symbol into a table and a reference to a row.
It runs in the local playground and prints the three components of the first structure, the three of the second, and
then first row amount: 20.00, second: 40.00.
REPORT zrtti_b.
TYPES: BEGIN OF ty_item,
claim_id TYPE c LENGTH 10,
item_no TYPE i,
amount TYPE p LENGTH 8 DECIMALS 2,
END OF ty_item.
CLASS lcl_dump DEFINITION.
PUBLIC SECTION.
CLASS-METHODS show IMPORTING row TYPE any.
ENDCLASS.
CLASS lcl_dump IMPLEMENTATION.
METHOD show.
FIELD-SYMBOLS <value> TYPE any.
DATA(descr) = CAST cl_abap_structdescr( cl_abap_typedescr=>describe_by_data( row ) ).
LOOP AT descr->components INTO DATA(component).
ASSIGN COMPONENT component-name OF STRUCTURE row TO <value>.
WRITE / |{ component-name } = { <value> }|.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
DATA item TYPE ty_item.
item = VALUE #( claim_id = 'C-1001' item_no = 2 amount = '35.00' ).
lcl_dump=>show( item ).
" the same method also works for a structure of a different shape
DATA(other) = VALUE ty_item( claim_id = 'C-2002' item_no = 1 amount = '8.40' ).
lcl_dump=>show( other ).
" a field symbol points at a row inside a table; a data reference holds a pointer in a variable
DATA items TYPE STANDARD TABLE OF ty_item WITH EMPTY KEY.
items = VALUE #( ( claim_id = 'C-1001' item_no = 1 amount = '10.00' ) ( claim_id = 'C-1001' item_no = 2 amount = '20.00' ) ).
LOOP AT items ASSIGNING FIELD-SYMBOL(<row>).
<row>-amount = <row>-amount * 2.
ENDLOOP.
DATA(first_row) = REF #( items[ 1 ] ).
WRITE / |first row amount: { first_row->amount }, second: { items[ 2 ]-amount }|.
Two details of ASSIGN matter for dynamic code. The static form ASSIGN var TO <fs> needs no check, but the dynamic
forms set a return code: ASSIGN COMPONENT name OF STRUCTURE struc TO <fs> and ASSIGN ref->(name) TO <fs> set sy-subrc, which is 4 when
the component does not exist. The name or position of the component may be a variable, which is the whole point.
Test sy-subrc after every dynamic ASSIGN and do not use the field symbol when it is not assigned, because using an
unassigned field symbol stops the program with a runtime error.
Runtime type identification (RTTI)
RTTI answers the question "what type is this?" while the program runs. ABAP keeps one type description object per
type, and the objects are instances of a family of system classes, the type description classes. They form an
inheritance tree that mirrors the ABAP types, shown below. CL_ABAP_TYPEDESCR at the top offers the static methods that
return a type description object; the subclasses add the properties of each kind of type.
The procedure is always the same two steps.
-
Call a static method of
CL_ABAP_TYPEDESCRto get a type description object.describe_by_datatakes a data object,describe_by_nametakes a type name (it raises a classic exception when the type does not exist),describe_by_data_reftakes a data reference anddescribe_by_object_reftakes an object reference. The result is typedREF TO cl_abap_typedescr, so it already gives the general facts: the attributekind(is it elementary, structure, table, reference, class or interface),type_kind(the exact built-in type, for exampleC,I,Porgfor a string) andabsolute_name. -
To reach the properties of one kind, cast down to the matching subclass with
CAST. A structure description (cl_abap_structdescr) has the attributecomponents, a table with the name and type of every component. A table description (cl_abap_tabledescr) hastable_kindand the methodget_table_line_type( ). An elementary description (cl_abap_elemdescr) haslengthanddecimals. A class description (cl_abap_classdescr) lists methods and attributes.
The comparison constants cl_abap_typedescr=>kind_struct, kind_table and the others avoid writing the one-letter
codes yourself. The following example reads an elementary data object, a structure and a table of claim items. It runs
in the local playground and prints the lines shown after the listing.
REPORT zrtti_a.
TYPES: BEGIN OF ty_item,
claim_id TYPE c LENGTH 10,
item_no TYPE i,
amount TYPE p LENGTH 8 DECIMALS 2,
END OF ty_item.
TYPES ty_items TYPE STANDARD TABLE OF ty_item WITH EMPTY KEY.
DATA item TYPE ty_item.
DATA items TYPE ty_items.
DATA note TYPE string VALUE `hello`.
" An elementary data object: one description object, general information
DATA(descr) = cl_abap_typedescr=>describe_by_data( note ).
WRITE / |note: kind { descr->kind }, type kind { descr->type_kind }|.
" A structure: cast to the structure class to reach its components
DATA(struct_descr) = CAST cl_abap_structdescr( cl_abap_typedescr=>describe_by_data( item ) ).
WRITE / |item is a structure: { xsdbool( struct_descr->kind = cl_abap_typedescr=>kind_struct ) }|.
LOOP AT struct_descr->components INTO DATA(component).
WRITE / | { component-name } has type kind { component-type_kind }|.
ENDLOOP.
" An internal table: table kind and the type of one row
DATA(table_descr) = CAST cl_abap_tabledescr( cl_abap_typedescr=>describe_by_data( items ) ).
WRITE / |items is a table of kind { table_descr->table_kind }|.
DATA(line_descr) = table_descr->get_table_line_type( ).
WRITE / |a row is of kind { line_descr->kind }|.
Real output of the listing in the local playground:
note: kind E, type kind g item is a structure: X CLAIM_ID has type kind C ITEM_NO has type kind I AMOUNT has type kind P items is a table of kind S a row is of kind S
The first line shows E, the kind code of an elementary type, and g, the type code of a string. For the table, S in
table_kind means a standard table, and the last S is the kind of the row, a structure; a table itself has the kind T.
Component names come back in capital letters.
Describing a type by its name works for global types, such as a data element of the ABAP Dictionary or a global class,
and for types declared in the program the call runs in. The local playground resolved the global table ZEXP_CLAIM
but stopped with a "type not found" error for describe_by_name( 'TY_ITEM' ), a type declared in the program, so
that call is shown without output further down.
Runtime type creation (RTTC)
RTTI reads a type that exists. RTTC builds one that does not exist in the source. You need it when the shape of the data
depends on input: a generic export whose columns come from a configuration, or a result table whose columns the user
picked. The classes that read types also create them, through static create and get_* methods.
Building a table type at runtime takes four steps, and the next listing follows them:
-
Describe the columns as a list of name and type pairs, using the
get_*methods ofcl_abap_elemdescrfor the built-in types, for exampleget_c( 12 ),get_i( ),get_string( )orget_p( p_length = ... p_decimals = ... ). The list has the typecl_abap_structdescr=>component_table. -
Create the structure type with
cl_abap_structdescr=>create( components ), then the table type withcl_abap_tabledescr=>create( struct_descr ). The results are type description objects, and the types exist from now on for the rest of the program run. -
Create the data objects with
CREATE DATA ref TYPE HANDLE descr.HANDLEtakes a reference to a type description object, and the reference variable then points at an anonymous data object of that type. -
Work on the new data through a field symbol, assigning
ref->*to it and reading components by name withASSIGN COMPONENT.
The example builds a table of expense categories with two columns that are named only in the VALUE list, fills two
rows through field symbols and writes them back. It runs in the local playground and prints rows: 2, then
TRAVEL: 120.50 and MEALS: 35.00.
REPORT zrtti_c.
" 1. Describe the columns of a table that does not exist in the source code
DATA(components) = VALUE cl_abap_structdescr=>component_table(
( name = 'CATEGORY' type = cl_abap_elemdescr=>get_c( 12 ) )
( name = 'AMOUNT' type = cl_abap_elemdescr=>get_p( p_length = 8 p_decimals = 2 ) ) ).
" 2. Create the structure type and the table type as type description objects
DATA(struct_descr) = cl_abap_structdescr=>create( components ).
DATA(table_descr) = cl_abap_tabledescr=>create( struct_descr ).
" 3. Create the data objects from the type description objects
DATA table_ref TYPE REF TO data.
DATA row_ref TYPE REF TO data.
CREATE DATA table_ref TYPE HANDLE table_descr.
CREATE DATA row_ref TYPE HANDLE struct_descr.
FIELD-SYMBOLS <table> TYPE STANDARD TABLE.
FIELD-SYMBOLS <row> TYPE any.
FIELD-SYMBOLS <cell> TYPE any.
ASSIGN table_ref->* TO <table>.
ASSIGN row_ref->* TO <row>.
" 4. Fill and read the new table through field symbols and component names
ASSIGN COMPONENT 'CATEGORY' OF STRUCTURE <row> TO <cell>.
<cell> = 'TRAVEL'.
ASSIGN COMPONENT 'AMOUNT' OF STRUCTURE <row> TO <cell>.
<cell> = '120.50'.
INSERT <row> INTO TABLE <table>.
ASSIGN COMPONENT 'CATEGORY' OF STRUCTURE <row> TO <cell>.
<cell> = 'MEALS'.
ASSIGN COMPONENT 'AMOUNT' OF STRUCTURE <row> TO <cell>.
<cell> = '35.00'.
INSERT <row> INTO TABLE <table>.
WRITE / |rows: { lines( <table> ) }|.
LOOP AT <table> ASSIGNING FIELD-SYMBOL(<line>).
ASSIGN COMPONENT 1 OF STRUCTURE <line> TO <cell>.
DATA(category) = |{ <cell> }|.
ASSIGN COMPONENT 'AMOUNT' OF STRUCTURE <line> TO <cell>.
WRITE / |{ category }: { <cell> }|.
ENDLOOP.
A shorter route exists when the type already has a name. CREATE DATA ref TYPE (type_name) creates an anonymous data
object of the type whose name is in the variable, and the type description object is not needed. Use HANDLE when the
type is new, and the name form when it exists. Component types may also be taken from existing type description
objects, for example CAST cl_abap_datadescr( cl_abap_typedescr=>describe_by_name( 'LAND1' ) ) for a data element of the
ABAP Dictionary.
Dynamic statements
Besides data, many ABAP statements accept a name or a condition from a variable. The variable goes in parentheses, and the content is read when the statement runs. The table below lists the forms that a newcomer meets most often.
| Statement | Dynamic part | Available in |
|---|---|---|
|
the component, or the data object, by name or position |
Cloud and Standard ABAP |
|
the sort column |
Cloud and Standard ABAP |
|
the whole condition, as text |
Cloud and Standard ABAP |
|
any clause of an ABAP SQL statement |
Cloud and Standard ABAP |
|
the class and the method, by name |
Cloud and Standard ABAP |
The next listing shows three of them on the running example: a sort whose column name is a variable, a SELECT whose
table name is a variable and a WHERE condition built as text, and the class method cl_abap_dyn_prg=>quote that makes a
value safe to paste into that text (explained in the next section). It runs in the local playground and prints the
three sorted rows, claims: 2, the condition text claim_id = 'C-1002', matching claims: 1 and the quoted value
'O''Neil', in which the apostrophe has been doubled.
REPORT zrtti_e.
TYPES: BEGIN OF ty_item,
category TYPE c LENGTH 10,
amount TYPE i,
END OF ty_item.
DATA items TYPE STANDARD TABLE OF ty_item WITH EMPTY KEY.
items = VALUE #( ( category = 'TRAVEL' amount = 3 ) ( category = 'MEALS' amount = 9 ) ( category = 'HOTEL' amount = 5 ) ).
" a dynamic sort: the column name arrives in a variable
DATA sort_column TYPE string VALUE 'AMOUNT'.
SORT items BY (sort_column) DESCENDING.
LOOP AT items INTO DATA(item).
WRITE / |{ item-category } { item-amount }|.
ENDLOOP.
" a dynamic read of the database table ZEXP_CLAIM: the table name arrives in a variable
INSERT zexp_claim FROM @( VALUE zexp_claim( claim_id = 'C-1001' status = 'N' amount = 5 ) ).
INSERT zexp_claim FROM @( VALUE zexp_claim( claim_id = 'C-1002' status = 'A' amount = 7 ) ).
DATA table_name TYPE string VALUE 'ZEXP_CLAIM'.
DATA claims TYPE STANDARD TABLE OF zexp_claim WITH EMPTY KEY.
SELECT * FROM (table_name) INTO TABLE @claims.
WRITE / |claims: { lines( claims ) }|.
" a dynamic WHERE condition: a value pasted into the text is quoted first, which doubles any apostrophe
DATA wanted TYPE c LENGTH 10 VALUE 'C-1002'.
DATA(condition) = |claim_id = { cl_abap_dyn_prg=>quote( wanted ) }|.
WRITE / condition.
SELECT * FROM zexp_claim WHERE (condition) INTO TABLE @claims.
WRITE / |matching claims: { lines( claims ) }|.
WRITE / cl_abap_dyn_prg=>quote( 'O''Neil' ).
The listing writes the SELECT result into a table typed with zexp_claim. The playground reported an unknown type
for the inline form INTO TABLE @DATA(rows) together with a dynamic table name, so the example types the target; the
Keyword Documentation allows the inline form. When you do not know the table at all, the target can be a table created
by RTTC (see above) behind a field symbol typed STANDARD TABLE.
Sorting internal tables by a column given in a variable is described in its own topic of the Keyword Documentation.
Calling a method by name
CALL METHOD ref->(method_name) calls a method whose name is in a variable, and CREATE OBJECT ref TYPE (class_name)
creates an object of the class named in a variable. For the class, the Keyword Documentation says the variable must
contain the name in capital letters when the statement runs. CALL METHOD is otherwise
obsolete: the Keyword Documentation says that every use of it other than the dynamic one is obsolete, so you write
ref->method( ) in new code and keep CALL METHOD for the dynamic call. The reference for the created object is
typed TYPE REF TO object, which can hold any object; its methods are therefore only reachable by name.
The example creates a greeter object from a class name in a variable, calls its method by name and writes the
result. It runs in the local playground and prints Hello, Ada!. In a system you would normally name a global class.
REPORT zrtti_d.
CLASS lcl_greeter DEFINITION.
PUBLIC SECTION.
METHODS greet IMPORTING name TYPE string RETURNING VALUE(result) TYPE string.
ENDCLASS.
CLASS lcl_greeter IMPLEMENTATION.
METHOD greet.
result = |Hello, { name }!|.
ENDMETHOD.
ENDCLASS.
DATA object TYPE REF TO object.
DATA class_name TYPE string VALUE 'LCL_GREETER'.
DATA method_name TYPE string VALUE 'GREET'.
DATA result TYPE string.
CREATE OBJECT object TYPE (class_name).
CALL METHOD object->(method_name) EXPORTING name = 'Ada' RECEIVING result = result.
WRITE / result.
When the class is not known only by a name but you can choose between a few, an interface and a factory (see Interfaces and Inheritance) are the better tool: the compiler checks the call, and a test can replace the class. Reach for a dynamic call when the class name truly comes from configuration.
Input from outside: security
A dynamic statement that contains text from outside the program is the classic way to attack an application. Suppose a
screen lets the user type a claim number and the program builds claim_id = ' followed by the typed text followed by
'. A user who types an apostrophe and more ABAP SQL ends the value early and adds a condition of their own; this is
an SQL injection. The Keyword Documentation names the risks for the dynamic parts of ABAP SQL: access to tables or columns
the user should not read, manipulation of a dynamic WHERE condition, a fully dynamic SELECT and manipulation of a
set expression. The same holds for any other dynamic statement.
The rules that follow from it are short.
-
Never paste unchecked input into a dynamic token. Prefer to name an ABAP variable in the condition (
claim_id = @wanted), because then the content of the variable is a value and never part of the statement. The Keyword Documentation says this is the more secure form. The local playground does not evaluate a host variable in a dynamic condition, so the listing above uses the quoting route instead. -
If a value must be pasted into the text, quote it with
cl_abap_dyn_prg=>quote, which wraps it in single quotes and doubles any apostrophe inside. -
Check names against an allowlist.
cl_abap_dyn_prg=>check_allowlistcompares a value with the allowed ones,check_table_name_strchecks that a table name is a table of an allowed package, and both raise an exception that you catch and treat as "wrong input". -
The built-in function
escapeescapes for other targets, for example for HTML or JavaScript output.
The following sketch uses the checks before a dynamic read. It requires an SAP system; not run here, because the
playground has neither the allowlist class nor the package information it needs. ZEXP_CLAIMS stands for the
package of the expense claims application.
DATA(input_table) = `zexp_claim`. " pretend this arrived from a screen
TRY.
DATA(table_name) = cl_abap_dyn_prg=>check_table_name_str(
val = to_upper( input_table )
packages = `ZEXP_CLAIMS` ).
SELECT * FROM (table_name) INTO TABLE @DATA(rows) UP TO 100 ROWS.
CATCH cx_abap_not_a_table cx_abap_not_in_package.
" reject the input; never fall back to reading an unchecked name
ENDTRY.
DATA(input_column) = `status`.
TRY.
DATA(column) = cl_abap_dyn_prg=>check_allowlist(
val = to_upper( input_column )
allowlist_str = `CLAIM_ID,STATUS,AMOUNT` ).
SELECT (column) FROM zexp_claim INTO TABLE @DATA(values).
CATCH cx_abap_not_in_allowlist.
" reject the input
ENDTRY.
The general section of the Keyword Documentation on risks from outside input lists the other dynamic statements to guard in the same way.
What the local playground cannot run
The playground covers RTTI, RTTC and most dynamic statements, as the listings show. A few forms in this page fail
there and need a system, so they are shown without output. The code below requires an SAP system; not run here. It
collects: describing a global type by name, a dynamic WHERE in LOOP, and ASSIGN to a variable by name.
" type description of a data element of the ABAP Dictionary, then its properties
DATA(land_descr) = CAST cl_abap_elemdescr( cl_abap_typedescr=>describe_by_name( 'LAND1' ) ).
DATA(is_ddic) = land_descr->is_ddic_type( ).
" a dynamic WHERE condition on an internal table
DATA(cond_loop) = `amount > 5`.
LOOP AT items INTO DATA(item) WHERE (cond_loop).
" ...
ENDLOOP.
" ASSIGN to a data object by its name
DATA claim_count TYPE i VALUE 3.
FIELD-SYMBOLS <any> TYPE any.
ASSIGN ('CLAIM_COUNT') TO <any>.
IF sy-subrc = 0.
" <any> points at claim_count
ENDIF.
Legacy: DESCRIBE FIELD and generated programs
Before the type description classes existed, a program learned about a variable with DESCRIBE FIELD. It returns a few
properties of an elementary data object into variables: the one-character type, the length, the number of decimals, the
output length and similar. The Keyword Documentation says the statement is mainly suitable for elementary types and that
for structures and tables only elementary properties can be read; for the static or dynamic type of a reference
variable and for everything else the type description classes are to be used. The statement exists only in Standard
ABAP; ABAP Cloud has no entry for it. DESCRIBE TABLE for the line count is likewise replaced by lines( ).
The listing shows the same facts both ways. It runs in the local playground and prints
DESCRIBE FIELD: type P, length 8, decimals 2 and RTTI: type P, length 8, decimals 2.
REPORT zrtti_f.
DATA amount TYPE p LENGTH 8 DECIMALS 2.
" the classic statement, valid in Standard ABAP only
DATA type_id TYPE c LENGTH 1.
DATA bytes TYPE i.
DATA places TYPE i.
DESCRIBE FIELD amount TYPE type_id LENGTH bytes DECIMALS places.
WRITE / |DESCRIBE FIELD: type { type_id }, length { bytes }, decimals { places }|.
" the same facts from the type description object, valid in ABAP Cloud as well
DATA(descr) = CAST cl_abap_elemdescr( cl_abap_typedescr=>describe_by_data( amount ) ).
WRITE / |RTTI: type { descr->type_kind }, length { descr->length }, decimals { descr->decimals }|.
The most dynamic technique of all is generating ABAP source at runtime. INSERT REPORT writes a program into the
repository from an internal table of source lines, and GENERATE SUBROUTINE POOL compiles a temporary subroutine pool
that lives only in the current internal session. Both belong to Standard ABAP; the ABAP Cloud Keyword Documentation has
no entry for either. Generated code cannot be checked before it is generated, hides what the program really does from
every reader and tool, and is as dangerous as any dynamic statement if any part comes from outside, so new code does
not use it and a maintainer meets it in old programs only.
Older material you may meet
Books and programs written for AS ABAP 7.4 and 7.5, for example the 1st edition of Complete ABAP, which has a chapter on field symbols, data references, the runtime type services, dynamic tokens and program generation, predate ABAP Cloud. The ideas on this page have not changed, but the surface has.
-
Field symbols are declared at the top with
FIELD-SYMBOLS <fs> TYPE ANY; todayASSIGNING FIELD-SYMBOL(<fs>)declares one at its first use. -
A downcast of a type description object is written
descr ?= cl_abap_typedescr=>describe_by_data( x )after a separate declaration; todayCASTdoes it in one expression, as in the listings. -
Methods are called with
CALL METHOD ... EXPORTING ... IMPORTING, and objects are created withCREATE OBJECT; the functional call andNEWare the modern forms, andCALL METHODstays only for the dynamic method name. -
Types of single variables are read with
DESCRIBE FIELD, which ABAP Cloud does not have; use the type description classes. -
Programs are sometimes generated at runtime with
INSERT REPORTorGENERATE SUBROUTINE POOL; this exists only in Standard ABAP and is not recommended for new code.
Where an older book and the Keyword Documentation differ, follow the Keyword Documentation of the release you work on.
Summary
-
Dynamic programming decides names, types or statement parts at runtime. It gives up compile-time checks, so use it only for the part that really varies.
-
A field symbol is a handle on a memory area; a data reference is a variable holding an address that
->*dereferences.ASSIGNconnects a field symbol to data, and the dynamic forms setsy-subrc. -
RTTI reads a type: get a type description object with
describe_by_data,describe_by_nameand friends, then cast to the subclass that has the property you need. -
RTTC builds a type: describe the components, create the structure and table types and use
CREATE DATA ... TYPE HANDLEto create the data. -
Dynamic tokens let a variable supply a name or a condition. Input from outside must be passed as a variable value, quoted with
cl_abap_dyn_prg=>quote, or checked against an allowlist, never pasted as text. -
DESCRIBE FIELDand program generation are legacy and exist only in Standard ABAP.
References
-
ABAP Keyword Documentation: SELECT, dynamic token specification
-
ABAP Keyword Documentation: Sorting Internal Tables Dynamically
-
ABAP Keyword Documentation: SQL Injections Using Dynamic Tokens in ABAP SQL
-
ABAP Keyword Documentation: Security Risks Caused by Input from Outside
-
Keyword Documentation for Standard ABAP: Dynamic Programming Techniques
-
Keyword Documentation for Standard ABAP: GENERATE SUBROUTINE POOL
-
Complete ABAP (SAP PRESS), Kiran Bandari; the copy consulted is the 1st edition