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.
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 > Execute Transform > UML2 to Java.
4. The Run Transform 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 Transform 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.
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 |
Interface | Java interface with the same name* and visibility |
(Interface) Generalization | Java interface "extends" the specified interface |
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 | Generated Java Type |
True | True | java.util.SortedSet |
True | False | java.util.List |
False | True | java.util.Set |
False | False | java.util.Collection |
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 > Transforms > Configure Transforms.
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.