Skip to Content
Jun 24, 2013 at 10:09 AM

Dynamically accessing subclass attribute from within superclass method


Hello ABAP pros,

I'm trying to dynamically access attributes from within a superclass method. This works for all superclass attributes but not for the subclass attributes.

Imaging the following:

In an abstract superclass I define protected attributes a and b. In a subclass, inheriting from this superclass, I define a third protected attribute c. Now I'd like to define a method in the superclass which should dynamicall fill a structure out of all accessible instance attributes.

This method somehow looks like this:

METHOD to_structure. DATA: lref_structure_description TYPE REF TO cl_abap_structdescr, lref_structure_object TYPE REF TO data, lv_empty TYPE string. FIELD-SYMBOLS: <ls_component> TYPE abap_compdescr, <lv_value> TYPE any, <ls_structure> TYPE any, <lv_structure_component_value> TYPE any.

" get_structure_description( ) is a self written method that returns a RTTI description object of the desired structure of attributes lref_structure_description = me->get_structure_description( ). IF es_structure IS REQUESTED. CREATE DATA lref_structure_object TYPE HANDLE lref_structure_description. ASSIGN lref_structure_object->* TO <ls_structure>. LOOP AT lref_structure_description->components ASSIGNING <ls_component>. IF <lv_value> IS ASSIGNED. UNASSIGN <lv_value>. ENDIF. ASSIGN me->(<ls_component>-name) TO <lv_value>. IF <lv_value> IS NOT ASSIGNED. ASSIGN lv_empty TO <lv_value>. ENDIF. IF <lv_structure_component_value> IS ASSIGNED. UNASSIGN <lv_structure_component_value>. ENDIF. ASSIGN COMPONENT <ls_component>-name OF STRUCTURE <ls_structure> TO <lv_structure_component_value>. IF <lv_structure_component_value> IS ASSIGNED. <lv_structure_component_value> = <lv_value>. ENDIF. ENDLOOP. es_structure = <ls_structure>. ENDIF. ENDMETHOD.

The problem lies within the line "ASSIGN me->(<ls_component>-name) TO <lv_value>.". This works for <ls_component>-name = a and <ls_component>-name = b but not for <ls_component>-name = c (the subclass attribute).

I call the method on an instance of the subclass. The debugger shows that "me" is an instance of the subclass and if I enter "me->c" in the debugger I see the corresponding value. But <lv_value> just won't get assigned in this case. It also does not work if I specify the component name fully dynamic (test = 'me->' && <ls_component>-name. ASSIGN (test) TO <lv_value>.) nor if I explicitly use an object reference of the subclass (lref_subclass ?= me. ASSIGN lref_subclass->(<ls_component>-name) TO <lv_value>.).

It's a 7.31 SP3 system.

Any ideas what may be wrong here or is this just not possible?