ABAP Objects instanceOf command

This code sample by Charles Folwell creates an instanceOf command in ABAP Objects.

On June 4, 2003, a question was posed to Matthew Billingham of the Ask the Expert forum (Is there an equivalent to Java's instanceOf-operator in ABAP?), regarding an instanceOf command in ABAP Objects similar to this same command in Java. I recently ran across this question and took up the challenge to create such a command. All work was done in 4.6C.

The definition of the instanceOf operator in Java reads:

The instanceOf operator tests whether its first operand is an instance of its second.
op1 instanceof op2
op1 must be the name of an object and op2 must be the name of a class. An object is considered to be an instance of a class if that object directly or indirectly descends from that class.

Below is a short program that implements this instanceOf command in ABAP objects.

Some helpful references:
The class CL_ABAP_TYPEDESCR implements the Run Time Type Identification utilities (RTTI). You can do help on ABAP command DESCRIBE which provides a link to help on RTTI. The ?= operator implements a widening cast, also called a down cast.

You get the following report when you run the program:

INSTANCEOF command from JAVA

Answer:X
Answer:
Answer:X

Question/comments – I can be reached at cfolwell@csc.com.
REPORT zcdf_instanceof.

* Implement JAVA INSTANCEOF Command.

* The following class definitions are for testing only and are NOT
* a part of the instanceof command.

CLASS c10 DEFINITION.
ENDCLASS.
*
CLASS c11 DEFINITION INHERITING FROM c10.
ENDCLASS.
*
CLASS c12 DEFINITION INHERITING FROM c11.
ENDCLASS.
*
CLASS c20 DEFINITION.
ENDCLASS.
*
CLASS c21 DEFINITION INHERITING FROM c20.
ENDCLASS.
*
CLASS c22 DEFINITION INHERITING FROM c21.
ENDCLASS.

* Define a utility class to implement the instanceof command.

CLASS util DEFINITION ABSTRACT.

PUBLIC SECTION.

TYPES:
ty_ind(1) TYPE c.

CLASS-METHODS:

instanceof
IMPORTING
ip_object TYPE REF TO object
ip_class_name TYPE seoclsname
RETURNING
value(rp_ind) TYPE ty_ind.

ENDCLASS.

* Implement the instanceof command.

CLASS util IMPLEMENTATION.

METHOD instanceof.

DATA:
offset TYPE i,
class_name TYPE seoclsname,
descr_ref_class TYPE REF TO cl_abap_classdescr,
descr_ref_type TYPE REF TO cl_abap_typedescr.

* Default the answer to NO. Space = no, 'X' = yes.

CLEAR rp_ind.

* Get object that describes the class of the object we were
* asked to interogate.

descr_ref_class ?=
cl_abap_typedescr=>describe_by_object_ref( ip_object ).

* Try to match up class names moving up superclass chain
* until we find a match or run out of superclasses.

DO.

* Get the class name. Set sy-fdpos via compare pattern
* operator.

IF descr_ref_class->absolute_name CP '*CLASS=*'.

offset = sy-fdpos + 7.
class_name = descr_ref_class->absolute_name+offset.

ELSE.
EXIT. "EXIT DO loop, no class name found
ENDIF.

* If class names match, success, and we are done

IF class_name = ip_class_name.
rp_ind = 'X'.
EXIT. "EXIT DO loop, class name found
ENDIF.

* No match yet, get superclass name.

CALL METHOD descr_ref_class->get_super_class_type
RECEIVING
p_descr_ref = descr_ref_type
EXCEPTIONS
super_class_not_found = 4.

IF sy-subrc <> 0.
EXIT. "EXIT DO loop, ran out of superclass names
ENDIF.

* Loop back around to check this next class name.

descr_ref_class ?= descr_ref_type.

ENDDO.

ENDMETHOD.

ENDCLASS.

* Test data.

DATA:
ind(1) TYPE c,
ref1 TYPE REF TO c12,
ref2 TYPE REF TO c22.

START-OF-SELECTION.

* Test instanceof command.

CREATE OBJECT ref1.
CREATE OBJECT ref2.

* Test #1

ind = util=>instanceof( ip_object = ref1 ip_class_name = 'C11' ).
WRITE:/ 'Answer:', ind.

* Test #2

ind = util=>instanceof( ip_object = ref2 ip_class_name = 'C10' ).
WRITE:/ 'Answer:', ind.

* Test #3

ind = util=>instanceof( ip_object = ref2 ip_class_name = 'C22' ).
WRITE:/ 'Answer:', ind.

This was first published in September 2004

Dig deeper on SAP ABAP

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close