‎2009 Apr 08 3:05 PM
Hi there,
i am trying to program a dynamic classloader in Abap. by now i came this far:
DATA classname TYPE string VALUE 'ZCL_TEST'.
DATA dref TYPE REF TO data.
DATA type_descr TYPE REF TO cl_abap_typedescr.
FIELD-SYMBOLS <ref> TYPE ANY.
*
type_descr = cl_abap_typedescr=>describe_by_name( p_name = classname ).
classname = type_descr->absolute_name.
*
CREATE DATA dref TYPE REF TO (classname).
ASSIGN dref->* TO <ref>.
*
CREATE OBJECT <ref> TYPE (classname).
dref->to_string( ). -----> ERROR
The ClassLoading itselfs works perfectly, but now i don't know how to call a method of that class... Can't beliieve that i can instantiate a Class but not call a method?!?
So has someone done it already and can point me the right way?
Thanks
Mathias
‎2009 Apr 08 4:34 PM
To be able to access the object's method, you need to get the reference of the OBJECT, not of the DATA.
Try like this:
DATA classname TYPE string VALUE 'ZCL_TEST_TEXT'.
DATA: lo_obj TYPE REF TO object.
FIELD-SYMBOLS <ref> TYPE ANY.
CREATE OBJECT lo_obj TYPE (classname).
CALL METHOD lo_obj->('VALIDATE_MATNR').
WRITE: 'test'.
Regards,
Naimesh Patel
‎2009 Apr 08 4:34 PM
To be able to access the object's method, you need to get the reference of the OBJECT, not of the DATA.
Try like this:
DATA classname TYPE string VALUE 'ZCL_TEST_TEXT'.
DATA: lo_obj TYPE REF TO object.
FIELD-SYMBOLS <ref> TYPE ANY.
CREATE OBJECT lo_obj TYPE (classname).
CALL METHOD lo_obj->('VALIDATE_MATNR').
WRITE: 'test'.
Regards,
Naimesh Patel
‎2009 Apr 09 6:37 AM
Allright, yout solution works perfecrtly, thanks for pointing me the right way. Btw. have you any further sources of information or some nice linke about RTTS?
‎2009 Apr 09 7:09 AM
Ok I still don't get it entierely what is the difference between a DATA Reference and an OBJECT Reference? I derieved my original code from the F1 Help
INTERFACE i1.
...
ENDINTERFACE.
CLASS c1 DEFINITION.
PUBLIC SECTION.
INTERFACES i1.
ENDCLASS.
DATA dref TYPE REF TO data.
FIELD-SYMBOLS <ref> TYPE ANY.
...
CREATE DATA dref TYPE REF TO
('\program=kellerh_test\interface=i1').
ASSIGN dref->* TO <ref>.
CREATE OBJECT <ref> TYPE c1.
so what is happening here and why do they use the CREATE DATA Statement?
‎2009 Apr 09 2:32 PM
I don't know what would be the use of creating the object with REF TO DATA and than assign it back to the variable REF TO OBJECT to be able to access the data.
CREATE DATA dref TYPE REF TO
('\program=kellerh_test\interface=i1').
Here you are creating the TYPE at runtime which is type referent to the Interface. You get the object reference to the Field-Symbol. But Since field-symbol is not allowing to access the attributes, we need to cast that into the variable TYPE REF TO OBJECT.
Check this code:
REPORT ztest_np_1.
INTERFACE i1.
DATA: w_num TYPE i.
ENDINTERFACE. "i1
CLASS c1 DEFINITION.
PUBLIC SECTION.
INTERFACES i1.
ENDCLASS. "c1 DEFINITION
DATA dref TYPE REF TO data.
FIELD-SYMBOLS <ref> TYPE any.
CREATE DATA dref TYPE REF TO
('\program=ZTEST_NP_1\interface=i1').
ASSIGN dref->* TO <ref>.
CREATE OBJECT <ref> TYPE c1.
*----
FIELD-SYMBOLS <attr> TYPE ANY.
DATA: lo_obj TYPE REF TO object.
lo_obj ?= <ref>.
ASSIGN lo_obj->('I1~W_NUM') TO <attr>.
<attr> = 10.
*----
WRITE: 'Test'.
Regards,
Naimesh Patel
‎2009 Apr 09 2:42 PM
Allright, i slowly get it maybe the SAP guys should then change their online documentation.
But anyway. thanks a lot for your time and effort
Greetings Mathias