Frage

Is there any way to get the value of an objects' private attribute without a getter. Modifying the class is not permitted in any shape or form.

Please find below an example class with a private attribute.

CLASS counter DEFINITION.
  PUBLIC SECTION.
    METHODS: set IMPORTING value(set_value) TYPE i.
  PRIVATE SECTION.
    DATA count TYPE i.
ENDCLASS.                    "counter DEFINITION 

CLASS counter IMPLEMENTATION.
  METHOD set.
    count = set_value.
  ENDMETHOD.                    "set
ENDCLASS.                    "counter IMPLEMENTATION

How can I get the value of count? Inheriting from counter will not work because count is private, not protected.

War es hilfreich?

Lösung

Unfortunately not, I have tried this myself in many different ways none of which work:

  • Having a standard super class - the super class cannot access the
    private attributes of subclasses dynamically
  • Making a subclass will never work since it can only access protected
  • Attempting to use the unit test framework doesn't work. I tried to call the kernel modules that allow access to private data but to no avail.

You are basically flat out of luck. There is one obscure option though depending on the class you are trying to access. Some classes have interfaces specified as friends and if you implement that interface you can access their private data (the ALV on 7.20 is like this) but unfortunately this will only work in a few limited cases.

Andere Tipps

Runtime type services are the abap's equivalent of reflection. They allow You nearly to scan every object, and mostly even modify it at runtime. As far as i know, the visibility of attributes does not matter. But be careful. And read about the various classes, because there are many, each specified to work on a special type of dataopbject ( structs, objects, etc)

http://wiki.scn.sap.com/wiki/pages/viewpage.action?pageId=42965

You could make a sub class, re-implement the setter and set a second variable, then call the parent method. Be aware of the ramifications of having two variables holding the same stuff... Please see vwegert's comments and see if you really want to because it's generally not a great idea and it breaks the rules of OO.

CLASS counter_sub DEFINITION INHERITING FROM counter.
  PUBLIC SECTION.
    data count2 type i read-only.
    METHODS: set REDEFINITION.
ENDCLASS.                    "counter_sub DEFINITION

CLASS counter_sub IMPLEMENTATION.
  METHOD set.
    count2 = set_value.
    super->set( set_value ).
  ENDMETHOD.                    "set
ENDCLASS.                    "counter_sub IMPLEMENTATION
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top