Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

instance of

Former Member
0 Likes
405

I would like to know what is the equivalent in abap object of the "instance of" of Java, because I have an object upcasted and I want to know if i am able to downcast it, thanks

2 REPLIES 2
Read only

Former Member
0 Likes
381

Hello,

To downcast you can use the operator ?=.

This is an example:


DATA: ref1 TYPE REF TO object,
  ref2 TYPE REF TO class.

ref2 ?= ref1.

And to catch errors you can use the following.


CATCH SYSTEM-EXCEPTIONS move_cast_error = 4.
  ref2 ?= ref1.
ENDCATCH.
IF sy-subrc NE 0.
  WRITE 'Error when casting'.
ENDIF.

Regards,

Read only

uwe_schieferstein
Active Contributor
0 Likes
381

Hello

On ECC 6.0 there is a static class method available which could be used as the InstanceOf equivalent.


*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_IS_INSTANCE_OF
*&
*&---------------------------------------------------------------------*
*& Thread: instance of
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="819410"></a>
*&---------------------------------------------------------------------*

REPORT  zus_sdn_is_instance_of.

TYPE-POOLS: abap.


DATA: go_msglist      TYPE REF TO cl_reca_message_list,
      gd_msg          TYPE bapi_msg.

PARAMETER:
  p_class   TYPE seoclsname  DEFAULT 'IF_RECA_MESSAGE_LIST'.


START-OF-SELECTION.

  go_msglist ?= cf_reca_message_list=>create( ).

  IF ( cl_wdy_wb_reflection_helper=>is_instance_of(
        object    = go_msglist
        type_name = p_class ) = abap_true ).
    concatenate 'Is instance of' p_class
      into gd_msg SEPARATED BY space.
    MESSAGE gd_msg TYPE 'I'.
  ELSE.
    concatenate 'Is NOT instance of' p_class
      into gd_msg SEPARATED BY space.
    MESSAGE gd_msg TYPE 'I'.
  ENDIF.


END-OF-SELECTION.

However, please note that this class may be only used in a rather restricted sense related to WebDynpro.

Regards

Uwe