UML to Java transformation


The UML to Java transformation transforms UML 2.0 specifications to Java classes. You can use the transformation to seed a Java project from UML 2.0 specifications. You can also apply the transformation after the initial seeding to merge updated UML models with existing Java code.
 

1. Getting started

Before you use the UML to Java transformation, you should be familiar with creating and editing UML 2.0 models and Java projects.

The following procedure shows the simplest use of the Java transformation.

To transform a UML model to Java:
1. Switch to the Modeling perspective.
2. In the Model Explorer view, right-click a UML class (for example, Class1).
3. Click Transform > Run Transformation > UML2 to Java.
4. The Run Transformation dialog opens. Click the Create Target Container button.
5. The New Java Project dialog opens. Enter simple in the Project Name field and click Finish.
6. In the Run Transformation dialog, click Finish.

The new Java project contains a default package, which contains the new Java class: Class1.

The transformation can also perform several other, more complex functions, such as overriding names without modifying the source model, replacing the UML object with a visualization, merging changes in the UML model and Java source, and validating the UML model for potential transformation problems.
 

2. Transformation details

2.1 Transformation source

You can select UML 2.0 models, packages, non-nested classes, non-nested interfaces and enumerations as the source for the transformation. The transformation proceeds if you select other items as the source, but it does not transform those items. When the transformation transforms a model, package, class, or interface, it also transforms all the items that those objects contain. For example, when the transformation transforms a class, it also transforms all the operations and properties of that class.
 

2.2 Transformation target

You can select any Java project that has at least one source folder as a target of the transformation.
 

2.3 Interpretation of UML Source Objects

UML Java 
Package Java package with the same name*
Package stereotyped <<perspective>> Ignored
Package with keyword <<Analysis>> or <<analysis>> Ignored
Class Java class with the same name* and visibility
(Class) isLeaf  property Java class is "final" if true
(Class) isAbstract property Java class is "abstract" if true
(Class) Generalization Java class "extends" the specified superclass
Implementation Java class "implements" the specfied interface
(Class to Interface) Realization
Java class "implements" the specfied interface
Interface Java interface with the same name* and visibility 
(Interface) Generalization Java interface "extends" the specified interface
Enumeration
Java interface with the same name* and visibility
EnumerationLiteral
Java field with the same name* and visibility
Operation Java method with the same name* and visibility
(Operation) isStatic property Java method is "static" if true
(Operation) isAbstract property Java method is "abstract" if true
(Operation) isLeaf property Java method is "final" if true
(Operation) with same name as its class Java constructor
(Operation) stereotyped <<create>> Java constructor
Parameter Java parameter with the same name*
(Parameter) Type property Java parameter has the specified type, which can be another class or a primitive type
(Parameter) Direction property Java method has "return <param type>" if set to "return"; otherwise, adds "<param type> <param name>" to the method signature
(Parameter) Multiplicity property  See the following table that contains information about multiplicity
Property  Java field with the same name* and visibility
(Property) isStatic property Java field is "static" if true
(Property) isLeaf property Java field is "final" if true
(Property) Type property Java field has the specified type, which can be another class or a primitive type
(Property) Multiplicity property  See the following table that contains information about multiplicity

*  Some characters are not valid in Java identifiers (such as a space). The Java transformation automatically replaces invalid characters with an underscore (_) to create a valid identifier. This replacement can cause name conflicts that prevent the transformation from generating duplicate classes and interfaces. You can use the validation feature to detect these problems. To avoid these replacement issues, rename the elements or use the mapping feature to specify an alternate name that does not modify the source model.

Java types for properties and parameters
UML multiplicity Generated Java type
0..1 Attribute, pointer, or reference (for example, String)
1 Attribute (for example, String)
N (N > 1)  Array (for example, String[])
1..*, *, or x..y  Collection, see the following table

Java collections for 1..*, * or x..y multiplicities

isOrdered Property isUnique Property UML Collection
Generated Java Type
True True Ordered Set
java.util.SortedSet
True False Sequence
java.util.List
False True Set
java.util.Set
False False Bag
java.util.Collection

Note that the generated types listed above are defaults.  They may be overridden in the Advanced tab of a transformation configuration.

Advanced
The Advanced tab provides some code generation options.  Click Modeling > Transform > Configure Transformations, select a UML to Java configuration, and select the Advanced tab.  The tab contains the Generate getters and setters checkbox, and the Collections group. 

