由于系统自带的各种数据共享方法存在着各种各样的不足,因此对于数据之间的传递,可以使用一个数据共享类来实现,该共享类可以有自己的写法,但是核心就是每次只实例化一次,然后设置和获取数据。

该共享类支持任意类型数据的共享。

具体代码如下:

*———————————————————————-*
*       CLASS Y_CL_WD_SHARED_DATA DEFINITION
*———————————————————————-*
*
*———————————————————————-*
CLASS y_cl_wd_shared_data DEFINITION
  
PUBLIC
   FINAL
  
CREATE PUBLIC .

   PUBLIC SECTION.
    
CLASS-METHODS get_instance
       RETURNING
        
VALUE(ro_instance) TYPE REF TO y_cl_wd_shared_data .
  
PROTECTED SECTION.
  
PRIVATE SECTION.

     TYPES:
      
BEGIN OF s_parameter,
        
key   TYPE string,
        
value TYPE REF TO data,
      
END OF s_parameter .
    
TYPES:
       t_parameter
TYPE HASHED TABLE OF s_parameter WITH UNIQUE KEY
  
key .

     DATA mt_parameter TYPE t_parameter .
    
CLASS-DATA mo_shared_data TYPE REF TO y_cl_wd_shared_data .
ENDCLASS.

CLASS y_cl_wd_shared_data IMPLEMENTATION.

* <SIGNATURE>—————————————————————————————+
* | Static Public Method Y_CL_WD_SHARED_DATA=>GET_INSTANCE
* +————————————————————————————————-+
* | [<-()] RO_INSTANCE                    TYPE REF TO Y_CL_WD_SHARED_DATA
* +————————————————————————————–</SIGNATURE>
  
METHOD get_instance.

     IF mo_shared_data IS NOT BOUND.
      
CREATE OBJECT mo_shared_data TYPE y_cl_wd_shared_data.
    
ENDIF.
     ro_instance
= mo_shared_data.

   ENDMETHOD.

* <SIGNATURE>—————————————————————————————+
* | Instance Public Method Y_CL_WD_SHARED_DATA->IF_FPM_PARAMETER~DELETE_VALUE
* +————————————————————————————————-+
* | [—>] IV_KEY                         TYPE        STRING
* +————————————————————————————–</SIGNATURE>
  
METHOD if_fpm_parameter~delete_value.

     DELETE TABLE mt_parameter WITH TABLE KEY key = iv_key.

   ENDMETHOD.

* <SIGNATURE>—————————————————————————————+
* | Instance Public Method Y_CL_WD_SHARED_DATA->IF_FPM_PARAMETER~GET_KEYS
* +————————————————————————————————-+
* | [<-()] RT_KEYS                        TYPE        T_KEYS
* +————————————————————————————–</SIGNATURE>
  
METHOD if_fpm_parameter~get_keys.

     DATA: lv_key TYPE string.

     FIELD-SYMBOLS: <fs_parameter> LIKE LINE OF mt_parameter.

     LOOP AT mt_parameter ASSIGNING <fs_parameter>.
       lv_key
= <fs_parameter>key.
      
APPEND lv_key TO rt_keys.
    
ENDLOOP.

   ENDMETHOD.

* <SIGNATURE>—————————————————————————————+
* | Instance Public Method Y_CL_WD_SHARED_DATA->IF_FPM_PARAMETER~GET_VALUE
* +————————————————————————————————-+
* | [—>] IV_KEY                         TYPE        STRING
* | [<—] EV_VALUE                       TYPE        ANY
* | [<—] ER_VALUE                       TYPE REF TO DATA
* +————————————————————————————–</SIGNATURE>
  
METHOD if_fpm_parameter~get_value.

     DATA: lr_descr           TYPE REF TO cl_abap_typedescr.
    
DATA: lr_ev_value_descr  TYPE REF TO cl_abap_typedescr.

     FIELD-SYMBOLS: <fs_parameter> LIKE LINE OF mt_parameter.
    
FIELD-SYMBOLS: <fs_value>    TYPE any.

     READ TABLE mt_parameter ASSIGNING <fs_parameter> WITH KEY key = iv_key.
    
