Skip to Content
Author's profile photo Jerry Wang

Step by Step to generate ABAP code automatically using Code Composer

Today I am going through the SAP help for BRFplus stuff and come across with some introduction about ABAP code composer.

I would like to share with you a very simple example to demonstrate its logic.

/wp-content/uploads/2015/09/clipboard1_788067.png

How to find the above help document in a quick way? Just google with key word “ABAP CODE COMPOSER” and click the first hit.

/wp-content/uploads/2015/09/clipboard2_788068.png

And here below are steps how to generate ABAP codes which contains a singleton pattern using ABAP code composer.

1. Create a new program with type “INCLUDE”:

/wp-content/uploads/2015/09/clipboard3_788111.png

And paste the following source code to include and activate it:

*---------------------------------------------------------------------*
*       CLASS $I_PARAM-class$ DEFINITION
*---------------------------------------------------------------------*
*       Instance pattern: SINGLETON
*---------------------------------------------------------------------*
CLASS $I_PARAM-class$ DEFINITION
@if I_PARAM-GLOBAL @notinitial
  \ PUBLIC
@end
\ FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    INTERFACES:
      $I_PARAM-interface$.
    CLASS-METHODS:
      s_get_instance
        RETURNING
          value(r_ref_instance) TYPE REF TO $I_PARAM-interface$
@if I_PARAM-exception @notinitial
        RAISING
          $I_PARAM-exception$
@end
\.
  PRIVATE SECTION.
    CLASS-DATA:
      s_ref_singleton TYPE REF TO $I_PARAM-interface$.
    CLASS-METHODS:
      s_create_instance
        RETURNING
          value(r_ref_instance) TYPE REF TO $I_PARAM-class$
@if I_PARAM-exception @notinitial
        RAISING
          $I_PARAM-exception$
@end
\.
ENDCLASS.                    "$I_PARAM-class$ DEFINITION
*---------------------------------------------------------------------*
*       CLASS $I_PARAM-class$ IMPLEMENTATION
*---------------------------------------------------------------------*
*       Instance pattern: SINGLETON
*---------------------------------------------------------------------*
CLASS $I_PARAM-class$ IMPLEMENTATION.
************************************************************************
*       METHOD S_CREATE_INSTANCE
*----------------------------------------------------------------------*
*       Constructs an instance of $I_PARAM-class$
*......................................................................*
  METHOD s_create_instance.
*    RETURNING
*      value(r_ref_instance) TYPE REF TO $I_PARAM-class$
@if I_PARAM-exception @notinitial
*    RAISING
*      $I_PARAM-exception$
@end
************************************************************************
@if I_PARAM-exception @notinitial
    DATA:
      l_ref_instance TYPE REF TO $I_PARAM-class$.
************************************************************************
    CREATE OBJECT l_ref_instance.
@slot object_construction
*   Construction of the object which can lead to $I_PARAM-exception$
@end
    r_ref_instance = l_ref_instance.
@else
    CREATE OBJECT r_ref_instance.
@end
  ENDMETHOD.                    "s_create_instance
************************************************************************
*       METHOD S_GET_INSTANCE
*----------------------------------------------------------------------*
*       Keeps track of instances of own class -> only one
*......................................................................*
  METHOD s_get_instance.
*    RETURNING
*      value(r_ref_instance) TYPE REF TO $I_PARAM-interface$
@if I_PARAM-exception @notinitial
*    RAISING
*      $I_PARAM-exception$
@end
************************************************************************
    IF s_ref_singleton IS NOT BOUND.
      s_ref_singleton = s_create_instance( ).
    ENDIF.
    r_ref_instance = s_ref_singleton.
  ENDMETHOD.                    "s_get_instance
ENDCLASS.                    "$I_PARAM-class$ IMPLEMENTATION

The string wrapped with a pair of @,for example, the string “$I_PARAM-class$”, acts as a importing parameter of code composer, which means during the code generation, you must tell code composer what is the actual class name in generated code, by passing the actual name to this parameter.

This activated include will act as a code generation template. We now have the following importing parameter:

  • $I_PARAM-class$
  • $I_PARAM-global$
  • $I_PARAM-interface$
  • $I_PARAM-exception$

2. create another driver program which will call code composer API to generate the code with the help of the template include created in step1. The complete source code of this program could be found from attachment.

/wp-content/uploads/2015/09/clipboard5_788112.png

I just use the cl_demo_output=>display_data( lt_tab_code ) to simply print out the source code.

In the output we see all of the placeholder ( $XXXX$ ) in the template have been replaced with the hard coded value we specify in the driver program.

/wp-content/uploads/2015/09/clipboard6_788113.png

Although the google result shows the code composer API is marked as for SAP internal use only and thus could not be used in application code, however I think we can still leverage it to design some tool which can improve our daily work efficiency.

/wp-content/uploads/2015/09/clipboard7_788117.png

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Aditya Verma
      Aditya Verma

      Attachment is missing, can you add them please?

      Author's profile photo Anisha Benjamin
      Anisha Benjamin

      Hi Jerry,

      How do you loop in the template? for example- we need to create a set of objects named ob1, ob2.. obn for a class.