DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Code Generation and Templating Made Really Easy

A new approach for code generation is presented. It consists of a powerful template engine which works quite differently from other approaches.

Robert Mencl user avatar by
Robert Mencl
·
May. 09, 16 · Tutorial
Like (4)
Save
Tweet
Share
19.40K Views

Join the DZone community and get the full member experience.

Join For Free

Templates are pre-compiled during automatic build of the IDE into so-called template beans (POJOs). By design, application logic is written in user-defined Java code which uses template beans for the data insertion process. Each template bean can render its contents to a text string by simply calling the toString()-method. If previously generated source files have to be updated, the code generation framework also offers support for protected regions which leave manually written text regions unchanged. 

In the following, we list some of its features and continue with a quick introduction.

Features

  • Compile-time safety for your templates,
  • Full control of code generation & template engine via Java,
  • Very effective and comprehensible template notation,
  • Template notation symbols can be changed dynamically according to your target platform (if needed),
  • Strict separation of code (generator) and design (template),
  • Templates do not carry any model-specific information ⇒ completely re-usable across different projects,
  • Syntax highlighting for the target language and not for the template notation,
  • Supports protected regions,
  • Supports any type of model for which an API exists,
  • Supports each IDE (no plug-ins necessary),
  • Easily extensible via Java,
  • No polyglot programming

Quick Introduction 

Example 1: Letter Template

Below we see a simple example template for a letter or email.

Templates are simple text files enriched with template notation elements in order to work with placeholders.

There are only two basic structures for placeholders: variables and subtemplates. In the example letter, inline subtemplates are used, a special case of subtemplates. The red template notation elements are symbols only. There are no special keywords. The green identifiers can be chosen arbitrarily by the user.

The template is pre-compiled during the automatic build process of the IDE into a so-called template bean. The green text elements of the template become nested structures (classes, fields, ...) within the template bean class.

Parameters can be simply filled into the template bean as shown in the lower left part of the next picture. The text representation of the template bean Letter_jgt delivers the template text with inserted parameter values ("Smith", "Jenny Jones") and subtemplate instances (Mr, Ms). The text representation of subtemplate Mr via the toString()-method is "Mr.".

The text output of template bean Letter_jgt is shown in the lower right of the picture below.

Example 2: Java Class Template

The next example shows a Java class template Class.jgt which uses two additional subtemplates: Attribute.jgt and GetterSetter.jgt.

The subtemplate structure identifiers like "foreachAttribute" can be chosen by the template designer totally arbitrarily. It does not matter if we write "pourChaqueElement" (french) or "fuerJedesAttribut" (german), or separate the single words with spaces as in "for each attribute". This identifier is always compiled into the corresponding structure within the template bean with the same name and can be used for the (multiple) insertion of subtemplate instances.

The picture below depicts the translation of the Java class template into the template bean class via automatic build of your IDE.

The attribute template Attribute.jgt is rather short and consists of only two variable values: DataType  &  AttributeName.

Getters and Setters are defined in the GetterSetter.jgt template which is compiled into the template bean class GetterSetter_jgt.

Insertion of values into the Java class template is performed in the same way as in the letter template example. The data can be obtained from any type of model for which a Java API exists. 

All other features (more notation elements, indenting in templates, value propagation, dynamic change of notation symbols, protected regions, configuration, ...) are described in the tutorial and handbook.

Tutorial and Handbook

Distribution

The distribution consists of a Maven project with an example for a code generation application, generator templates, and the PDF version of the Tutorial & Handbook. It is sufficient to just import it as Maven project into your IDE.  

Download the code generation framework here ...

Template

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Build a Spring Boot GraalVM Image
  • Comparing Map.of() and New HashMap() in Java
  • Create Spider Chart With ReactJS
  • Best CI/CD Tools for DevOps: A Review of the Top 10

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: