2015 Apr 29 3:25 PM
Hello,
I would like to know how to modify a read-only static attribute from a class enhancement.
Example: I added GV_MY_ATTRIBUTE as a static (class) read-only attribute to the CL_A_STANDARD_CLASS class via the Z_MY_ENHANCEMENT enhancement. I added a pre-exit method to the CL_A_STANDARD_CLASS=>A_STATIC_STANDARD_METHOD static standard method via the Z_MY_ENHANCEMENT enhancement. How can I modify GV_MY_ATTRIBUTE from the pre-exit?
Of course, I don't want to create a public static setter to modify the attribute value, because I don't want it to be modifiable outside of the class.
Thanks in advance for your help!
Mathieu
2015 Apr 29 3:30 PM
Is there a problem changing GV_MY_ATTRIBUTE from your pre-exit? I can imagine there might be, due to the way enhancements to classes are handled.
Possible solutions:
Create a private static setter and call that from your pre-exit.
Define GV_MY_ATTRIBUTE as a private static, and only allow external access via a getter.
Hello,
I would like to know how to modify a read-only static attribute from a class enhancement.
Example: I added GV_MY_ATTRIBUTE as a static (class) read-only attribute to the CL_A_STANDARD_CLASS class via the Z_MY_ENHANCEMENT enhancement. I added a pre-exit method to the CL_A_STANDARD_CLASS=>A_STATIC_STANDARD_METHOD static standard method via the Z_MY_ENHANCEMENT enhancement. How can I modify GV_MY_ATTRIBUTE from the pre-exit?
Of course, I don't want to create a public static setter to modify the attribute value, because I don't want it to be modifiable outside of the class.
Thanks in advance for your help!
Mathieu
2015 Apr 29 3:30 PM
Is there a problem changing GV_MY_ATTRIBUTE from your pre-exit? I can imagine there might be, due to the way enhancements to classes are handled.
Possible solutions:
Create a private static setter and call that from your pre-exit.
Define GV_MY_ATTRIBUTE as a private static, and only allow external access via a getter.
2015 Apr 29 3:41 PM
It won't work: if the setter is private or protected, it can not be called from within the enhancement.
2015 Apr 29 3:56 PM
Create another class which has the SAP class as a friend. Put GV_MY_ATTRIBUTE as a private static attribute of this new class, and create a public getter.... or... make GV_MY_ATTRIBUTE a public read only attribute of the new class, can create a private setter for it.
2015 Apr 29 4:07 PM
I'm not sure I want to create a class just for this ^^ Wouldn'it a bit weird to create a class that only contains an attribute and a setter? Isn't there a more direct, simpler method?
Isn't it possible to use the core_object enhancement object (that represents the current instance of the class that is enhanced, in other words, "me") to access the class itself?
2015 Apr 29 4:19 PM
2015 Apr 29 4:36 PM
When an attribute is read-only, you can read it from outside its class / instance, and you can modify it only from within its class / instance.
In other words, it allows to avoid to create a getter (as the attribute is public) but without the "danger" for the attribute to be modified by "someone else" (another object / class than the object / class it belongs to).
The problem I have is that the attribute is part of a class' enhancement and the enhancement is considered as "someone else" than the class. Therefore, the only solution is to make the attribute as NON read-only, which I don't want because I don't want the attribute to be modifed outside of the class, or to create a public setter that I call from the enhancement, which I don't want either for the same reason.
2015 Apr 29 5:37 PM
2015 Apr 29 6:01 PM
It depends how SAP technically manage the enhancement. If it's a subclass, it should work.
2015 Apr 29 6:04 PM
Obviously that would be ideal, but the enhancement of classes isn't (as you've found) entirely straightforward - so some convoluted solution might be the only way.
I don't see a problem with creating a small class for one specific purpose. I've often done it. More often in Java, for sure, but also in ABAP.
2015 Apr 30 9:20 AM
As already told, "if the setter is private or protected, it can not be called from within the enhancement".
Unfortunately (?), an enhancement is not a sublcass of the enhanced class.
So I did it! I made the enhancement a sublass of the enhanced class and I have been able to modify directly the class read-only attribute, without using a public setter .
I went to my enhancement code and changed:
CLASS lcl_z_my_enhancement DEFINITION.
PUBLIC SECTION.
[...]
ENDCLASS. "LCL_Z_MY_ENHANCEMENT
To:
CLASS lcl_z_my_enhancement DEFINITION INHERITING FROM cl_a_standard_class.
PUBLIC SECTION.
[...]
ENDCLASS. "LCL_Z_MY_ENHANCEMENT
And then:
CLASS lcl_z_my_enhancement IMPLEMENTATION.
METHOD constructor.
me->core_object = core_object.
ENDMETHOD. "CONSTRUCTOR
To:
CLASS lcl_z_my_enhancement IMPLEMENTATION.
METHOD constructor.
super->constructor( ).
me->core_object = core_object.
ENDMETHOD. "CONSTRUCTOR
After that, I have been able to do:
gv_my_attribute = a_value.
Even if gv_my_attribute is read-only.
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |