Actually ABAP has the functionality of an instanceOf-operator. The difference is that it is not an operator but a method of the RTTI. Please see the code example below. You can ask an instance of the RTTI directly whether a given instance belongs to the type described by the RTTI instance or not. This method is quite fast and if you use it in a loop you have to take into account that the RTTI instance has only to be derived once. It does not matter how this RTTI instance is derived (e.g. by RTTI methods DESCRIBE_BY_NAME, DESCRIBE_BY_OBJECT_REF or it is a parameter given from some other service). I hope this tip will help to simplify the code. If you have any further question about the RTTI or reflection in ABAP in general, do not hesitate to write me.
Regards,
Holger.Janz@sap.com
Requires Free Membership to View
When you register, you will start receiving targeted emails from my award-winning team of editorial writers. Our goal is to keep you informed on the hottest topics and biggest challenges faced by SAP professionals today.
Hannah Smalltree, Editorial Directorprogram rtti_instanceof.
class c00 definition.
endclass.
data:
class_ref type ref to cl_abap_classdescr,
any_object type ref to object.
create object any_object type c00.
* This works also for cl_abap_intfdescr.
* In this case APPLIES_TO means whether the interface is implemented
* by the instance or it is not.
class_ref ?= cl_abap_typedescr=>describe_by_name( 'C00' ).
if abap_true = class_ref->applies_to( any_object ).
write / 'Object is intance of C00.'.
else.
write / 'Object is not intance of C00.'.
endif.
This was first published in November 2004
Join the conversationComment
Share
Comments
Results
Contribute to the conversation