RTTI
Since the introduction of ABAP Objects, a class-based concept has been developed, called Run Time Type Identification (RTTI). RTTI determines type attributes at runtime. RTTI includes all ABAP types and covers all the functions of the now obsolete statements DESCRIBE FIELD and DESCRIBE TABLE. RTTI includes a description class for each type with special attributes for special type attributes.
The class hierarchy of the description classes corresponds to the hierarchy of types in ABAP Objects. In addition, the description classes for complex types, references, classes, and interfaces have special methods that you can use to specify references to subtypes. You can use these methods to navigate through a compound type to all its subtypes.
RTTI – Methods and Attributes of the Root Class
To obtain a reference to a description object of a type, use the static methods of the class CL_ABAP_TYPEDESCR or the navigation methods of the special description class. The description objects are then created from one of the subclasses. At runtime, exactly one description object exists for each type. The attributes of the description object contain information on the attributes of the type.
Structured Type RTTI Descriptions
The example in the figure shows how to identify the attributes of a structure using the subclass CL_ABAP_STRUCTDESCR from RTTI.
To identify the attributes of a structure, we first define a reference to the appropriate description class. The description class has a COMPONENTS attribute that you can use to describe the components of the relevant structure. Because the COMPONENTS attribute is an internal table, you also need to define a work area with a compatible line type.
The functional method call provides the reference to the description instance of the structure that you want to query.
The abstract class CL_ABAP_TYPEDESCR contains the static method DESCRIBE_BY_DATA. Its returning parameter is typed as a reference to this superclass. However, since the actual parameter is a reference to the subclass CL_ABAP_STRUCTDESCR, you need to assign the object using a down-cast.
You can then access the attributes of the description instance in any form. In this example, the program displays the component names as column headers. (For clarity, we have omitted the formatting options.)
Object Type RTTI Descriptions
In our previous business example with the travel agency and its business partner, we specified that an instance of the vehicle rental class (LCL_RENTAL) reacts to an event by including the vehicle instance that triggered the event in a list. The triggering instances include both buses (LCL_BUS) and trucks (LCL_TRUCK).
To extend the example, assume that the vehicle rental company is only interested in buses. The SENDER parameter of the event handler method contains the reference to the triggering vehicle instance. Its dynamic object type must be analyzed to determine whether the vehicle in question is a bus or a truck.
A functional RTTI method call returns the reference to the description instance of the transferred vehicle instance.
The abstract class CL_ABAP_TYPEDESCR has DESCRIBE_BY_OBJECT_ REF method. You type DESCRIBE_BY_OBJECT_REF returning parameter as a reference to CL_ABAP_TYPEDESCR. However, since you type the actual parameter GO_DESCR on the subclass CL_ABAP_CLASSDESCR, you need to assign the object using a down-cast.
Now, you can access the attributes of the description instance in any way. The functional method GET_RELATIVE_NAME supplies the class name.
Note: It is possible to query object type attributes at run time without using RTTI classes. For example, you can use a down-cast assignment from SENDER to a reference variable that has a LCL_BUS static type. This results in a runtime error for vehicle instances that are not buses. You can then catch this runtime error. In this case, the fact that a runtime error was not triggered is the criterion for including a vehicle into the vehicle list.