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

Modify read-only static attribute from a class enhancement

Former Member
0 Likes
2,900

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

1 ACCEPTED SOLUTION
Read only

matt
Active Contributor
0 Likes
2,194

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

10 REPLIES 10
Read only

matt
Active Contributor
0 Likes
2,195

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.

Read only

Former Member
0 Likes
2,194

It won't work: if the setter is private or protected, it can not be called from within the enhancement.

Read only

matt
Active Contributor
0 Likes
2,194

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.

Read only

Former Member
0 Likes
2,194

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?

Read only

0 Likes
2,194
Hi Meister Matthew!

Excuse my ignorance but the READ ONLY statement is not only used to read the public attribute, blocking overwritten in it?

I believe that using SETTER / GETTER help in this case.
Read only

Former Member
0 Likes
2,194

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.

Read only

Former Member
0 Likes
2,194

Might a protected setter do the job?

Read only

matt
Active Contributor
0 Likes
2,194

It depends how SAP technically manage the enhancement. If it's a subclass, it should work.

Read only

matt
Active Contributor
0 Likes
2,194

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.

Read only

Former Member
0 Likes
2,194

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.