When Generate getters and setters is checked, the transformation will generate a private field with getter and setter methods for each UML property.  If the property's isLeaf (final) or isReadOnly are set to true, no setter is generated.  If the property's isDerived is true, the setter implementation will be empty. 

You can specify the collections generated by the UML to Java transformation in the Collections group.  For example, select "java.util.Vector" from the Sequence drop down, and the transformation will generate vectors instead of lists.  You may also specify custom collection classes by typing the class name in the drop down.  If you type "custom.Bag" in the Bag drop down, the transformation will generate bags instead of collections. 

3. Support for common transformation technologies

3.1 Integration with Team Support

The Java transformation provides integration with Team Support allowing automatic check-outs and addition of new files into source control systems.
 

3.2 Transformation mapping

You can specify alternate names for the transformation's target elements if you do not want to modify a source model for a transformation. You might also want to specify alternate names if you want to include Java-specific details (such as naming restrictions) in platform-independent models, or if the logical organization of the source model is not appropriate as target packaging. To support alternate target names without modifying the source model, use the transformation mapping feature.
To use the transformation mapping feature:
1. Switch to the Modeling perspective.
2. In the Model Explorer view, click a UML model.
3. On the menu bar, click Modeling > Transform > Configure Transformations.
4. Create a new UML2 to Java Transform (for example, name the transformation Create mapping.)
5. On the Mapping page, click Only create mapping model (no transformation).
6. Specify a file name and click Run.

When you click Create Mapping Model, the Java transformation creates a mapping model, which is  a separate model that has an artifact for each transformable element. The artifact refers to and has the same name as the original transformable element. To specify an alternate name for the original element, type a new name in the file name property of the artifact. If you do not change the artifact's file name property, the transformation generates the element with the artifact's default name. If you specify alternate names for packages, the alternate names affect all classifiers in that package (unless the classifier specifies a fully qualified name). The following table lists examples of alternate names.
 
UML source Mapping artifact file name Generated Java
"Package1" ""  "Package1"
"Package2" "com.ibm.test" "com.ibm.test"
"Class1" in Package1  "" Package1.Class1
"Interface1" in Package2 "ITestable" com.ibm.test.ITestable
"Class2" in Package2 "com.ibm.test.Testable" com.ibm.test.Testable

To use a mapping model, complete the following steps:
1. Switch to the Modeling perspective.
2. In the Model Explorer view, click a UML model.
3. On the menu bar, click Modeling > Transform > Configure Transformations.
4. Create a new UML2 to Java Transform (for example, name the transformation Use mapping.)
5. On the Mapping page, click Use Mapping Model.
6. Click the mapping model that you created earlier and click Run.
When the transformation runs, it uses any alternate names that you specified in the mapping model.
 

3.3 In-Place visualization support

If you do not want to keep the original UML model, you can use the visualization features of the transformation to delete the UML elements and replace them with shortcuts to generated Java elements. To configure the transformation to delete the UML model elements, in the Configure Transformations window, on the Common page, click Replace UML Elements. The transformation does not replace elements if the replacement causes a loss of data. For example, if UML elements contain diagrams, the transformation deletes the diagrams, so it does not replace those elements.
 

3.4 Re-Apply

The Java transformation merges changes to the source model and modified source code by using "@generated" tags in generated javadocs. The transformation owns Java elements that are marked with "@generated" and overwrites them during a retransformation or deletes them if they were removed from the source model. You can reclaim "ownership" of the elements, by removing the "@generated" tag, which prevents the transformation from overwriting or deleting the elements.
 

3.5 Validation

The Java transformation plug-in provides a UML profile that you can use to validate models for potential transformation problems. For example, multiple inheritance, circular generalizations, and naming conflicts cause compilation errors in generated Java code. To see a list of potential problems that can occur in the generated code, apply the profile to the source model and run the validation feature.
 

3.6 Traceability

The Java transformation can create derived relationships from generated Java elements to their UML source. To create derived relationships, in the Configure Transformations window, on the Common page, click Create source to target relationships. When the system visualizes the generated Java element, the element has a derived relationship to its source element.
 
Terms of use | Feedback
(C) Copyright IBM Corporation 2004. All Rights Reserved.