Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Get class name without object initialization

0 Kudos

Hi folks,

I'm using RTTS to dynamically get class name for an initialized object. Following codes work just fine:


data obj type ref to zflh_test_class

obj = new #( ).

data cls_descr type ref to cl_abap_classdescr.

cls_descr ?= cl_abap_classdescr=>describe_by_object_ref( obj ).

data(rel_name) = cls_descr->get_relative_name( ). " returns zflh_test_class

The question is: How can I get the class name without object initialization. I would like to have something like this


data obj type ref to zflh_test_class

data cls_descr type ref to cl_abap_classdescr.

cls_descr ?= cl_abap_classdescr=>describe_by_object_ref( obj ).

data(rel_name) = cls_descr->get_relative_name( ).


Of course cl_abap_classdescr=>describe_by_object_ref throws exception because "obj" is initial.

Any idea?

Cheers,

Ferry

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor
0 Kudos

Something like that :

DATA obj type ref to if_ixml.
data(ref) = cast cl_abap_refdescr( cl_abap_typedescr=>describe_by_data( obj ) ).
data(objtype) = cast cl_abap_objectdescr( ref->get_referenced_type( ) ).
data(rel_name) = objtype->get_relative_name( ).
ASSERT rel_name = 'IF_IXML'.
3 REPLIES 3

Sandra_Rossi
Active Contributor
0 Kudos

Something like that :

DATA obj type ref to if_ixml.
data(ref) = cast cl_abap_refdescr( cl_abap_typedescr=>describe_by_data( obj ) ).
data(objtype) = cast cl_abap_objectdescr( ref->get_referenced_type( ) ).
data(rel_name) = objtype->get_relative_name( ).
ASSERT rel_name = 'IF_IXML'.

0 Kudos

It works like a charm. Many thanks for the solution.

raphael_almeida
Active Contributor
0 Kudos

Simplifying a bit, we have it:

DATA obj TYPE REF TO if_ixml.
DATA(rel_name) = CAST cl_abap_objectdescr( CAST cl_abap_refdescr( cl_abap_typedescr=>describe_by_data( obj ) )->get_referenced_type( ) )->get_relative_name( ).


ASSERT rel_name = 'IF_IXML'.


Warm regards,


Raphael Pacheco.