Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline
Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Subscription to Java Pro

Writing a Code Generator in Java (Continued)

Approaches
We can classify a code generator according to the type of input. The two major approaches are code driven and model driven.

A code-driven generator takes as input a file containing source code and special tags, which drive the code generation process. JavaDoc is an example of such a generator. In fact, it takes as input Java source code containing special comments and generates HTML code representing the documentation of that code. Another example of a code-driven generator is XDoclet (see Resources). It is a very popular code generator for Java with the primary goal of generating EJB code, starting from particular JavaDoc tags within the entity beans code.

Model-driven generators can also subclassified into two types: custom and MDA. A custom generator takes as input a proprietary model representing the information that must be converted in source code. Using XSLT or template engines, such as Apache Velocity, it is quite easy to generate code according to the metadata coming from the model.

When the model as input is a representation of UML, then the code generator follows the Model-Driven Architecture (MDA). MDA is an Object Management Group (OMG) initiative to create a standard for code generation. An MDA code generator takes as input a platform-independent model (usually XMI, an XML representation of UML) and turns it into a platform-specific model that, by means of templates, can be converted easily in source code (see Resources).

Architecture
The code generator designed partially here follows MDA. I say "partially" because being very flexible it could also be used for code-driven and custom, model-driven approaches. The code generator consists of three different modules:

  • Importer – reads the model as input (not necessarily UML) and translates it in a platform-independent internal format based on an object model. An importer could be seen as a sort of DOM parser. But, as you will see, it could be implemented for working with any kind of input you want.
  • Internal Object Model (IOM) – is the platform-independent internal format and could be considered as the core of the code-generator architecture. IOM contains a set of classes that make it easy to manipulate the information coming from the model to generate outputs. The structure of the object model is a very important issue—when well designed it can be a powerful target-technology, independent representation that can be converted easily in source code.
  • Exporter – accesses the IOM and takes the relevant information to generate code. It could use templates, which drive the generation process.

Figure 1 diagrams the architecture of the proposed code generator. I have designed this architecture with these benefits in mind:

  • The input is completely independent of the output in terms of technologies. For instance, you can create an importer that reads a UML diagram and then an exporter writing C#-ADO code and another one writing Java-JDBC code, but both work with the same UML model as input.
  • If the IOM is implemented according to UML rules, you can represent everything you can do with UML, which means that every kind of input can be represented and converted into a real object-oriented design. It is a big advantage in writing importers and exporters manipulating the model.
  • By implementing multiple exporters and applying a simple pattern, you can create different layers of code—source code, descriptors, documentation, scripts, and so on—with a single pass that assures you consistency and synchronization among the different outputs.

I believe that such an ambitious code generator can be implemented effectively using a modern imperative programming language like Java, C#, C++, Perl, or Python. It would be too much complexity to write it by using other technologies like transformations or template languages. However, it is also true that using XSLT or Velocity Template Engine for specific duties can be faster and more effective, especially when you just want to generate code covering simple aspects of your project.

I'm proposing an architecture for a code generator on which you can base your whole project or a major part of it. It is very flexible, easy to maintain, intuitive to work with, and based firmly on a powerful internal object model. The last point is the major lack of transformation and template-only approach: the internal model is often poor and rigid or, in same cases, even missing.

Moreover, using an imperative language you can use powerful features and libraries when implementing importers, an internal model, and exporters. For example, by using Java, you can take advantage of aspects such as interfaces, reflection, and exception handling that will make the code generator very extensible, reliable, and robust.




Back to top













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home