返回abap中持久化类的所有属性列表



我在自定义表上获得了一个持久类。现在使用GET_PERSISTENT_BY_QUERY,我可以得到一个对象列表。我现在怎么能得到这些对象与他们所有的属性打印给用户(如ALV)?

我可以调用实例函数,如果我点击它?

这是一个动态解决方案,query_data是调用get_persistent_by_query()返回的结果。最后,数据将存储在结构化表

中,您可以使用它在ALV中显示。注意,对字段的顺序几乎没有控制。对于实际使用,您还必须添加一些错误处理。
DATA: pers_obj_desc   TYPE REF TO cl_abap_classdescr,
      pers_query_data TYPE REF TO data,
      field_cat       TYPE lvc_t_fcat.
FIELD-SYMBOLS: <table> TYPE STANDARD TABLE.
LOOP AT query_data ASSIGNING FIELD-SYMBOL(<pers_data>).
  AT FIRST.
    pers_obj_desc ?= cl_abap_typedescr=>describe_by_object_ref( p_object_ref = <pers_data> ).
    LOOP AT pers_obj_desc->attributes ASSIGNING FIELD-SYMBOL(<attr_desc>).
      APPEND INITIAL LINE TO field_cat ASSIGNING FIELD-SYMBOL(<field_cat>).
      <field_cat>-fieldname = <attr_desc>-name.
      <field_cat>-intlen    = <attr_desc>-length.
      <field_cat>-datatype  = <attr_desc>-type_kind.
    ENDLOOP.
    cl_alv_table_create=>create_dynamic_table(
      EXPORTING
        it_fieldcatalog = field_cat
       IMPORTING
         ep_table       = pers_query_data
    ).
    ASSIGN pers_query_data->* TO <table>.
  ENDAT. " first
  APPEND INITIAL LINE TO <table> ASSIGNING FIELD-SYMBOL(<line>).
  LOOP AT pers_obj_desc->attributes ASSIGNING <attr_desc>.
    ASSIGN COMPONENT <attr_desc>-name OF STRUCTURE <line> TO FIELD-SYMBOL(<field>).
    data(getter) = 'GET_' && <attr_desc>-name.
    CALL METHOD <pers_data>->(getter) RECEIVING result = <field>.
  ENDLOOP.
ENDLOOP.

一个(我不认为好)的方法是定义一个名为list的实例属性。然后,生成的getter可以像下面这样重写,它可以从任何地方寻址。

DATA: WA TYPE ZMM_SMATERIAL.
WA-ERNAM = GET_ERNAM( ).
wa-ERSDA = GET_ERSDA( ).
WA-LAEDA = GET_LAEDA( ).
WA-MATNR = GET_MATNR( ).
WA-MTART = GET_MTART( ).
.
.
.
result = WA.
       " GET_LIST
endmethod.

…代表列表中我想要的每个属性。

这个解决方案的缺点:

  • 每次调用都有非常很多逻辑要做
  • 属性未被使用

另一个(我不认为好)的方法是定义一个名为list的实例属性。然后,在初始化时,准备好列表。

DATA: WA TYPE ZMM_SMATERIAL.
WA-ERNAM = GET_ERNAM( ).
wa-ERSDA = GET_ERSDA( ).
WA-LAEDA = GET_LAEDA( ).
WA-MATNR = GET_MATNR( ).
WA-MTART = GET_MTART( ).
.
.
.
LIST = WA.

endmethod.

…代表列表中我想要的每个属性。

然后,在每个setter中,也在这个列表中它被改变

这个解决方案的缺点:

  • 一切都是多余的,
  • 每个setter必须被更改

另一种方法是定义一个名为list的实例属性。然后,生成的getter可以像下面这样重写,它可以从任何地方寻址。

if me->list is initial.
  DATA: WA TYPE ZMM_SMATERIAL.
  WA-ERNAM = GET_ERNAM( ).
  wa-ERSDA = GET_ERSDA( ).
  WA-LAEDA = GET_LAEDA( ).
  WA-MATNR = GET_MATNR( ).
  WA-MTART = GET_MTART( ).
  .
  .
  .
  me->list = WA.
endif.
result = me->list
   " GET_LIST
endmethod.

…代表列表中我想要的每个属性。

除此之外,我还需要CLEAR每个setter上的列表。

最大的缺点是生成器会覆盖所有内容。(没有任何警告)

根据gert beukema的回答,我已经编写了一个助手类(主要改进如接受多于公共简单属性而不会失败)

class ZCL_PS_OBJECT_TO_STRUCTURE_HLP definition
  public
  create private .
public section.
  class-methods REFTAB_TO_STRUCTURE_LIST
    importing
      !IT_REFRENCE_TABLE type OSREFTAB
    returning
      value(RR_OBJECT_ATTRIBUTES_TABLE) type ref to DATA .
  class-methods CREATE_FIELDCAT
    importing
      !IR_OBJ_DESC type ref to CL_ABAP_CLASSDESCR
    returning
      value(RO_FIELD_CAT) type LVC_T_FCAT .
