Chapter 11. Code Generation and Round-trip Engineering

Table of Contents
Generating Code
Fine-tuning code generation
Reverse-Engineering Code
Round-Trip Engineering

Generating Code

UML wouldn't be worth all the sophisticated work if all it came down to was pretty vector graphics. When analyzing and designing a software system, your final goal will be to generate well-implemented code.

Poseidon for UML provides a very powerful and flexible code generation framework, based on a template mechanism. It is currently used to generate Java and HTML code, but it is flexible enough to generate any kind of programming language, or other output, such as C++ or XML.

Java code generation is usually based on the classes of a model and other information displayed in the respective Class Diagrams. Additionally, Poseidon can generate setter and getter methods for the fields of each class.

By default, associations between classes in UML are bi-directional; that is, associations allow navigation between classes in both directions. For the common object-oriented programming languages, these need to be transformed into separate uni-directional associations. If one of these is set, the other should be set accordingly. The code for managing bidirectional as well as unidirectional associations is also generated automatically.

For generating the corresponding HTML documentation for your model, you need the UMLdoc Plug-in. The look and feel of the generated documentation is very similar to Javadoc. Poseidon for UML allows you to specify Javadoc information directly in your model (in the Documentation tab). This information, like comments to your classes or methods, is included in the code. But Javadoc alone gives a view on the code only, not on the model. For example, you do not see your diagrams. With this plug-in you get the same information as with Javadoc, in addition to all diagrams from your model. This includes Class Diagrams, your use case diagrams, sequence diagrams etc. This is valuable information that you would want in your documentation.

Both kinds of generation can be invoked from the generation menu. Select Generate Classes of Model and a dialog will appear. Here you can select or deselect model elements from the tree, specify an output and a template folder, indicate if the destination folder should be cleared and select either Java for code generation or HTML and UMLdoc for the documentation generation.

Figure 11-1. Code Generation Dialog — Java.

After the generation is finished, you will get a corresponding message in the Generator/Compiler output. Now you can open the HTML documentation in your favorite browser. UMLdoc generates an HTML page for the model overview, each package and each classifier (actors, use cases, classes, interfaces). They are connected by hyperlinks, so that you can easily navigate through the whole document.

Figure 11-2. Generated UMLdoc opened in Netscape.

You'll find the generated java files in the specified output folder, sorted by packages.

Generating code from state diagrams is another advanced possibility that is available in Poseidon as a plug-in, currently in an early stage. It is the Statechart-to-Java plug-in, where you can just draw your state diagram, then click a button and watch your diagram execute.

Moreover, the OCL-to-Java plug-in supports sophisticated generation of Java code from OCL constraints. Define your business rules in OCL, press a button and check your constraints in your running application.