Create Static code Templates in ABAP on Eclipse

ABAP development tools in Eclipse comes with a number of predefined code templates, but what are these? As understood by the name these are code templates that can be inserted inside an ABAP code, as and when required. This feature to insert static code templates in ABAP on eclipse can speed up developers coding efforts. Routine codes can be converted code templates and used thereby reducing error and increasing consistency.

Lets create a static code template in ABAP on Eclipse.

From the main menu of ADT in Eclipse, select Window -> Preferences. In the window, expand ABAP Developement -> Editors -> Source Code Editors -> ABAP Templates.

Create Static code Templates in ABAP on Eclipse

A close looks at the templates indicates that there are templates for ABAP and WebDynpro ABAP.

Below is list of ABAP templates available in ADT in Eclipse for ABAP:

assertEquals – for assert_equals ABAP unit
case – for Case block
catchAl – ##CATCH_ALL pragma
functionModuleParameter – Function module with parameter
if – if block
ifElse – if else block
lcl – Local class
lif – Local interface
loopAtAssigning – loop-at assigning block
needed – ##NEEDED pragma
noHandler – ##NO_HANDLER pragma
noText – ##NO_TEXT pragma
testClass – test class ( ABAP Unit )
textIdExceptionClass – default text id for exception class
tryCatchInfo – try-catch into block

Below is the list of predefined WebDynpro Templates in ADT for your reference:

firePlug – firePlug for use in ViewController only
generateMessage – generate message via message manager
instantiateUsedComponent – instantiate used component
personalization – personalization
portalIntegration – portal integration
raiseEvent – raise event

We can create our own static code template in ADT on Eclipse. Click on the New button on the window shown above. Fill in the template details like Name, Description and code template. Click Ok button to save the template.

Create Static code Templates in ABAP on Eclipse
cl_salv_table=>factory( 
     importing r_salv_table = ${alv_reference}
      changing t_table = ${data_table} ).
${alv_reference}->display( ).

The code prefixed with $ are known as placeholders. Later we should replace these placeholders with values or objects. Once you replace the placeholder with a value, it automatically replaces all occurrences of the placeholder with the value. This reduces repeated effort.

If you click on Insert Variable button, you see a lot of predefined placeholders that can be used in the template pattern. It includes cursor, date, dollar, enclosing_object, enclosing_package, line_selection, system_id, time, user, word_selection and year.

Create Static code Templates in ABAP on Eclipse

In order to use the template in ABAP program, just start typing few alphabets of the name of template and make use of Ctrl+Space and Shift+Enter keyboard shortcuts to insert the template in the ABAP code.

Another quick way of using the static code template in ADT is drag and drop from templates view into editor view.

Create Static code Templates in ABAP on Eclipse

This article explained how to create Static code template in Abap Development tool for eclipse and how to use it.