vlad-ardelean
vlad-ardelean

Reputation: 7622

How to (if possible) get the reference of an in-memory object (class instance)?

I'm trying to see if there's a way to get a refference of an object which is outside the local (and global) scope, but who exists in memory.

Let's say in my program, i've instantiated an object whose reference is this: {O:9*\PROGRAM=ZAVG_DELETE_THIS\CLASS=LCL_SMTH}

Far away after tons of calls, in a context where i wouldn't be able to access this object, could i do something like getting the reference of this object simply by knowing the above string?

I was looking into the cl_abap_*descr classes, but i haven't found a method that takes the 'program_name', 'class_name' and 'instance_number', to return the reference of an object.

I'm trying to do this for the purpose of debugging, not to build something that works.

[EDIT 1]: I assumed that the o:9 string was required in order to get the reference of the object. As pointed out in the response of @mydoghasworms, this isn't the case. It seems that i only need the local name of the variable which holds the reference.

Upvotes: 4

Views: 8235

Answers (1)

mydoghasworms
mydoghasworms

Reputation: 18591

I hope I understand your question correctly, because I am not sure what you mean with "for the purpose of debugging", but here goes:

You can access the variables of another program that are loaded in the memory of the same session (I am pretty sure it does not need to be in the call stack) using:

ASSIGN ('(PROGRAM)VARIABLE') TO LV_LOCAL.

With reference variables, it becomes a bit more tricky, but here is an example that will help to demonstrate.

Here is our calling program that contains a reference variable LR_TEST which we want to access somewhere else. For the purpose of the demonstration, I make reference to a locally defined class (because that's what I gather from your question).

REPORT  ZCALLER.

class lcl_test definition.
  public section.
    data: myval type i.

    methods: my_meth exporting e_val type i.
endclass.

data: lr_test type ref to lcl_test.

CREATE OBJECT lr_test.

lr_test->MYVAL = 22.

perform call_me(zcallee).

class lcl_test implementation.
  method my_meth.
* Export the attribute myval as param e_val.
    e_val = myval.
  endmethod.
endclass.

Here is the program in which we want to access a variable from the above program.

REPORT  ZCALLEE.

form call_me.

  field-symbols: <ref>.
  data: ld_test type ref to object.
  data: lv_val type i.

* Exhibit A: Gettinf a reference to a 'foreign' object instance
  assign ('(ZCALLER)LR_TEST') to <ref>.
* <ref> now contains a reference to the class instance from the program
* ZCALLER (not very useful, except for passing around maybe)

* Exhibit B: Getting a public attribute from a 'foreign' class instance
  assign ('(ZCALLER)LR_TEST->MYVAL') to <ref>.
* <ref> now contains the value of the attribute MYVAL

* Exhibit C: Getting a reference to an instance and calling a method
  assign ('(ZCALLER)LR_TEST') to <ref>. "Again the class reference
  if sy-subrc = 0. "Rule: Always check sy-subrc after assign before
                   "accessing a field symbol! (but you know that)
    ld_test = <ref>. "Now we have a concrete handle
* Now we make a dynamic method call using our instance handle
    CALL METHOD ld_test->('MY_METH')
      IMPORTING
        e_val = lv_val.
  endif.
endform.

Upvotes: 1

Related Questions