Application Development and Automation 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: 
Read only

Dynamic class calling

Former Member
0 Likes
1,715

Hi All,

I want to know the same class name which I am executing in the constructor of the class. Is there any system variable which stores this information. Like for example sy-repid for reports.

Then after getting the calss name in string how I am going instantiate the same.

Regards,

Guru Dutt

1 ACCEPTED SOLUTION
Read only

naimesh_patel
Active Contributor
0 Likes
1,307

You can use the RTTS classes to get to know to the class name but this would only possible once you have the object.

CRATE OBJECT ... statement creates the object before calling the CONSTRUCTOR. So, when system calls the CONSTRUCTOR, the attributes and methods can be accessible by the reference of ME. So, you can pass the reference ME to the method DESCRIBE_BY_OBJECT_REF of the class CL_ABAP_TYPEDESCR in the parameter P_OBJECT_REF and get back the Description object. Attribute ABSOLUTE_NAME of the description object would be the name of the class.

Like:


   data: type_ref TYPE REF TO cl_abap_typedescr,
    type_ref = cl_abap_typedescr=>describe_by_object_ref( me ).
    type_ref->absolute_name " Name of the class

Regards,

Naimesh Patel

6 REPLIES 6
Read only

Former Member
0 Likes
1,307

Hi,

You could use the class cl_abap_typedesc to achive this

check out this coding http://help.sap.com/abapdocu_70/de/ABENRTTI_OBJECT_TYPE_ABEXA.htm

the informations you need will be in the attributes of the object of cl_abap_typedesc.

Greetings,

dsp

Read only

naimesh_patel
Active Contributor
0 Likes
1,308

You can use the RTTS classes to get to know to the class name but this would only possible once you have the object.

CRATE OBJECT ... statement creates the object before calling the CONSTRUCTOR. So, when system calls the CONSTRUCTOR, the attributes and methods can be accessible by the reference of ME. So, you can pass the reference ME to the method DESCRIBE_BY_OBJECT_REF of the class CL_ABAP_TYPEDESCR in the parameter P_OBJECT_REF and get back the Description object. Attribute ABSOLUTE_NAME of the description object would be the name of the class.

Like:


   data: type_ref TYPE REF TO cl_abap_typedescr,
    type_ref = cl_abap_typedescr=>describe_by_object_ref( me ).
    type_ref->absolute_name " Name of the class

Regards,

Naimesh Patel

Read only

0 Likes
1,307

Hi,

When I am using

DESCR_REF = CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( ME ).

There is a compile error showing : The reference 'Me" to the current instance exists only with instance methods.

Please let me know how to overcome this.

Regards,

Guru

Read only

0 Likes
1,307

You can't access the own Object Reference with ME inside the static method because to access static method, you don't need to instantiate the object.

Mention your overall design and what you are trying to do.

Regards,

Naimesh Patel

Read only

0 Likes
1,307

Hi,

There is a standard interface A with methods A1(Instance dependent), A2(static). There is a custom class B which uses the interface A. There is a sub-class C which inherits B as super-class.

A(Interface)->B(Super Class)->C(Sub Class)

Now method A2 can be modified in class B. So here I can create a object refernce of type B. But when the same method A2 is visible in class C I cannot create obejct reference for class C. in method A2.

So for the same reason I needed to know the calling class name so that I can make the object reference dynamic based on calling class.

Please let me know is there any way to handle this.

Regards,

Guru

Read only

0 Likes
1,307

hi,

for objects you also need to use the method describe_by_object_ref( )

Greetings,

dsp