Overriding annotation merge behavior during UML-to-Java or related transformations

Technote (FAQ)


Question

The default merge behavior for annotations during UML-to-Java or related transformations, such as UML-to-EJB 3.0, UML-to-JPA, etc, is to overwrite modelable annotations and retain unmodelable and user added annotations in the target. If an end user wants to customize this behavior, there should be a facility to make this customization easier.

Answer

In version 8.5.1, it is possible to override the behavior of annotation generation during UML-to-Java or related transformations, by writing a simple UML-to-Java transformation extension.

The extension entails extending the Java transformation and adding a custom rule to the root transformation. Care should be taken to insert the rule before the GenerateRule rule executes.

The following entry in the plugin.xml of the extension should suffice (the below example caters to JPA transformation) :

<TransformationExtension
author="IBM"
name="Test java extension"
description="Test java extension description"
id="com.ibm.xtools.transform.uml2.javatest.extension"
enabled="false"
targetTransformation="com.ibm.xtools.transform.uml2.ejb3.java.jpa.internal.UML2JPATransform"
version="1.0.0">

<RuleDefinition
name="TestAnnotationMergeOverrideRule"
class="com.ibm.xtools.transform.uml.testjava.extension.TestOverrideRule"
id="com.ibm.xtools.transform.uml.wsdl.testjavaextension.testannotationmergerule">
</RuleDefinition>

<ExtendTransform targetTransform="com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform">
<AddRule id="com.ibm.xtools.transform.uml.wsdl.testjavaextension.testannotationmergerule" insertAtID="com.ibm.xtools.transform.uml2.java5.internal.GenerateRule" />
</ExtendTransform>

</TransformationExtension>

Following this, the desired annotation merge behavior can be achieved by customizing the custom rule body.

For instance, to ensure that a certain annotation is never deleted during a UML-to-Java transformation, the following code can be added :


@Override
protected Object createTarget(ITransformContext context) throws Exception { // get source
AnnotationMergerInfoCache mergeCache = ContextPropertyUtil.getAnnotationMergeInfoMap(context);
// With the below change, the Table annotations are never deleted from the code by the transformation
mergeCache.removeMergerInfo("Table");
return context.getTarget();
}

To apply this behavior for multiple annotations, each of the annotation names can be removed from the merge cache similarly.

The above snippets can be found in the attached example which is a running UML-to-Java transformation extension. The attached plug-in project can be exported as a plug-in and installed on a product installation with version 8.5.1, or later.

The Eclipse plug-in installation instructions depends on the operation system. Refer to the Eclipse site or forums for detailed installation instructions for Eclipse plug-ins for the concerned operating system.


Cross reference information
Segment Product Component Platform Version Edition
Software Development Rational Software Architect for WebSphere Software Transformation Linux, Windows 8.5.1
Software Development Rational Software Architect RealTime Edition Transformation Linux, Solaris, Windows 8.5.1

Rate this page:

(0 users)Average rating

Add comments

Document information


More support for:

Rational Software Architect
Transformation

Software version:

8.5.1

Operating system(s):

Linux, Windows

Reference #:

1619212

Modified date:

2012-12-11

Translate my page

Machine Translation

Content navigation