Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 3193

Re: Determine class name used for calling a static method

$
0
0

Hi Mike,

 

This is a little bit old thread, but the question is interesting and also I am facing a similar problem.

 

I am using a static attribute (class-data) named THIS_CLASS_NAME to hold the name of the current class/subclass. This static attribute is updated in the CLASS_CONSTRUCTOR of each subclass, like this:

 

CLASS ZCL_SUB ...


METHOD class_constructor.

  TYPES dummy    TYPE REF TO ZCL_SUB.

  this_class_name    = 'ZCL_SUB'.

ENDMETHOD.

As the class name assigned to the this_class_name attribute is a hardcoded text, if the subclass is renamed this text won't match the real subclass name. To reduce that chance, the TYPES clause will throw a syntax error when the subclass is renamed. Unluckily, it doesn't save us if the subclass is rename to ZCL_OTHER while there is now other class named ZCL_SUB.

 

Now I can have generic constructors in the super class using the this_class_name attribute when required, as in:

 

CLASS-METHODS build_this_way ...

...

CREATE OBJECT result TYPE (this_class_name).

...

or use it as argument invoking methods in specialized factory classes.

 

Best regards,

 

OdL


Viewing all articles
Browse latest Browse all 3193

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>