More on ABAP Objects instanceOf commands

More on ABAP Objects instanceOf commands

One year ago a user posted the question: Is there an equivalent to Java's instanceOf-operator in ABAP? The answer 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.
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 Director

    By submitting your registration information to SearchSAP.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchSAP.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.


program 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

    All fields are required. Comments will appear at the bottom of the article.

    Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.