Disclosed is a mechanism for keeping consistency between model files and their generated code. The mechanism detects the changes of source code written in Java* programming language at compilation time by embedding hash code calculated by the body of a method in the code and using the annotation technology of Java*. In application development with a code generator using model files, often written in XML format, manually changing the generated code by the developers should be restricted, because the changes might be lost when the user of the generator generates again and unintentionally overwrites them. This disclosed detects the changes and avoid that situation by embedding hash codes calculated from the body of each method into the code in form of a specific annotation for each method when the code is generated, and comparing the value of the annotation and newly calculated value from the body of the generated method when the code is compiled.
Maintaining consistency between model and source code with code generator
Figure 1 shows the mechanism of this disclosed.
a) Embedding hash code.
Annotation
HashCode A
Model File
HashCode A
public class Employee { @Generated (value=HashCode A) public void methodA {
Generated Code; ・・・
}
calculates
Method
Generated Code
Source Code Image
Generator
Compilation Error
notifies
b) Comparing hash codes.
Compiler
recognizes calls
public class Employee { @Generated (value=HashCode A) public void methodA {
Changed Code; ・・・
}
Developer
Validation Program
compares
HashCode B
Annotation
HashCode A
Method
Changed Code
Changes
calculates
Figure 1 Mechanism of detecting method changes.
The code generator can detect the changes along the following steps, embedding and comparing phase.
a) Embedding hash code.
1. The generator creates a source code image from a model file.
2. The generator calculates a hash code for each method from a method of the source code image. Though there is no constraint which algorithm should be used to calculate the hash code, it must have enough length to identify the changes.
3. The generator embeds the hash code into the source code image in form of annotation for each method.
4. The generator generates source code from the image.
b) Comparing hash codes.
1. Compiler begins to compile the generated source code.
2. Compiler r...