Professional Documents
Culture Documents
Learn to use the NetBeans UML Modeling Project by reverse engineering a complete Java application
Fernando Lozano
ing full UML support in NetBeans IDE. This article showcases NetBeans UML support, going through a case study where we reverse engineer a working Java application, highlighting the IDEs modeling features in the process.
he UML Modeling Project (uml.netbeans.org), which started as a part of the Enterprise Pack is now a separate download provid-
and MDA tools like AndroMDA (andromda.org) have been quite sucthe NetBeans UML modules provide many features absent from other open-source CASE tools, and even come with an extensible Design Pattern Catalog, including not only classical GoF patterns but also many Java EE Blueprints out-of-the-box.
A1
A2
uml.netbeans.org
cessful in the Java arena. MDA advocates will be happy to find that
A
Figure 1. Selecting the Update Center for modules still in beta
A
Figure 2. Selecting the UML Modeling feature
Second Edition N 41
Java code generation Java packages, classes and interfaces can be generated directly from the UML model; Round-trip engineering if a UML project is linked to a Java project, changes in code are automatically reflected in the model; Ability to nest diagrams and packages inside other model elements; A Design Center, which provides an extensible design pattern catalog; Rich-text documentation for model elements created directly inside the IDE; Operations and attributes can be shown in class elements using either standard UML notation or Java syntax; Generation of model reports in JavaDoc format.
Official OMG site dedicated to UML, where you can download UML specifications and find about other UML CASE tools and tutorials
UML Modeling
uml.org
downloading. When the downloads are done, click Finish to install the modules (see Figure 4). (If you are not the only user of your development workstation, be sure to check the Global option for all modules.) Finally, after restarting NetBeans youll find new UML project templates and configuration options in the IDE.
A
Figure 3. Modules to install for the UML Modeling feature
A3
A
Figure 4. Click Finish to install all UML modeling modules
A4
The application used for the case study was created for NetBeans Magazine Issue One. Its a simple Task Manager, similar to those provided in a PIM suite. The Todo application has two windows: a task list (Figure 5) and a task editing form (Figure 6). The task list window displays tasks ordered either by priority or by due date, and it can filter out completed tasks. Tasks are colored to indicate completed, late or alert status. The complete application is available as a download in the NetBeans Magazines website.
42 N NetBeans Magazine
Our application is quite simple, so reverse engineering it wont be a complex task. Even so, it is complex enough for us to demonstrate many of NetBeans UML features. Well work through the application as if we didnt know how it was designed, using UML diagrams to understand its structure and design ideas.
the sources and populate the project with model elements for Java packages, classes and interfaces. (Note that for large projects this may take a long time and eat up a large amount of memory.)
ress for each source file. When the scanning finishes, click Done to dismiss this window, and youll end up with a new UML project as shown in Figure 9.
A5
A6
New Project icon and choose the UML category. Then select Java-Platform Model by
A
Figure 5. The task list window
A
Figure 6. The task form window
Second Edition N 43
A
UML Modeling
Figure 7. UML project templates
A7
with conceptual modeling and then move on to physical modeling, and keep track of which physical elements realize which conceptual elements1.
A
Figure 8. Creating a UML Project for reverseengineering a Java Project
A8
A
Figure 9. The new UML project
A9
Diagrams, Notes, Data Types, Associations and anything else that can be defined as part of the UML project itself. The Diagrams container provides quick access to all diagrams created
as part of the model. You may be surprised to see there are no diagrams in our reverse-engineering project. Thats because a UML model consists of model elements, which may or may not be shown as diagrams. Every UML diagram is a partial view of a model, and youll need to choose which details to show and which to leave
F1 A realize UML relationship between two model elements states that one element represents an abstract concept in the problem domain, while the related element represents a software construct implementing that concept, making it physical in the softwares point of view.
out. So NetBeans populates the reverse-engineered model with elements but leaves the diagram creation to the developer. Finally, the Imported Elements container allows a model to reference elements created as part of another model (i.e. another UML Project). This is useful for organizing large models as a set of UML projects; for example one for business logic and another for GUI elements. Its also useful for reusing model elements in various projects. And it allows for a Platform-independent UML Model thats referenced by a Java Platform UML Model. This way, you can start
44 N NetBeans Magazine
A 10
partment or expand it to select specific fields to hide. After hiding an elements details, you can right-click Resize Element to Contents to optimally resize it (Figure 14). (You can also hide/show a compartment by double-clicking the compartment separators.) After these changes, the diagram will probably need some rearrangement. NetBeans can do this using four different auto-layouts, accessible through the last four buttons in the Diagram Editor toolbar (see Figure 15). Click the first button, for Hierarchical Layout. This is the default layout NetBeans uses when generating a diagram from model elements. The results are shown in Figure 16.
A
Figure 10. The new UML project, showing a few model elements
A 11
A
Figure 11. Creating a class diagram from model elements
Second Edition N 45
A
UML Modeling
Figure 12. The NetBeansgenerated model for all elements inside the todo. model namespace.
A 12
and only attributes (all private) for the Task class. Thats because I assume Task is a DTO (Data Transfer Obect) or Value Object, and showing accessor methods wouldnt help in understanding the class. But for
A
Figure 13. Hiding details from classes
A 13
A
Figure 14. Optimally resizing a class
A 14
46 N NetBeans Magazine
Now, if our conclusions are OK, the model generated by NetBeans is not entirely correct. There is no part/whole relationship between TaskManager and Parameters, so they should not be connected by an aggregation. This should be changed to a simple association. Right-click on the aggregation (it should turn blue) and select Transform>Remove Aggregate from the context menu (see Figure 19). I fact, you should expect NetBeans (or any other UML CASE tool) to generate a few incorrect associations when reverse engineering Java sources. Thats because UML associations have much richer semantics than Java references. All structural associations between Java classes originate from attributes referencing other classes/interfaces (or collections and arrays of these). Similarly to most other programming languages, Java does not distinguish between composites, aggregates and simple associations. Also, a reference to an instance of another class could be there just for convenience, and not be a structural association at all. Now, if TaskManager is a DAO class, responsible for persisting instances of
A 15
A
Figure 15. Auto-layout diagram buttons from the NetBeans diagram editor toolbar
A 16
A
Figure 16. Class diagram for the todo. model package, after hiding details, deleting elements and applying an auto-layout
A 17
A
Figure 17. By default, deleting an element from a diagram doesnt remove it from the model
Second Edition N 47
get similar results with other classes from the same package, so its
is the controller for some secondary usage scenarios, so we should examine ListEdit-
UML Modeling
safe to assume the class diagram built for the package has enough information. The dependency diagram for TaskManager is an example of a disposable diagram. Theres no value in keeping it around after using it to verify that other related diagrams have enough information. You should delete the diagram from the model, and also the dependency diagrams for other classes in the same package (if you generated them). The key is not to pollute your model with useless diagrams.
A
Figure 18. Adding a stereotype to a class
referenced inside a method body. Only attribute and method declarations are followed, so there will be cases when youll need to inspect the code to find additional dependencies and add them manually.
A 18
A
Figure 19. Transforming an aggregation into a simple association
A 19
A
Figure 20. Class diagram model for the todo.model package after changing associations between elements. Note the dependency from TaskManager to Task, and that the association from TaskManager to Parameter is no longer an aggregation. Note also the stereotypes for TaskManager and Task.
and TaskForm. It looks like ListEditTasks reacts to those events by calling TaskManager methods, acting as a workflow controller for the application. Note also that TaskList and TaskForm are the two windows in the application. The fact that both are connected to ListEditTasks strongly suggests its the main controller class for the application. The dependency diagram for
A 20
48 N NetBeans Magazine
TaskForm)
to
ListEditTasks.
A 21
A
Figure 21. Dependency Diagram for TaskManager
A 22
A
Figure 22. Dependency Diagram for ListEditTasks
Second Edition N 49
A
UML Modeling
Figure 23. Class diagram for the Todo application user interface
A 23
of objects during the execution of an operation. Note that it refers to class instances and not to the classes themselves. A solid arrow represents a message, that is, a Java method call (in UML terms, an operation call). A dashed arrow is either a message return or a create message, i.e. the creation of a new object. Again, the automatically generated diagram comes with too much detail. Removing the args, length, err and System objects, and moving the elements around should solve this (see the result in Figure 25). The diagram shows that main() just constructs instances of the classes we have already identified as being the main ones for the application, and that all are connected by the ListEditTasks constructor. So
50 N NetBeans Magazine
far weve gleaned the same information we had expressed in the class diagrams. But its important to confirm our guesses. The fact the main() operation creates no instances of other classes, and that it doesnt call operations other than constructors and setVisible() for the TaskList window, makes us confident of not missing any important class.
A 24
A
Figure 24. Sequence diagram generated by NetBeans for the main() operation
Second Edition N 51
UML Modeling
it. So they dont need to know which Swing component (from the window classes) generated which event. This isolates the controller classes from the internal structure of the view (window) classes.
more clearly to other developers. If you are serious about reverse engineering applications, youll inevitably have to create some diagrams on your own. A cross-level sequence diagram is useful in understanding complex interactions among objects like event handling for the Todo application, which involves TaskList,
A hand-made diagram
We cant fully automate reverse engineering, but we can document the knowledge gleaned from the reverse-engineering process in the form of UML diagrams, and communicate it
A
Figure 25. Sequence diagram for the main() operation with excess detail removed
A 25
A
Figure 26. Sequence diagram for the event handling between view and controller classes
A 26
52 N NetBeans Magazine
Diagram, and name it ActionEventSequence. From the Projects window, drag the elements JButton (javax.
swing), ActionEvent (java.awt.event), ActionSupport (todo.view), ListEditTasks (todo. control), TaskForm (todo.view), Task (todo. model) and TaskManager (todo.model).
Connect these from left to right, using either the Syncronous Message or the Create Message from the Sequence Diagram Palette. Then, for each message, right-click it and select Operations from the context menu to see all operations supported by the messages target. The sequence of messages is create, it is representative of all other
Action event is handled by the application, he can figure out how all
other Action events are handled. This ends our case study. We now have a model rich enough so any developer can understand and maintain the Todo application. We could consider drawing other UML diagrams, like Use Case or Deployment, but these would probably add little value in our case. An important part of doing good modeling is knowing when to stop modeling.
C
Fernando Lozano (fernando@lozano.eti. br) is an independent consultant and has worked with information systems since 1991. Hes the Community Leader of the Linux Community at Java.net, webmaster for the Free Software Foundation and counselor to the Linux Professional Institute. Lozano helps many open-source projects and teaches at undergraduate and postgraduate college courses. Hes also a technical writer and book author, as well as Contributing Editor at Java Magazine (Brazil) and freelance writer for other leading IT publications.
actionPerformed(),
actionPerformed(),
Conclusions
In the past, modeling was done using expensive proprietary tools with steep learning curves. This prevented most development teams to effectively use UML models and hindered adoption by most small and medium shops. Modeling was performed mainly by Business Experts and System Analysts, sometimes by a Software Architect. And many of those never came close to real Java source code. Also CASE tool developers were often unaware of the state-of-the-art in enterprise software development in general and the Java platform in particular. All this is changing with the full open-source UML support in NetBeans. Having UML modeling seamlessly integrated in the IDE encourages developers to use models and diagrams, improving both communication between team members and the quality of OO designs. The UML Modeling Project provides developers, from Agile Modeling fans to MDA advocates, with a powerful toolset that enables ubiquitous and effective modeling.
Second Edition N 53