Generating UML From the NetBeans IDE
By dananourie on Jun 18, 2007
Imagine having been given the source code for a project that needs to be maintained, and the maintainer is long gone. You have the task of adding a new feature. Take a look at what you can discover by using a tool such as the Unified Modeling Language (UML) Modeling module of the NetBeans IDE 5.5 to explore the source code and generate UML diagrams of the system.
The abook.zip file , which includes a project designed by Sun Microsystems. It is a basic address-book management system. Using the NetBeans IDE and the UML Modeling module, you'll discover the objects in this application.
First, you need to install the NetBeans IDE, if you don't have it already.
Launch the NetBeans IDE. By default, the UML Modeling Module is not part of the installation, so you must add that after installing and launching the IDE. Go to the Update Center's Tools menu, then click on Update Center to launch the wizard:
Make sure you have selected the NetBeans Update Center option. When you select Next, the wizard connects to each Update Center for potential modules to add, such as the UML Modeling one. Although you can also install the UML Bank App Sample module, it is the UML Modeling module under Features that you need to install for this article.
Although the UML Modeling module is only 10 KB, pressing Add with UML Modeling selected will add not only the selected module but all of its dependencies, totaling over 18 MB in this case.
Press the Next button at the bottom to add all the necessary modules and get the UML Modeling module, after you accept a few licensing agreements.
After the 17 modules, including UML Modeling, are installed, press the Next button to see the list of certificates you must view before installation, and then press Finish. This will prompt you to restart the IDE to install the modules.
You'll see the IDE exit and restart while it installs everything. Once it has restarted, you can now start analyzing the provided source.
To open this project, select the File menu, then the Open Project menu item. You'll need to locate the top-level directory for where you unzipped the ZIP file. For instance, if you unzipped everything to C:\\work, select the ABook entry in the C:\\work directory, then press the Open Project Folder button. Loading causes a complaint about the derby JAR file not being found. Just click past the problem for now. You're not going to run anything just yet.
Now that you have a project, it is time to reverse engineer it and generate some UML. To do that, you will create a second project. Select File, then New Project. Select UML from the Categories on the left, then select Reverse Engineer a Java Project from the Projects on the right.
Pressing the Next button brings up the new Reverse Engineer a Java Project window. Make sure ABook is the Java Project and press the Finish button. Change the default location if you wish the new project to be nearer the source code.
At this point, the source from the ABook project has been imported, but no diagrams have been generated. You have to manually select the elements you wish to generate diagrams for. Looking quickly through the class names, Contact looks like the primary data-related class. Right-clicking on the class in the Projects window will allow you to select Create Diagram From Selected Elements.
You then pick the type of diagram to generate, Class Diagram, and press the Finish button.
You'll then have the generated diagram in a new window on your desktop. Feel free to increase the percentage to see the diagram better. It is a little small because class has many properties and operations. You can copy the diagram to the clipboard to paste it into OpenOffice or just view it to learn about the class design.
Without generating any diagrams, you can see the attributes, operations, and relationships of a class. For instance, if you open the Relationships of the ContactEditor, you'll see it has several Aggregation associations. You'll also notice it is a generalization of a JFrame, and an implementation of an ActionListener and ListSelectionListener. The generalization reveals the parent class. The implementations reveal the implemented interfaces.
To get into some more interesting diagrams, right-click on ContactList and Generate Dependency Diagram to show what that class depends on. The diagram is small because it depends on quite a few other things. Increase the percentage to see things a little better. You'll find ContactList on the bottom, with dependencies on ContactDataModel, ContactEditor, and several other classes. The Dependency Diagram includes the Class Diagram for each class involved.
Generating a sequence diagram is easy from the dependency diagram. Just right-click on one of the Operations and select Reverse Engineer Operation.
This brings up the Create New Diagram window with a different set of available diagrams to generate. From here, you can get a Sequence or Collaboration Diagram. Select Sequence Diagram and press the Finish button.
For the newContactActionPerformed operation, you then get the following diagram.
That's the whole process of reverse engineering your Java source code. Try out the module to generate some of the other supported UML diagrams. The process is basically the same, just select a different diagram type from the Create New Diagram window.