Considerations for rerunning UML-to-Java transformations

The UML-to-Java (deprecated) transformation and the UML-to-Java transformation add the @generated tag to certain generated elements. When you rerun a transformation, the transformation overwrites elements with the @generated tag. The transformation also overwrites qualifiers and members of classes, and qualifiers, parameters, and the body of methods.

Before you run a UML-to-Java transformation, you can remove the @generated tags from the elements that you do not want the transformation to overwrite. Instead, you might want to edit the @generated tags by inserting characters after the @ character; for example, @wasgenerated. Inserting characters can help you to identify the generated code that the transformation does not overwrite when you rerun the transformation.

Elements that the transformation generates

The UML-to-Java transformations add the @generated tag to the following generated elements:
  • Classes
  • Interfaces
  • Fields
  • Methods

Protecting generated code

When you rerun a UML-to-Java transformation, by default, the transformation does not overwrite method bodies that the transformation generates, or that a user creates. The transformation always preserves the method bodies between the comments // begin-user-code and // end-user-code, whether or not you remove or edit the @generated tag.

Although the transformation always preserves method bodies, you must remove or edit the @generated tag from a method to preserve structural changes to the method. For example, you must remove or edit the @generated tag for the method if you change the method signature, or rename the method.

If you do not remove or edit the @generated tag, the transformation overwrites the elements to which the @generated tag applies.

Note: To add the method to the source UML model, run the reverse transformation.
In this example, the source UML model contains a UML class named Class1, which contains an integer attribute named attribute, and two operations named Operation1 and Operation2 that do not have any parameters. The following table lists examples of elements that the transformation preserves and overwrites. The Modified code column lists the code that contains the following changes:
When you rerun the transformation, it does the following things:
  • Preserves the inheritance because the @generated tag was removed
  • Overwrites the changes to the method signature for the Operation1 method, because the @generated tag was not removed
  • Does not preserve the changes to the Operation1 method body because the transformation cannot locate the method to which the new method body belongs
  • Preserves the changes to the Operation2 method body
UML model element Generated code Modified code Generated code after you rerun the transformation
This image shows the UML class named Class1 that is described in the preceding paragraph. Class1.java:
/** 
 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
 */
public class Class1 {
	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	private Integer attribute1;

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void Operation1() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void Operation2() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}
}
Class1.java:
/** 
  */
public class Class1 extends MyClass {
	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	private Integer attribute1;

	/**
	 * @generated  "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void Operation1(int parameter1) {
		// begin-user-code
		// TODO Auto-generated method stub
		int i = 2;
		System.out.println(i);
		// end-user-code
	}

	/**
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void Operation2() {
		// begin-user-code
		// TODO Auto-generated method stub
		int j = 2;
		System.out.println(j);
		// end-user-code
	}
}
Class1.java:
/**
 * 
 */

public class Class1 extends MyClass {
	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	private Integer attribute1;

	/**
	 * @generated  "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void Operation1() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/**
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void Operation2() {
		// begin-user-code
		// TODO Auto-generated method stub
		int j = 2;
		System.out.println(j);
		// end-user-code
	}
}

Protecting new code

The transformation does not overwrite Java elements that you add to the generated code, unless you accidentally add the @generated tag to these elements.
Note: To add Java elements to the source UML model, run the reverse transformation.

Although the @generated tag and user-defined sections offer flexibility, to avoid possible problems, you should make structural changes to the source UML model instead of the generated code. You can also run the reverse transformation to add new methods or attributes to the source UML model.


Feedback