One year ago a user posted the question: Is there an equivalent to Java's instanceOf-operator in ABAP? The answer...
By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.
in the tip ABAP Objects instanceOf command was quite interesting. The example has one problem. It deals with class names! In ABAP it is possible to have global and local classes and it is possible that a global class has the same name as a local class. The scope of the use of the class name decides what class to use. So this example will not work correctly in some cases.
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.
class c00 definition.
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.'.
write / 'Object is not intance of C00.'.