‎2008 Apr 11 7:03 PM
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
‎2008 Apr 11 7:11 PM
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,
‎2008 Apr 11 7:31 PM
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