Application Development 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: 

How to get the name of the class which is currently in use

Former Member
0 Kudos

Hi all,

I have two classes Mother and Child, and I use this code to print the name of the class in use

DATA m TYPE REF TO zmother.

DATA class_name type string VALUE 'CHILD'.

CREATE OBJECT m TYPE (class_name).

CALL METHOD m->PrintName .

So, what is the source code that I have to put in the PrintName methode?

Thanks.

1 ACCEPTED SOLUTION

former_member188685
Active Contributor
0 Kudos

you can try like this..

REPORT  ztest_method.

*----------------------------------------------------------------------*
*       CLASS c1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 DEFINITION.
  PUBLIC SECTION.
    METHODS: get_data.
ENDCLASS.                    "c1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 IMPLEMENTATION.

  METHOD get_data.

  ENDMETHOD.                    "get_data

ENDCLASS.                    "c1 IMPLEMENTATION

DATA oref TYPE REF TO object.
DATA: method TYPE string.
DATA: cla_name(20).

START-OF-SELECTION.
  cla_name = 'C1'.
  CREATE OBJECT oref TYPE (cla_name).
  BREAK-POINT.

  CALL METHOD oref->(method).

8 REPLIES 8

former_member188685
Active Contributor
0 Kudos

you can try like this..

REPORT  ztest_method.

*----------------------------------------------------------------------*
*       CLASS c1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 DEFINITION.
  PUBLIC SECTION.
    METHODS: get_data.
ENDCLASS.                    "c1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 IMPLEMENTATION.

  METHOD get_data.

  ENDMETHOD.                    "get_data

ENDCLASS.                    "c1 IMPLEMENTATION

DATA oref TYPE REF TO object.
DATA: method TYPE string.
DATA: cla_name(20).

START-OF-SELECTION.
  cla_name = 'C1'.
  CREATE OBJECT oref TYPE (cla_name).
  BREAK-POINT.

  CALL METHOD oref->(method).

0 Kudos

Ok, but how can I get the name of the object class that calls the methode!


REPORT  ztest_method.
 
*----------------------------------------------------------------------*
*       CLASS c1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 DEFINITION.
  PUBLIC SECTION.
    METHODS: get_data.
ENDCLASS.                    "c1 DEFINITION
 
*----------------------------------------------------------------------*
*       CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 IMPLEMENTATION.
 
  METHOD get_data.
*****************************************************************************
****** What is the code to put here to know the name of this Class?*
*****************************************************************************
  ENDMETHOD.                    "get_data
 
ENDCLASS.                    "c1 IMPLEMENTATION
 
DATA oref TYPE REF TO object.
DATA: method TYPE string.
DATA: cla_name(20).
 
START-OF-SELECTION.
  cla_name = 'C1'.
  CREATE OBJECT oref TYPE (cla_name).
  BREAK-POINT.
 
  CALL METHOD oref->(method).

0 Kudos

Hi,

You can check the below link. it may be useful for you

Cheers,

Kothand

0 Kudos

why do you need to know the object name..? it is completely depends on the class. in the example get_data is used to get the data from some db table.

what exactly you need, can you explain in detail,.

0 Kudos

I programming a framwork, and I want to know the name the class to call the appropriate Transformation programme.

So I don't want to call the transformations in each Child class, but to call it ones in the Mother class.

0 Kudos

You can use the SYSTEM_CALLSTACK function module.

Read this thread, Uwe has given details on both getting the calling class and method:

Most probably this solves your needs.

Regards

0 Kudos

An other question, please. How can I retrieve Information about a class attributs by coding?

And is it possible to access to the attributs of the Child class from the Mother class?

And thinks for the answer

Hi,

you can use run-time type identification as in the following example.

REPORT z_demo_classdescr.

*----------------------------------------------------------------------*
*       CLASS lcl_myclass DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_myclass DEFINITION.
  PUBLIC SECTION.
    METHODS main.
ENDCLASS.                    "lcl_myclass DEFINITION

DATA:
  gr_myobj TYPE REF TO lcl_myclass.

START-OF-SELECTION.
  CREATE OBJECT gr_myobj.
  gr_myobj->main( ).

*----------------------------------------------------------------------*
*       CLASS lcl_myclass IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_myclass IMPLEMENTATION.
  METHOD main.
    DATA:
      lv_name  TYPE        string.
    lv_name = cl_abap_classdescr=>get_class_name( me ).
    WRITE: / lv_name.
  ENDMETHOD.                    "main
ENDCLASS.                    "lcl_myclass IMPLEMENTATION

Cheers,

Thorsten