2017 Aug 15 9:11 AM
Hello,
How to get all elements from class.
I want to clear all global variables. But I don't know, how I get all global elements from my Abap class.
I hope, you could help me.
2017 Aug 15 9:27 AM
An ABAP class doesn't have any global variables. All attributes are local to the class.
RTTI services can provide you all components of a class, use CL_ABAP_CLASSDESCR.
2017 Aug 15 1:29 PM
and how i could clear all these components or is it not possible?
2017 Aug 15 2:09 PM
Loop over the attribute table, dynamic assign to field symbol, CLEAR field symbol. Basic stuff.
2017 Aug 15 2:37 PM
How I get these attribute table. I don't manually used the CL_ABAP_CLASSDESCR before.
2017 Aug 15 3:12 PM
If you haven't done this before, then I would ask why you think you need to do it this way?
See answers from Uwe and Suhas.
2017 Aug 15 3:19 PM
2017 Aug 15 9:27 AM
Loop over the attribute table, dynamic assign to field symbol (attribute must be visible), CLEAR field symbol.
2017 Aug 15 11:16 AM
If you mean how to clear all statics in a class - write a method to do it and call that.
2017 Aug 15 12:59 PM
I want to do this dynamic and don't want to clear every variable by hand.
2017 Aug 16 10:23 AM
Clearing all the static attributes of a class indicates that your design is poor. I can't see why the complexity of doing it dynamically could be preferable. I've never written a class where this was needed, and I've written thousands.
Consider using the singleton pattern instead. Then you can have a static method that creates and new instance, and returns that (clearing the old one).
2017 Aug 15 1:31 PM
I'd just create a new reference, let the garbage collector do the rest for you...
(after clearing all public and private variables the whole class is initial, isn't it? Or am I missing something?)
2017 Aug 15 5:48 PM
OK for instance attributes. But I think the OP means static attributes by saying global variables.
2017 Aug 15 5:57 PM
Then I would use a structure instead of single variables.
Dynamic programming just to clear variables would be "mit Kanonen auf Spatzen schießen" IMO. But possible of course.
2017 Aug 15 6:07 PM
2017 Aug 15 9:19 PM
Does that mean a public instance attribute is less global than a private static attribute?
Is there a globality ranking published somewhere I can use for future reference? 😉
2017 Aug 16 6:19 AM
Independently from what the OP meant, you cannot clear all attributes by garbage collection but only instance attributes. Since he said "all elements from class", it is justified to assume that he meant all attributes including static attributes.
The terms for your reference are defined in the ABAP Glossary, e.g. global data, which, alas, are seldom use here .
2017 Aug 16 9:19 AM
It was a joke. You assumed he meant static, but if someone inaccurately calls attributes global, which they clearly aren't (as I said in my first response), then they would not necessarily have the same view.
If we try to define the globularity (globulosity?) of scope vs lifetime, one could argue that that which is visible to the outside looks 'global', making a public 'more global' than a private static, even though it has a shorter lifetime.
But, just in case someone draws any conclusions, all of this is nonsense of course 🙂
2017 Aug 15 1:56 PM
@OP: What is your motivation for clearing the attributes of the instance?
2017 Aug 16 10:25 AM
Your question is bit poor. Rather than ask how to implement your chosen solution to the programming problem, you should explain what you are trying to achieve in general. Then you may get other solutions that are better than your preferred one (dynamic clearing of static attributes, it seems). Furthermore, it means people don't have to guess at your intentions, thereby increasing the quality of the answers.