The two knowledge points introduced in this article are relatively fragmentary, so I put them in the same article to introduce.

Jerry read this book while learning JavaScript while working on SAP CRM Fiori application development:

The book uses this syntax diagram to explain JavaScript syntax:

A similar syntax diagram exists in the ABAP help document, hidden in an obscure corner:

Take the keyword APPEND as an example to see the syntax diagram:

Click on the hyperlink and the syntax diagram is displayed, which I find much more intuitive than the text description in the help document:

Click on the question mark to see a detailed description of each component:

We sometimes find that we write ABAP code that does not work as expected at runtime, often because of a specular understanding of the functionality of the ABAP keyword as described in the ABAP help documentation. If you’re reading a keyword help document and it’s hard to understand, take a look at the syntax diagram for a change.

Jerry had studied SAP Commerce, and when he set up a development environment and executed the Ant All command line to build a project, he found that a large number of.Java files were automatically generated. My colleague told me that the contents of these Java files were generated based on a Java templating engine called Velocity.

These files within the Hybris installation package, ending in.vm, are Velocity template files:

When you open these template files, you’ll find that they consist of static content as shown in blue and variables as shown in red.

When Java files are generated based on these template files, the starting variable is replaced by the actual value computed at runtime. For example, the starting variable is replaced by the actual value computed at runtime. For example currentDateTime is replaced with the timestamp of the actual Ant All command-line build project.

An example of a Java file generated based on the template file shown above:

Is there a templating engine like Java Velocity in ABAP?

Like JavaScript, ABAP has the concept of String templates:

The ABAP string template is defined within two vertical lines, as shown in line 14 above. The string template contains static string text and ABAP variable names. At run time, these ABAP variables that act as placeholders are replaced by the values they actually contain.

Here is an example of a string template in JavaScript, with single quotes instead of vertical lines in ABAP:

Using ABAP’s string template, we can also implement requirements similar to Java Velocity for dynamically generated code. Jerry’s previous articles on Java and SAP ABAP’s static and dynamic proxies and ABAP’s attempts at section-oriented programming have been covered.

The source code of the ABAP class that needs to be created dynamically is pieced together with a string template, stored in the inner table LT_source, and then the keyword GENERATE SUBROUTINE POOL is called to create a temporary ABAP class whose life only exists for the duration of the current session.

If you want to generate a global ABAP class that can be persisted on an ABAP server and opened via SE24 and SE80, pass the cobbled inner table containing the source code for the ABAP class implementation to the ABAP class creation API, the function SEO_CLASS_CREATE_COMPLETE.

The following code creates a new ABAP class in the $TMP package based on the contents stored in the inner table mt_sourcecode.

These are the two most common ways ABAP generates code automatically.

In fact, there is a third method, which is used in SAP frameworks such as BRFplus to automatically generate ABAP classes:

Help documents can be found using the keyword ABAP CODE COMPOSER.

Here Jerry shows you how to automatically generate Code using ABAP Code Composer with a practical example.

As with Java Velocity, we automatically generate new code based on an ABAP template. Create an ABAP Include to act as a template, named ZSINGLETON:

The template code below defines the basic skeleton of an ABAP class implemented using the Singleton design pattern.



The opening and ending variables in this ABAP template act like variables in Java Velocity, acting as placeholders, and are replaced at run time by the actual values passed in by the template’s caller.

While @if and @end, like #if and #end of Velocity, provide simple conditional branching and control when generating actual code from templates.

My ZSINGLETON template defines a total of four variables:

  • IPARAM−cl A ss i_param-class IPARAM−class
  • IPARAM glob A l i_param-global IPARAM−global
  • IPARAM− I nte R f A ce i_param-interface IPARAM−interface
  • IPARAM−exce P t I on i_param-exception IPARAM−exception

Finally, write an ABAP program that consumes the ZSINGLETON template using Code Composer, passing in the actual values of the above four variables, and generate a persisted ABAP singleton class named ZCL_JERRY as specified in line 18.

Below is the source Code of the ABAP class generated by the ABAP template and ABAP Code Composer. You can see that the variables defined by $in the template have been replaced by the actual values passed in when the template was called to generate the ABAP class.

Hopefully, this article has given you an idea of several ways to automate code generation in SAP ABAP. Thanks for reading.

