Field symbols are dereferenced pointers that have ‘symbolic’ access to a data object. Therefore, data object can be accessed using the field symbol itself. Field symbols are declared using the keyword, FIELD-SYMBOLS, as shown. Angular brackets (<>) are a part of the naming convention. The field symbols that are defined using TYPE or LIKE addition statically refer to a particular type. They can also be generically defined using TYPE ANY. You can assign a data object to a field symbol using the ASSIGN statement. As shown in the slide, the field symbol <fs_int> after assignment contains the value of gv_int. When the UNASSIGN statement is used, the field symbols do not indicate any data object.
EXAMPLE: GENERIC FIELD SYMBOL IN DYNAMIC SELECT
If a field symbol is typed generically, it can be assigned to any data object irrespective of the type of data object. In this example,<fs_tab> is a field symbol of type internal table. Depending on the parameter supplied to the variable, lv_table_name, <fs_tab> should hold values from SCARR or SBOOK table. The SELECT statement is used to fill the internal table using <fs_tab>.
A field symbol assigned to a data object can be used to refer to a different data object using the CASTING addition in the ASSIGN statement. The data object then behaves as if it implicitly had the data type of the field symbol. However, if the CASTING TYPE addition is used in the ASSIGN statement, then the data object behaves as if it is explicitly specified with the type of the field symbol. After the first ASSIGN statement, the field symbol <fs> will hold current date as shown here.
DYNAMIC ACCESS TO DATA OBJECTS
As shown in the slide, any data object, including structure components, can be assigned to a field symbol. After the release of version 6.1, even static and instance attributes can be assigned to a field symbol.
DYNAMIC ACCESS TO OBJECT ATTRIBUTE AND CLASS ATTRIBUTE
Like data objects or structure components, dynamic access to the static attributes of a class or an instance of a class, can be made using field symbols, as shown here. The attribute name can be specified either in uppercase or lowercase. Even a class name can be mentioned either ways.
DYNAMIC ACCESS TO STRUCTURE COMPONENTS
We already know that structure components can be assigned to a field symbol. Using the ASSIGN statement, it is possible to access a particular component of a structure. As shown here, the syntax used for this type of assignment is ASSIGN COMPONENT variable_name OF STRUCTURE. The component of a structure can also be accessed using its position.
FULL PROCESSING OF ANY NON-NESTED, FLAT STRUCTURE
The components of a structure can be accessed using its field symbols. Let us understand this with the example shown in the slide. The variable, is_struct, is an elementary structure declared as TYPE any. The system variable, sy-index, is used to assign the components of the structure to the field symbol, ls_comp.
The condition sy-subrc <> 0 (not equal to zero) checks the ASSIGN statement. If the condition is true, control exits the loop. Otherwise, the index value is displayed using the WRITE statement.
The reference type for a reference variable is defined using the TYPE REF TO addition in the TYPES statement. It can be either fully specified or generic. The DATA statement is used when you wish to assign a data object to a reference variable that is already defined with a specified type.
Let us understand the two statements with an example.
The reference variable gty_ref_int is of the type, Integer. The data object gr_int is also of the same type, as it refers to get_ref_int. The data object GV_INT holds a value 15.
By using the statement, GET REFERENCE OF, gr_int can point to gv_int. The dereferencing operator ( → *) is used to directly access the content of the data object.
VALIDATING REFERENCE VARIABLE: LOGICAL
The statement, ref IS [NOT] BOUND, is used to check if a reference variable contains a valid reference. In the above statement, ref can be a data object or reference variable. If an object reference is involved, the logical expression returns “true” as the value. Otherwise, the value is “false.”
GENERIC DATA REFERENCES
Reference variables that are generic can be created using TYPE REF TO DATA assignment. It is not possible to directly dereference a generically typed reference variable. Such reference variables can be used during dynamic internal table call and for dynamic creation of data objects.
CAST ASSIGNMENT FOR DATA REFERENCES
When values are assigned between two reference variables of different types, it can be defined as cast assignment. As shown in the slide, lr_int and lr_date are fully typed reference variables; whereas, lr_gen is generically typed. When a generically typed reference variable is assigned to a fully typed reference variable, it is known as down cast. However, the opposite assignment is called up cast.
As seen here, down cast in the first case is type compatible. Since the assignment is not type compatible in the second case, execution of the program will lead to runtime error.
DEFERENCING GENERICALLY TYPED DATA
You can dereference generically typed reference variables only by using the ASSIGN statement. As shown in the slide, lr_data is assigned to the field symbol <fs> using the ASSIGN statement. After assignment, the field symbol points to the same data object as the data reference. Value of the data object reference can be obtained using the field symbol.
OPTIONS FOR CREATING DATA OBJECT AT RUNTIME
A data object pointing to a general data reference variable can be created dynamically using the CREATE DATA statement. As shown in the slide, when lr_spfli is assigned to lr_data, an up-cast is explicitly defined. However, the TYPE addition in a CREATE DATA statement implicitly generates an up-cast assignment. Using the LIKE addition in a CREATE DATA statement, references to an already existing data object can be specified. References to a character-type data object can be obtained by specifying the name of the object in parentheses in the CREATE DATA statement as shown here.
ACCESS TO DYNAMICALLY GENERATED DATA
As shown in the slide, CREATE DATA lr_data creates a data object whose type is unknown. Therefore, in case of dynamically created data objects, reference variable should be generic in nature. Using the ASSIGN statement, the field symbol <fs> points to the content of the data object, lr_data.
A field symbol is required in this case because
Only the ASSIGN statement can be used to dereference a reference variable.
Generically typed field symbols can be used anywhere.
EXAMPLE: DYNAMICALLY GENERATED INTERNAL TABLE AND DYNAMIC SELECT
An internal table can be created dynamically using the TYPE TABLE OF addition in the CREATE DATA statement. The line type of the internal table is defined by specifying the name of the table in parentheses, as shown in the slide. In order to display the values, you can use a generically typed field symbol in the ASSIGN statement.
CLASS HIERARCHY OF RTTI DESCRIPTION CLASSES
ABAP defines a hierarchical list of global classes that can be used. An instance of the class describes all the properties associated with the class which can be called using the public attributes and methods. Each class describes a specific category of types, for example CL_ABAP_TABLEDESCR is used to describe table types. The list is a combination of abstract classes used for inheritance, as well as those classes which can be instantiated.
RTTI CLASSES: INSTATIATION POSSIBLE
Only six of the ten RTTI classes can be instantiated and used to describe specific types. All the other classes are abstract. They cannot be instantiated. This is because they are used to centrally define the attributes and methods in several other classes (and implement them if necessary). The METHODS attribute, which contains a list of the methods, is not defined in class CL_ABAP_CLASSDESCR. It is defined in class CL_ABAP_OBJECTDESCR, as it is also needed in the same form in class CL_ABAP_INTFDESCR.
RTTI: METHOD AND ATTRIBUTES OF THE ROOT CLASS
CL_ABAP_TYPEDESCR is the root class in the hierarchy of RTTI description classes. CREATE OBJECT cannot be used to instantiate class directly. As shown in the slide, you need to call the static methods, Describe_By_name and Describe_By_data of class CL_ABAP_TYPEDESCR to create an object reference.
DESCRIBING A TYPE BASED ON ITS NAME
In order to access the attributes and methods of the respective type, you need to suitably cast the subclass with local or global data type. It is also possible to cast interfaces to the data objects.
CASING A SUITABLE REFERENCE FOR A TYPE DESCRIPTION OBJECT
If the user has no idea about the RTTI class that was instantiated, then the public instance attribute, kind, can be used. However, a downcast to a suitable subclass must be done.
DESCRIBING TYPES BASED ON DATA OBJECTS AND REFERENCES
As shown in the slide, static methods can be used to describe different reference types. The method, describe_by_data, returns description about the type of current parameter in the first example whereas, the same method returns type description of the reference variable in the second example. Similarly, the method, describe_by_object_ref, gives the type description of the object that is pointed to by the reference variable.
ANALYSIS OF AN ELEMENTARY DATA TYPE
All the properties of an elementary data type can be accessed using the RTTI class, CL_ABAP_ELEMDESCR. Technical properties, such as type, length, and decimal places are defined in the respective public attributes. The public attribute, Type_kind, stores constants, which must be evaluated against the corresponding constant for the root class. The method, Get_DDIC_field, returns the structure with dictionary information for the data element.
ANALYSIS OF THE TYPE OF A REFERENCE VARIABLE
The RTTI class, CL_ABAP_REFDESCR, can be used to obtain the type of a reference variable. The inherited public attribute differentiates between data references and object references. The method, get_referenced_type, is used to describe the details of the static type of the reference variable.
ANALYSIS OF A STRUCTURE TYPE
The class, CL_ABAP_STRUCTDESCR, is used to determine the structure of the reference variable at runtime. It contains public attributes, such as Length, Struct_Kind, Has_include, and Components. The attribute, struct_kind, determines whether the structure is nested or elementary. Similarly, the Components attribute is an internal table, which contains the names of all components and their technical properties. The method, Get_DDIC_field_list, describes the semantic information of all the components.
NAVIGATION FROM STRUCTURE TYPE TO COMPONENT TYPES
As shown in the slide, the method, get_componenet_type, returns the description object for the specified component. The type of reference variable, lo_elem, is identified by the class cl_abap_elemdescr. Using the variable, ls_comp, in the LOOP AT statement, description objects for all the components of the structure can be obtained.
ANALYSIS OF A TABLE TYPE
The RTTI class, CL_ABAP_TABLEDESCR, is used to determine the table type of the reference variable at runtime. Its public attributes contain information, such as whether the internal table is standard or sorted; and whether it has a unique key. The method, get_table_line_type, can be used to get the line type of the description object.
ANALYSIS OF AN OBJECT TYPE
The public attributes of class CL_ABAP_CLASSDESCR contain information about attributes, methods, and interfaces of the described class. The attribute, Class_kind, determines if the class is declared as abstract or final. The Methods attribute is defined as a nested internal table, which contains parameters and exceptions for the respective method.