‎2007 Mar 26 10:38 AM
what is RTTI?
what is the significance of CL_ABAP_TYPEDESCR and all its inherited class.
In the instantiation why are these classes protected?
‎2007 Mar 26 10:57 AM
hi
good
Abbreviation RTTI. Determining data types during program run time.
example->
Here is an example of RTTI. Check whether two data types are identical.
REPORT ...
CLASS conv_exc DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.
PARAMETERS: type1 TYPE c LENGTH 30,
type2 TYPE c LENGTH 30.
DATA: dref1 TYPE REF TO data,
dref2 TYPE REF TO data.
FIELD-SYMBOLS: <data1> TYPE ANY,
<data2> TYPE ANY.
DATA: descr_ref1 TYPE REF TO cl_abap_typedescr,
descr_ref2 TYPE REF TO cl_abap_typedescr,
mess TYPE string.
START-OF-SELECTION.
TRY.
CREATE DATA: dref1 TYPE (type1),
dref2 TYPE (type2).
ASSIGN: dref1->* TO <data1>,
dref2->* TO <data2>.
CATCH cx_sy_create_data_error.
MESSAGE 'Create data error!' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.
ENDTRY.
...
descr_ref1 = cl_abap_typedescr=>describe_by_data( <data1> ).
descr_ref1 = cl_abap_typedescr=>describe_by_data( <data1> ).
TRY.
IF descr_ref1 <> descr_ref2.
RAISE EXCEPTION TYPE conv_exc.
ELSE.
<data2> = <data1>.
ENDIF.
CATCH conv_exc.
CONCATENATE `Assignment from type `
descr_ref2->absolute_name
` to `
descr_ref1->absolute_name
` not allowed!`
INTO mess.
MESSAGE mess TYPE 'I' DISPLAY LIKE 'E'.
ENDTRY.
Examples of RTTI and Objects
REPORT ...
CLASS conv_exc DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.
PARAMETERS: otype1 TYPE c LENGTH 30,
otype2 TYPE c LENGTH 30.
DATA: oref1 TYPE REF TO object,
oref2 TYPE REF TO object.
DATA: descr_ref1 TYPE REF TO cl_abap_typedescr,
descr_ref2 TYPE REF TO cl_abap_typedescr,
mess TYPE string.
START-OF-SELECTION.
TRY.
CREATE OBJECT: oref1 TYPE (otype1),
oref2 TYPE (otype2).
CATCH cx_sy_create_object_error.
MESSAGE 'Create object error!' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.
CATCH cx_root.
MESSAGE 'Other error!' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.
ENDTRY.
...
descr_ref1 =
cl_abap_typedescr=>DESCRIBE_BY_OBJECT_REF( oref1 ).
descr_ref2 =
cl_abap_typedescr=>DESCRIBE_BY_OBJECT_REF( oref2 ).
TRY.
IF descr_ref1 <> descr_ref2.
RAISE EXCEPTION TYPE conv_exc.
ELSE.
oref1 = oref2.
ENDIF.
CATCH conv_exc.
CONCATENATE `Assignment from type `
descr_ref2->absolute_name
` to `
descr_ref1->absolute_name
` not allowed!`
INTO mess.
MESSAGE mess TYPE 'I' DISPLAY LIKE 'E'.
ENDTRY.
thanks
mrutyun^
‎2007 Mar 26 11:15 AM
Hello John
If the instantiation of a class is protected or even private then you need to search for <b>static methods</b> within the same class that return an instance of the class or for <b>factory classes</b> (e.g. CL_RECA_MESSAGE_LIST is instantiated using CF_RECA_MESSAGE_LIST).
The significance of RTTI is - at least - twofold:
- Get all relevant information about a (data) object at runtime
- Create any type of data object at runtime (-> hint: TYPE HANDLE statement).
Regards
Uwe