Peter Friese

Developer Advocate / Mobile Developer / Public Speaker

How to Distinguish Generated Code From Non-generated Code

| Comments

It is a well-known fact that for most real-life applications, you canot use full code generation – some bits have to implemented in the source, not in the model. I sometimes find it funny to hear us software developers talk about business logic. If the business logic really would be logical in a mathematical sense, we’d be able to do full code generation. Alas, it is not, thus we are required to write some source code.

Over the last years, quite a number of patterns have evolved for integrating generated and non-generated code. Some of them are protected regions, generation gap, recipe frameworks, particial classes and annotated models.

In this post, I want to concentrate on protected regions and the generation gap pattern, as these two have turned out to be used most often in model driven software development.

The question is, how do you distinguish generated code from non-generated code. Looking at average enterprise / JEE applications, one can see that the amount of generated code is much larger than the amount of manually crafted code. The developers working on your projects need to quickly be able to navigate to the places in the source code where they may insert hand crafted code.

Here are two things you can do.

Use Eclipse Color Decorators If you use the generation gap pattern (illustrated below), you should set aside a source directory for all generated code, e.g. src-gen. Hand-crafted code (particularly the classes derived from the generated base classes) go to the src directory as usual.

The Generation Gap Pattern

Since all generated code is generated by your code generator, there is no need to check in generated code. You only have to version your model and the manually crafted code. To ensure developers do not accidentally check in generated code, you should add the src-gen directory to your .cvsignore (or .svnignore) file. Having the ignore file, you can now turn on Eclipse color decorators, which is the corner stone of this tip. Here’s how:

  1. Open the preference dialog (Window > Preferences)

  2. Navigate to Team > CVS > Label Decorations

  3. Tick the Enable font and color decorations checkmark

  4. Navigate to General > Appearance > Colors and Fonts by clicking on the Colors and Fonts hyperlink below the check box

  5. In this dialog, change the color for Ignored Resource (Foreground) to silver or light gray

As a result, all ignored resource will appear in light gray (or any other color you might have chosen): Decorating generated code files

Use MINT If you use protected regions, you need a way to decorate the parts in your code that won’t be touched by the generator. The Eclipse Modeling Framework Technology project (EMFT) has a subcomponent which was written with this in mind – MINT. Using MINT, you can highlight the parts of the source code which have been changed by you: Highlighting handcrafted code with MINT

Comments