CHECK sysubrc IS INITIAL.

     er_value = <fs_parameter>value.

     IF ev_value IS SUPPLIED.
      
TRY.
          
ASSIGN <fs_parameter>value->* TO <fs_value>.

           CALL METHOD cl_abap_typedescr=>describe_by_data
            
EXPORTING
               p_data     
= <fs_value>
             RECEIVING
               p_descr_ref
= lr_descr
            
EXCEPTIONS
              
OTHERS      = 1.
          
IF sysubrc <> 0.
            
CLEAR ev_value.
            
RETURN.
          
ENDIF.
          
CALL METHOD cl_abap_typedescr=>describe_by_data
            
EXPORTING
               p_data     
= ev_value
             RECEIVING
               p_descr_ref
= lr_ev_value_descr
            
EXCEPTIONS
              
OTHERS      = 1.
          
IF sysubrc <> 0.
            
CLEAR ev_value.
            
RETURN.
          
ENDIF.
          
IF ( lr_descr->type_kind           = cl_abap_typedescr=>typekind_oref AND
                lr_ev_value_descr
->type_kind <> cl_abap_typedescr=>typekind_oref ) OR
             
( lr_descr->type_kind          <> cl_abap_typedescr=>typekind_oref AND
                lr_ev_value_descr
->type_kind  = cl_abap_typedescr=>typekind_oref ) OR
             
( lr_descr->type_kind           = cl_abap_typedescr=>typekind_table AND
                lr_ev_value_descr
->type_kind <> cl_abap_typedescr=>typekind_table ) OR
             
( lr_descr->type_kind          <> cl_abap_typedescr=>typekind_table AND
                lr_ev_value_descr
->type_kind  = cl_abap_typedescr=>typekind_table ).
            
CLEAR ev_value.
            
RETURN.
          
ENDIF.
           ev_value
= <fs_value>.
        
CATCH cx_root.
          
CLEAR ev_value.
      
ENDTRY.
    
ENDIF.

   ENDMETHOD.

* <SIGNATURE>—————————————————————————————+
* | Instance Public Method Y_CL_WD_SHARED_DATA->IF_FPM_PARAMETER~SET_VALUE
* +————————————————————————————————-+
* | [—>] IV_KEY                         TYPE        STRING
* | [—>] IV_VALUE                       TYPE        ANY(optional)
* | [—>] IR_VALUE                       TYPE REF TO DATA(optional)
* +————————————————————————————–</SIGNATURE>
  
METHOD if_fpm_parameter~set_value.

     DATA: ls_parameter LIKE LINE OF mt_parameter.
    
DATA: lr_datadescr TYPE REF TO cl_abap_datadescr.

     FIELD-SYMBOLS: <fs_parameter> LIKE LINE OF mt_parameter.
    
FIELD-SYMBOLS: <fs_value1>   TYPE any.
    
FIELD-SYMBOLS: <fs_value2>   TYPE any.

     READ TABLE mt_parameter ASSIGNING <fs_parameter> WITH KEY key = iv_key.
    
IF sysubrc <> 0.
*   key not part of this bag, create the key
       ls_parameter
key = iv_key.
      
INSERT ls_parameter INTO TABLE mt_parameter ASSIGNING <fs_parameter>.
    
ENDIF.

* Set value has copy semantic
    
IF iv_value IS SUPPLIED.
       lr_datadescr ?= cl_abap_datadescr
=>describe_by_data( iv_value ).
      
CREATE DATA <fs_parameter>value TYPE HANDLE lr_datadescr.
      
ASSIGN <fs_parameter>value->* TO <fs_value1>.
       <fs_value1>
= iv_value.
    
ELSEIF ir_value IS SUPPLIED.
       lr_datadescr ?= cl_abap_datadescr
=>describe_by_data_ref( ir_value ).
      
CREATE DATA <fs_parameter>value TYPE HANDLE lr_datadescr.
      
ASSIGN <fs_parameter>value->* TO <fs_value1>.
      
ASSIGN ir_value->* TO <fs_value2>.
       <fs_value1>
= <fs_value2>.
    
ELSE.
      
CLEAR <fs_parameter>value.
    
ENDIF.

   ENDMETHOD.
ENDCLASS.


To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply