Creo Elements/Direct Model Manager Customization Guide > Java Extension > Java API > Migrating Customization Code to Java Generics
Migrating Customization Code to Java Generics
The Creo Elements/Direct Model Manager 20.6 implementation makes heavy use of Java Generics introduced with Java 5. Java Generics provide more type safety and make the API easier to understand. Customization code that does not use generics is still compile-time and runtime compatible with the new typed interfaces. However, we strongly recommend to migrate Java customizations to use Java Generics as well.
In a few cases it was also necessary to change the signatures of some methods.
This page provides guidelines and tips for the migration to Java Generics.
Step 1 – Preparations before installing Creo Elements/Direct Model Manager 20.6:
This step is important to detect whenever a method's signature was changed in a super class. To do so you must add missing @Override annotations to customization code. You can add missing @Override annotations to customization code either manually using the NetBeans IDE or automatically using the Eclipse IDE.
1. Using NetBeans IDE,
a. Click Tools > Options > Editor.
b. Click Hints.
c. Click Standard Javac warnings > Overrides and set Show As to Error.
d. Click OK.
e. Click Window > Task List.
f. Missing @Override annotations are listed as errors in the task list.
g. Add all missing @Override annotations.
2. Using Eclipse IDE,
a. Setup project for source code.
b. Click Java Browsing view.
c. In the project's popup menu click Source > Clean Up (in my-src popup menu click ...).
d. Select Use custom profile.
e. Click Configure.
f. Disable all cleanup options on all tabs.
g. Click Missing Code.
h. Enable Add missing Annotations.
i. Enable '@Override'.
j. Click OK.
k. In the Clean Up dialog, click Finish.
l. Now all missing @Override are added.
Step 2:
Compile your customization code against Creo Elements/Direct Model Manager 20.6.
If you receive any error of the type this method does not override a method of the base class, check if the signature of the super classes method has changed and adopt your code accordingly.
Step 3:
Change the customization code to use generics instead of raw types. For example, use List<WMUserObject> instead of the raw type List.
Tip 1:
The return type for WMElement.getChildren() etc. is now List<WMUserObject>. When iterating over the children avoid to cast WMUserObject without checking the type (instanceof). As an alternative you can use the newly added convenience method WMObject.getFilteredList(...) to get the child objects of the type of interest.
For example:
final List<Part> partList = WMObject.getFilteredList(wmElement.getChildren(), Part.class);
for(final Part part : partList) {
...
}
Tip 2:
The signature for WMSession.close(..) has changed to accept only collections of WMClosable. Make sure that this method is never called with raw types.
Tip 3:
The type for revisions has been changed for consistency reasons to String. For example, signatures of Versionable.setMinorRef(...) have been changed accordingly.
Tip 4:
Do not rely on the fact that WMElement.getChildren() always returns the internally held list of child objects, when modifying the list. This method may return a copy of its internal list. If you have to modify the internal children, call getChildren(), modify the list and then set the modified list calling setChildren(...).
Tip 5:
Avoid unreliable casts. For example, instead of
final List list = versionable.getVersionables();
for(Object obj : list) {
WMElement e = (WMElement) obj;
...
}
consider using the utility method WMObject.getFilteredList(...) to get a type safe list:
final List<WMElement> list = WMObject(versionable.getVersionables(), WMElement.class);
for(final WMElement wmElement : list) {
...
}
Tip 6:
The methods addSection() and getMenuItem() in ActionMenuModel now return a List. Before Creo Elements/Direct Model Manager 17.0 these methods used to return a list consisting of JMenuItem and WMAbstractAction objects. In Creo Elements/Direct Model Manager 17.0, the WMAbstractAction objects are wrapped with a WMActionMenuItem.
For example:
menuItems.add(1, new WMActionMenuItem(new MasterDataPlotAction()));
Tip 7:
The return types for getTree(), getTable(), and getRowHeaderTable() in WMClientNavigationPanel have been changed.
Tip 8:
Clone methods (For Example, WMDefaultMenu) now return the corresponding object type instead of Object.
For example:
@Override
protected WMDefaultMenu clone( ) {
...
}
Tip 9:
com.osm.biz.UserComparator has been renamed to com.osm.biz.WMUserObjectComparator.
Tip 10:
For methods that only process the elements of a list passed in as an argument, you may want to consider using generics with wild cards to get more flexibility.
For example:
public void processList(List<? extends WMElement> list) {
...
}
instead of
public void processList(List<WMElement> list) {
...
}
The first signature allows you to pass in also (for example, List<Drawing>, List<Model>), the second one requires a List<WMelement>.
Was this helpful?