ABAP – List interfaces implemented by an ABAP Class

This short code snippet shows how to a) get list of all interfaces implemented by given class, b) check if an interface is implemented by a class, c) list all direct implementations of given interface.

List of interfaces implemented by a class

DATA:
  lo_class_description TYPE REF TO cl_abap_classdescr.
CONSTANTS:
  lc_class_name TYPE seoclsname VALUE 'CL_ABAP_CLASSDESCR'.
FIELD-SYMBOLS:
  <ls_interface> TYPE abap_intfdescr.

lo_class_description ?= cl_abap_classdescr=>describe_by_name( lc_class_name ).

LOOP AT lo_class_description->interfaces ASSIGNING <ls_interface>.
  WRITE:/ <ls_interface>-name.
ENDLOOP.

Does a class implement given interface?

DATA:
  lo_intf_description TYPE REF TO cl_abap_intfdescr.
CONSTANTS:
  lc_intf_name TYPE seoclsname VALUE 'IF_T100_MESSAGE',
  lc_class_name TYPE seoclsname VALUE 'CX_CDC_RECORDER'.

lo_intf_description ?= cl_abap_intfdescr=>describe_by_name( lc_intf_name ).

IF lo_intf_description->applies_to_class( lc_class_name ) = abap_true.
  WRITE: 'Class ', 
         lc_class_name, 
         ' implements the following interface: ', 
         lc_intf_name.
ELSE.
  WRITE: 'Class ', 
         lc_class_name, 
         ' does NOT implement the following interface: ', 
         lc_intf_name.
ENDIF.

All direct implementations of an interface

DATA:
  lt_implementations TYPE seor_implementing_keys.
CONSTANTS:
  lc_intf_name TYPE seoclskey VALUE 'IF_T100_MESSAGE'.


CALL FUNCTION 'SEO_INTERFACE_IMPLEM_GET_ALL'
  EXPORTING
    intkey       = lc_intf_name
  IMPORTING
    impkeys      = lt_implementations
  EXCEPTIONS
    not_existing = 1
    OTHERS       = 2.

CHECK sy-subrc = 0.

LOOP AT lt_implementations ASSIGNING FIELD-SYMBOL(<ls_implementation>).
  WRITE:/ <ls_implementation>-clsname.
ENDLOOP.

For further details to get PARENT-CHILD links between classes/interfaces, you can see the contents of the function module SEO_INTERFACE_IMPLEM_GET_ALL which reads data in form of CLASSREFCLASS from table VSEOIMPLEM.

Leave a Reply