For more of Jerry’s original articles, please follow the public account “Wang Zixi “:

ABAP project

  • Jerry’s ABAP, Java and JavaScript stew
  • What should ABAP developers learn in the future
  • Jerry’s 2017 May Day holiday:ABAP implementation of 8 classical sorting algorithms
  • Jerry’s collection of original ABAP technical articles
  • Three hundred lines of ABAP code implement a simplest blockchain prototype
  • Call functions in ABAP on-premise system using Java+SAP Cloud Platform +SAP Cloud Connector
  • Consume ABAP on-premise OData services in the CloudFoundry environment of SAP cloud platform
  • ABAP vs Java, breaststroke vs freestyle
  • Talk about C and ABAP
  • Use ABAP Channel to develop some small tools to improve daily work efficiency
  • All the boring things I’ve done with ABAP
  • Don’t like SAP GUI?Try using Eclipse for ABAP development
  • Write and activate ABAP Code using Visual Studio Code
  • Has your ABAP program been opened to Buddha?Let’s try Jerry’s trick
  • Write the first ABAP program in SAP cloud platform ABAP programming environment
  • ABAP programming specification issued by SAP
  • Do you know the hidden features of ABAP Code Inspector?
  • Are you using ABAP for secondary development of SAP products? Learn about this new concept of secondary development
  • The parasitic programming language inside ABAP Netweaver
  • Start with a blog post on the SAP community to talk about the sentiment behind SAP product naming
  • Cloud ABAP Restful service development
  • How to expose CDS View as OData service in SAP cloud platform ABAP programming environment
  • Use abapGit to transfer code between the ABAP ON-premises system and the SAP cloud platform ABAP environment
  • 30 minutes To develop a Fiori application with Restful ABAP Programming model to support add, delete, change, and query
  • Jerry walks you through the implementation of Action and Validation, part 2 of the Restful ABAP Programming Model series
  • Jerry takes you through this series of Restful ABAP Programming models:Cloud ABAP application debugging
  • How to consume third-party services in ABAP programming environment on SAP cloud platform
  • It’s time for ABAP developers to go to the cloud – now you can use a free trial version of the ABAP environment for SAP cloud Platform
  • The origin and application scenario of ABAP programming environment on SAP cloud platform
  • Trident application in SAP cloud platform
  • How to develop and deploy a Fiori application that supports add, delete, modify and query based on Restful ABAP Programming model
  • SAP 2019 TechEd Key Note: How do SAP practitioners do secondary development in the cloud era?
  • What ABAP keywords and syntax won’t work in the ABAP cloud environment?
  • The ABAP development environment finally supports automatic formatting of ABAP variable names with camel name
  • Complete a real work task using ABAP 740’s new REDUCE keyword
  • A shiver of ABAP code
  • Yesterday’s Halloween ABAP monster code mystery, revealed the answer
  • This paper introduces an efficient copy method of inner table in ABAP kernel state
  • A practical example of developing OData using the SAP Cloud Application Programming model
  • When ABAP met Prometheus
  • Draw scalable vector diagrams using ABAP
  • ABAP development environment syntax highlighting things
  • Seven weapons for SAP error message debugging:Enable all error messages to be located
  • Several methods of using ABAP to operate Excel
  • SAP GUI in the favorites transaction code management tool
  • SAP GUI and Windows registry
  • Can you do bad things with Debug permission? Be careful. Your every move is on the system
  • ABAP CCDEF, CCIMP, CCMAC, CCAU, CMXXX
  • Three ways to implement ABAP conditional breakpoints
  • Use SAT trace to monitor the performance and call stack of SAP applications opened from the browser
  • Advice from a 13-year ABAP veteran: Knowing the basics will never hurt ABAP development
  • SAP ABAP Netweaver containerization, impossible task?
  • SAP Product enhancement Technology Review
  • SAP API development methods
  • Brief introduction to Java and SAP ABAP static proxy and dynamic proxy, and ABAP tangential programming attempt
  • SAP ABAP application server HTTP response Status Code
  • Is there a Java List collection utility class in SAP ABAP? CL_OBJECT_COLLECTION take a look
  • ABAP interview question series: Write an ABAP procedure that creates a Deadlock
  • This section describes how to log in to the SAP ABAP Netweaver server