神刀安全网

Code Generation and Templating Made Really Easy

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

Quick Introduction

Example 1: Letter Template

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

Code Generation and Templating Made Really Easy

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

Code Generation and Templating Made Really Easy

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.

Code Generation and Templating Made Really Easy

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.

Code Generation and Templating Made Really Easy

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 ( MrMs ). The text representation of subtemplate  Mr via the  toString() -method is "Mr.".

Code Generation and Templating Made Really Easy

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

Code Generation and Templating Made Really Easy

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 .

Code Generation and Templating Made Really Easy

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.

Code Generation and Templating Made Really Easy

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

Code Generation and Templating Made Really Easy

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

Code Generation and Templating Made Really Easy

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

Code Generation and Templating Made Really Easy

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 …

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Code Generation and Templating Made Really Easy

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址