protected section.
private section.
ENDCLASS.

CLASS ZCL_PS_OBJECT_TO_STRUCTURE_HLP IMPLEMENTATION.

* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_PS_OBJECT_TO_STRUCTURE_HLP=>CREATE_FIELDCAT
* +-------------------------------------------------------------------------------------------------+
* | [--->] IR_OBJ_DESC                    TYPE REF TO CL_ABAP_CLASSDESCR
* | [<-()] RO_FIELD_CAT                   TYPE        LVC_T_FCAT
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD CREATE_FIELDCAT.
    LOOP AT IR_obj_desc->attributes ASSIGNING FIELD-SYMBOL(<fs_attr_desc>).
      APPEND INITIAL LINE TO ro_field_cat ASSIGNING FIELD-SYMBOL(<fs_field_cat>).
      <fs_field_cat>-fieldname = <fs_attr_desc>-name.
      <fs_field_cat>-intlen    = <fs_attr_desc>-length.
      <fs_field_cat>-datatype  = <fs_attr_desc>-type_kind.
      <fs_field_cat>-outputlen  = calculate a good outputlength (double when hex)
    ENDLOOP.

  ENDMETHOD.

* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_PS_OBJECT_TO_STRUCTURE_HLP=>REFTAB_TO_STRUCTURE_LIST
* +-------------------------------------------------------------------------------------------------+
* | [--->] IT_REFRENCE_TABLE              TYPE        OSREFTAB
* | [<-()] RR_OBJECT_ATTRIBUTES_TABLE     TYPE REF TO DATA
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD reftab_to_structure_list.
    DATA: lr_obj_desc   TYPE REF TO cl_abap_classdescr,
          lr_query_data TYPE REF TO data,
          lt_fieldcat  TYPE lvc_t_fcat.
    FIELD-SYMBOLS: <fs_table> TYPE STANDARD TABLE.
    LOOP AT it_refrence_table ASSIGNING FIELD-SYMBOL(<fs_object>).
      AT FIRST.
        lr_obj_desc ?= cl_abap_typedescr=>describe_by_object_ref( p_object_ref = <fs_object> ).
        lt_fieldcat = create_fieldcat( ir_obj_desc = lr_obj_desc ).
        CALL METHOD cl_alv_table_create=>create_dynamic_table
          EXPORTING
*           i_style_table             =
            it_fieldcatalog           = lt_fieldcat
*           i_length_in_byte          =
          IMPORTING
            ep_table                  = rr_object_attributes_table
*            ep_table                  = lr_query_data
*           e_style_fname             =
          EXCEPTIONS
            generate_subpool_dir_full = 1
            OTHERS                    = 2.
*        IF sy-subrc <> 0.
*          do some errorhandling here
*        ENDIF.
        ASSIGN rr_object_attributes_table->* TO <fs_table>.
*        ASSIGN lr_query_data->* TO <fs_table>.
      ENDAT. "first
      APPEND INITIAL LINE TO <fs_table> ASSIGNING FIELD-SYMBOL(<fs_line>).
      LOOP AT lr_obj_desc->attributes ASSIGNING FIELD-SYMBOL(<fs_attr_desc>).
        ASSIGN COMPONENT <fs_attr_desc>-name OF STRUCTURE <fs_line> TO FIELD-SYMBOL(<fs_attr_value>).
        DATA(getter) = 'GET_' && <fs_attr_desc>-name.
        TRY .
            CALL METHOD <fs_object>->(getter) RECEIVING result = <fs_attr_value>.
          CATCH cx_sy_dyn_call_illegal_method. "get_method is protected or private
            CLEAR <fs_attr_value>.
          CATCH cx_sy_dyn_call_illegal_type.
            "what to do with a list?
        ENDTRY.
      ENDLOOP.
    ENDLOOP.
  ENDMETHOD.
ENDCLASS.

现在有了这个helper类,我就不需要重新编码了,我总是想从对象中得到一个列表

在程序中,调用是这样的:

DATA: lt_query_data  TYPE osreftab,
      lt_query_table TYPE REF TO data.
FIELD-SYMBOLS <fs_table>.

lt_query_data = zca_ps_user=>agent->if_os_ca_persistency~get_persistent_by_query(
  i_query = cl_os_system=>get_query_manager( )->create_query( )
  ).
  CALL METHOD zcl_ps_object_to_structure_hlp=>reftab_to_structure_list
    EXPORTING
      it_refrence_table          = lt_query_data
    RECEIVING
      rr_object_attributes_table = lt_query_table.
  IF lt_query_table IS INITIAL.
    LEAVE.
  ENDIF.
  ASSIGN lt_query_table->* TO <fs_table>.
*Create a ALV
  DATA: lr_alv TYPE REF TO cl_salv_table .
  cl_salv_table=>factory( IMPORTING r_salv_table = lr_alv
    CHANGING t_table = <fs_table> )
      .
  lr_alv->display( ).

Ps:我想编辑答案,但不被接受

相关内容

  • 没有找到相关文章

最新更新