Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Introspection/reflection of a class or function module

Former Member
0 Likes
574

Dear all,

Is there a way of performing introspection on classes or function modules in ABAP? For example, I would need a piece of code that gets the attributes and their types of a class that was defined in the class builder.

Thanks,

Philon

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
417

Sure, check this out.



REPORT zrich_0003
       LINE-SIZE 200.


DATA: l_dref  TYPE REF TO cl_abap_typedescr.
DATA: l_clref  TYPE REF TO cl_abap_classdescr.
DATA: x_attributes TYPE abap_attrdescr.

CONSTANTS: query_class TYPE seoclsname
                    VALUE 'CL_GUI_ALV_GRID'.

* check if query_class exists in the current system
CALL METHOD cl_abap_classdescr=>describe_by_name
  EXPORTING
    p_name         = query_class
  RECEIVING
    p_descr_ref    = l_dref
  EXCEPTIONS
    type_not_found = 1
    OTHERS         = 2.
IF sy-subrc <> 0.
  EXIT.
ENDIF.

l_clref ?= l_dref.

LOOP AT l_clref->attributes INTO x_attributes.

  WRITE:/ x_attributes-name,
           x_attributes-type_kind,
           x_attributes-length,
           x_attributes-visibility.

ENDLOOP.

Regards,

RIch Heilman

1 REPLY 1
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
418

Sure, check this out.



REPORT zrich_0003
       LINE-SIZE 200.


DATA: l_dref  TYPE REF TO cl_abap_typedescr.
DATA: l_clref  TYPE REF TO cl_abap_classdescr.
DATA: x_attributes TYPE abap_attrdescr.

CONSTANTS: query_class TYPE seoclsname
                    VALUE 'CL_GUI_ALV_GRID'.

* check if query_class exists in the current system
CALL METHOD cl_abap_classdescr=>describe_by_name
  EXPORTING
    p_name         = query_class
  RECEIVING
    p_descr_ref    = l_dref
  EXCEPTIONS
    type_not_found = 1
    OTHERS         = 2.
IF sy-subrc <> 0.
  EXIT.
ENDIF.

l_clref ?= l_dref.

LOOP AT l_clref->attributes INTO x_attributes.

  WRITE:/ x_attributes-name,
           x_attributes-type_kind,
           x_attributes-length,
           x_attributes-visibility.

ENDLOOP.

Regards,

RIch Heilman