When you transform a UML model to Java code with the Replace UML elements option selected, objects in the UML model are replaced with corresponding shortcuts to generated Java files. You can use the Undo button to undo the operation. However, if you then click the Redo button, the model loses all shortcuts to the previously generated Java files. To work around this issue, do not use the Redo button. Instead, run the UML to Java transformation again.
A UML to Java transformation uses @generated tags to identify generated code. You can remove or modify these @generated tags to take ownership of code and to indicate that it should not be overwritten by subsequent transformations.
Alternatively, you can use code templates to include // begin-user-code and // end-user-code segments to identify code that should not be removed by subsequent transformations.
To create these segments, you must change the Java code template using the following steps:
- Click Window > Preferences.
- Expand Java > Code Style > Code Templates.
- On the Code Templates page, expand Code and click Method Body.
- Click Edit.
- Type the following pattern and click OK:
// begin-user-code
// ${todo} Auto-generated method stub
${body_statement}
// end-user-codeNOTE: You must include a new line after the // end-user-code segment or exceptions are thrown during Java transformations.
Methods generated by UML to Java transformations that use the new Java code template would then look like the following example:
/**
* @generated "UML to Java (com.ibm.xtools.transform.uml2.java.internal.UML2JavaTransform)"
*/
public void aMethod() {
// begin-user-code
// TODO Auto-generated method stub
// end-user-code
}Code that you insert between the // begin-user-code and // end-user-code segments is always preserved.
NOTE:
- This technique does not work with code that is generated from patterns; however, it does work with code that is generated from Java or EJB transformations.
- This technique does not work if you type the // begin-user-code and // end-user-code segments in the generated code. You must include these segments in the Java code template.
A UML to Java transformation might not update a Java element if two Javadoc comments precede the Java element.
For example, if a transformation generates a Java element that contains a @generated tag in its Javadoc, and another Javadoc comment precedes the Java element, that element (for example, transformGenerated()) does not update when the transformation is reapplied.
/**
* A commented user-created method
*/
// private void userCreated() {
// }
/**
* @generated "..."
*/
public void transformGenerated() {
}To work around this issue, comment out complete Java elements (complete method, field, class or interface definitions) and move them, including any preceding comments, to the end of the Java source file before the final brace (}). The original example would then look like the following:
/**
* @generated "..."
*/
public void transformGenerated() {
}
... end of source file, before final brace ...
/**
* A commented user-created method
*/
// private void userCreated() {
// }
A UML to Java transformation might remove a Java element from the source code if it is no longer specified in the source model.
For example, when a transformation is reapplied, if a method (for example, importantUserCreated()) is not defined in the source model, that method is removed from the source code, along with the commented code that contains the Javadoc.
/**
* @generated "..."
*/
// public void generatedMethodUserWantsToSave() {
// }
private void importantUserCreated() {
}To work around this issue, comment out complete Java elements (complete method, field, class, or interface definitions) and move them, and any preceding comments, to the end of the Java source file before the final brace (}). The original example would then look like the following example:
private void importantUserCreated() {
}
... end of source file, before final brace ...
/**
* @generated "..."
*/
// public void generatedMethodUserWantsToSave() {
// }
Return to the main readme file