WhyWaitForever IT Consultancy

Code Generator

A code generator is critical in producing factory quality software components that provide the base on which business processes are implemented. It is used to generate repetitive program and script source code. The generated code is produced in a independent accessible form that can be used with most development approaches including those that exploit integrated development environments and other code generators.

The code generator has been used with Eclipse , XDoclet , Ant and JUnit .

Usage

The code generator is a simple entry level tool. It provides a good introduction to a subject that can appear complex. It is a reasonable introduction to the more sophisticated set of tools that are XDoclet .

In the supplied JavaBean Java source code example the code generator generates Java classes and interfaces. These include not just getter and setter methods but also the validation status, validation messages and validate methods.

The code generator does not enforce mandatory naming conventions but it is recommended that the conventions used in the examples for classes, interfaces, methods, fields and field attributes are followed.

Though the avoidance of code duplication is advisable in most circumstances the code generator can be used to produce multiple versions of duplicate code. Sometimes extension of Java classes may not be the optimum approach.

The code generator is simple in implementation in that all that is required is Java and Ant core tasks together with user editable templates and code fragments. The code generator can be run directly within Ant or within an Ant aware development environment such as Eclipse . No additional Java jars or run time libraries are required. No registry changes are required on Windows operating systems.

History

Over the years there have been many code generators. Most have passed into history. Many long term survivors are "preprocessors" for programming languages such as PL/1 and COBOL. There are few if any general purpose code generators that continue to generate code in the same way they did even five years ago. The current generation of sophisticated code generators is best represented by XDoclet . History suggests it is a better to use the best general purpose tools of the time to implement code generation.

An ideal code generator needs to allow the editing of simple property files and simple code fragment templates. It needs to process these editable files to generate output code files using general purpose copy and string replacement utilities.

The approach is not new. Anyone involved in serious amounts of programming built their own tool set to reduce development effort and reduce costs.

The approach is the important factor. The tools are less important.

An optimum approach must avoid "lock-in". It must allow for easy migration to better and more capable tools. The key editable elements should be held as text files. These are the easiest to transform into the format for the current generation of tools.