Quantcast
Channel: ABAP Development
Viewing all articles
Browse latest Browse all 948

Step by Step to generate ABAP code automatically using Code Composer

$
0
0

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.

clipboard1.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.

clipboard2.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":

clipboard3.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.

clipboard5.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.

clipboard6.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.

clipboard7.png


Viewing all articles
Browse latest Browse all 948

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>