debugging - 如何(如果可能)获取内存中对象(类实例)的引用?

标签 debugging reflection scripting abap

我正在尝试查看是否有办法获取对本地(和全局)范围之外但存在于内存中的对象的引用。

假设在我的程序中,我已经实例化了一个对象,其引用是这样的:
{O:9*\PROGRAM=ZAVG_DELETE_THIS\CLASS=LCL_SMTH}

在大量调用之后,在我无法访问此对象的上下文中,我是否可以仅通过了解上述字符串来执行诸如获取此对象的引用之类的操作?

我正在研究 cl_abap_*descr 类,但我还没有找到一种使用“program_name”、“class_name”和“instance_number”来返回对象引用的方法。

我试图这样做是为了调试,而不是为了构建有效的东西。

[编辑 1]:
我假设需要 o:9 字符串才能获取对象的引用。正如@mydoghasworms 的回应所指出的那样,情况并非如此。似乎我只需要保存引用的变量的本地名称。

最佳答案

我希望我能正确理解你的问题,因为我不确定你所说的“为了调试”是什么意思,但这里是:

您可以使用以下方法访问加载在同一 session 内存中的另一个程序的变量(我很确定它不需要在调用堆栈中):

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

对于引用变量,它变得有点棘手,但这里有一个有助于演示的示例。

这是我们的调用程序,它包含一个引用变量 LR_TEST我们想在其他地方访问。出于演示的目的,我引用了本地定义的类(因为这是我从您的问题中收集到的)。
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.

这是我们要访问上述程序中的变量的程序。
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.

关于debugging - 如何(如果可能)获取内存中对象(类实例)的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14980241/

相关文章:

c - 在 VS 调试器 : any way to hover and see values? 中使用#define

c# - 使用反射从 C# 对象中去除所有值

kotlin - 从 Kotlin psi API 检索 Kotlin 属性类型

bash - 以非交互方式安装 Miniconda2

android - 从 android 程序中评估 ruby​​ 或 javascript 脚本字符串

debugging - 未找到 Visual Studio Code 中 'Debug: evaluate' 的 CommandID

visual-studio - Visual Studio - 从调试输出中过滤异常

c++ - C++ 中的轻量级反射

batch-file - CMD.EXE批处理脚本显示txt文件中的最后10行

java - 如何判断哪个派生类调用重写基方法