Applying Code Generation Techniques to the J2EE Development
Developing enterprise software requires a mixture of two mindsets: the creative and the mundane. The creative mindset calls for software engineers to apply their expertise to the task of building reliable, scaleable solutions. For the mundane, however, software engineers must resign themselves to the drudgery of the many repetitive tasks that are an all too common part of enterprise software development.
Code generation methods offer a means of delivering enterprise solutions extremely rapidly by reducing the mundane, repetitive tasks developers face. Consequently, code generators have tremendous potential for rapid development projects, and if used correctly, they can both shorten development timeframes and improve software quality and accuracy. Moreover, code generation can also make the software development process a far more enjoyable experience by freeing developers of routine tasks, enabling them to focus their attention on building better solutions.
This chapter discusses the different approaches to code generation and looks at best practices for applying code generation techniques to the development of enterprise software for the J2EE platform. Specifically, we cover a new programming paradigm, attribute-oriented programming, and explore how this and other code generation techniques can deliver higher developer productivity and more accurate software, and can help make the project team significantly more agile.
To achieve this aim, this chapter covers the following areas:
- The different types of code generators and the benefits they provide
- Apache's Velocity template engine for building custom code wizards
- Attribute-oriented programming for simplifying the development of J2EE components using XDoclet
- The issues involved in making code generation techniques part of the development process
Collectively, we'll see how the many facets of code generation can facilitate an adaptive approach to software development.
What Is Code Generation?
Code generators are software constructs that write software. It is code-writing code.
Code generation is a technique already common on development projects. The code wizards offered by most integrated development environments (IDE) are a form of template-driven code generation with which most of us are familiar. Other examples include the forward-engineering capabilities of modeling tools and the use of the Java decompiler (JAD) to reverse-engineer Java source from bytecode.
The use of code generation techniques is wide and varied, but code generators can be loosely categorized as either passive or active [Hunt, 1999].
Passive code generators.
These code generators are run once to provide the developer with a flying start. They produce code that the developer then modifies. Code wizards are an example of passive generators.
Active code generators.
Code generators that fall into the active category produce code that is continually regenerated throughout the project. The code produced is not modified by the developer and is overwritten each time the generator is run.
The distinction between active and passive code generation is a subtle one and essentially comes down to how the generated code is maintained over the course of the project. If the code generator is made part of the build process and generates fresh source each time a build is instigated, then we are using active generation. If the output from the same generator is taken by the software engineer, modified, and placed under source control, then we have passive code generation.
Both techniques can save time and effort, although the two techniques are applied quite differently. These next sections examine the uses of each type of code generation.