Create an inheritance tree of classes by using Software Analyzer
In object-oriented programming languages, inheritance shows the common fields and methods among inherited classes within a family and displays the specific fields and methods for each class. Developers and architects can use inheritance trees of the classes in a Java™ project to understand the relationships among the classes in the project. Suppose that you have created or loaded a Java project in IBM® Rational® Software Architect 7.5. To create an inheritance tree in the project:
- Open the Software Analyzer in Rational Software Architect V7.5 by selecting Run > Analysis (see Figure 1). You can use the Software Analyzer to generate reports that are based on rules that you select. Rational Software Architect V7.5 includes rules in the domains of Java Architectural Discovery, Code Review, UML Model Metrics, and UML Model. Creating an inheritance tree is a rule in the Java Architectural Discovery domain.
Figure 1. Open the Software Analyzer
- Create a configuration:
- In the Name field, type a name.
- In the Analysis Domains and Rules area, expand the Object-oriented Patterns category and select the Inheritance Tree check box (see Figure 2).
Figure 2. Create an inheritance tree
Click here for a larger view of Figure 2.
- Click Analyze. An inheritance tree is displayed (see Figure 3). This inheritance tree shows that a Vehicle class has three children:
Carclass has two children:
SportsCar. There is no information about class fields and methods in the inheritance tree. To explore the fields and methods of the classes, use a browse diagram.
Figure 3. The inheritance tree is generated by Software Analyzer
Click here for a larger view of Figure 3.
Explore the details of the inheritance tree by using a browse diagram
The inheritance tree that you created with the Software Analyzer does not contain details about the fields and methods of the classes. To find out about the details of each class within a certain scope, use a browse diagram. The scope can be a Java package, a project, a work set, or a workspace. For example, use the
FamilyCar class as a starting point to explore its details with a browse diagram:
- To open a browse diagram, right-click on the FamilyCar.java class name and then select Visualize > Explore in Browse Diagram (Figure 4).
Figure 4. Explore the FamilyCar class in the browse diagram
- In the Browse Diagram window, on the relationship toolbar, click the Extends button (see Figure 5). The relationship toolbar contains five types of relationships:
- Extends: Explores the classes from which
- Dependency: Explores the classes that are used in the methods of the
- Realization: Explores the interface that is implemented by
- Association: Explores the classes that are related to
- Declares: Explores declared packages in the
- Degree of separation: Explores the level of extension that the relationship applies
- Extends: Explores the classes from which
Figure 5. A browse diagram of the FamilyCar class with one degree of separation
- Next to the relationship toolbar is a degree of separation, which indicates the number of relationship levels from the
FamilyCarclass that are shown in the diagram. In Figure 5, the value of 1 (one) indicates that the diagram shows the
FamilyCarclass only. When you apply the value of 2, the diagram shows the
Vehicleclasses (see Figure 6).
Figure 6. The FamilyCar class in a browse diagram with 2 degrees of separation
The browse diagram of the inheritance tree is a temporary diagram that you cannot save. Typically, you use browse diagrams to explore a given class or to document the design of a project.
Generate an inheritance tree by using a topic diagram
The previous example showed how to use a browse diagram to visualize the inheritance tree of a class. In certain circumstances, you might want to create a diagram which consistently represents all of the inherited classes of the
FamilyCar class during the development of the program. In this case, you can use a topic diagram to represent the inheritance tree of a given class. Unlike a browse diagram, you can save a topic diagram in a model file for a reuse.
To create an inheritance tree of a class in a topic diagram:
- Open a topic diagram of a given class, such as
FamilyCar, by right-clicking the FamilyCar.java class and then selecting Visualize > Add to New Diagram File > Topic Diagram (see Figure 7).
Figure 7. Open the FamilyCar class in a topic diagram
- Generate a topic diagram:
- Select a topic, such as Inherited Java Classes (N levels)
- Specify the related elements to be shown. In this example, the Extend relation is selected with indefinite expansion on both incoming and outgoing directions to represent the whole inheritance tree.
- Click Finish. The result is shown on the right of Figure 8.
To respond to changes in Java source code, update the diagram by right-clicking the topic diagram workspace and clicking Refresh.
Figure 8. Create an inheritance tree by using a topic diagram
Click here for a larger view of Figure 8.
Integrate a generated class diagram into a model file by using Harvest
In Rational Software Architect, you can transform a model file into Java code. You might create a model file by creating a new class with some attributes and methods; you might also want to integrate existing Java classes from a project into the model file. To use the Harvest function in Rational Software Architect to copy a generated class diagram into your model file:
- Create a blank model file by clicking File > New item. A new wizard window opens (see Figure 9).
Figure 9. Open the Model wizard
- Click Next and follow the workflow in Figure 10, which shows a freedom diagram.
Figure 10: Create a model file
Click here for a larger view of Figure 10.
- Copy the class diagrams into the new model file by using the Harvest function:
- Select all of the classes in the diagram (see Figure 11).
- Right-click the diagram, and then click Harvest.
Figure 11. Harvest the selected classes in the browse diagram
- Paste the classes into the new model file (Figure 11 shows the result).
In Figure 11, neither the Vehicle nor Car classes show fields and methods information. To make the fields and methods visible, select one or more classes in the diagram, right-click the class or classes, and click Filters > Show/Hide Compartment > All Compartments (see Figure 12). To view the final results of enabling the compartments, see Figure 13.
Figure 12. Show all of the compartments of the Vehicle class
Click here for a larger view of Figure 12.
Figure 13. The compartments of the Vehicle class
To analyze and expand Java projects, you can use the tools in Rational Software Architect, Version 7.5 for these actions:
- To generate an inheritance tree for all of the classes in a project, use the Software Analyzer.
- To visualize the details of a class that is part of an inheritance tree, use a browse diagram.
- To use a fixed query to monitor code changes, use a topic diagram.
- To paste a class from an inheritance tree into a model file, use the Harvest function.
- For information about reverse engineering in earlier versions of Rational Software Architect, see Reverse engineering UML class and sequence diagrams from Java code with IBM Rational Software Architect by Fenglian Xu and Alex Wood (IBM developerWorks, June 2008).
- Learn about other applications in the IBM Rational Software Delivery Platform, including collaboration tools for parallel development and geographically dispersed teams, plus specialized software for architecture management, asset management, change and release management, integrated requirements management, process and portfolio management, and quality management. You can find product manuals, installation guides, and other documentation in the IBM Rational Online Documentation Center.
- Visit the Rational software area on developerWorks for technical resources and best practices for Rational Software Delivery Platform products.
- Explore Rational computer-based, Web-based, and instructor-led online courses. Hone your skills and learn more about Rational tools with these courses, which range from introductory to advanced. The courses on this catalog are available for purchase through computer-based training or Web-based training. Additionally, some "Getting Started" courses are available free of charge.
- Subscribe to the Rational Edge newsletter for articles on the concepts behind effective software development.
- Subscribe to the IBM developerWorks newsletter, a weekly update on the best of developerWorks tutorials, articles, downloads, community activities, webcasts and events.
- Browse the technology bookstore for books on these and other technical topics.
- For Rational Software Architect documentation, see the Rational Software Architect information center.
Get products and technologies
- Download trial versions of IBM Rational software.
- Download these IBM product evaluation versions and get your hands on application development tools and middleware products from DB2®, Lotus®, Tivoli®, and WebSphere®.
- Try a 30-day trial of Rational Software Architect.
- Check out developerWorks blogs and get involved in the developerWorks community.
- To discuss Rational Software Architect with other users, check out the Development Tools forum.
Dig deeper into Rational software on developerWorks
Experiment with new directions in software development.
Read and subscribe for the best and latest technical info to help you deal with your development challenges.
Software development in the cloud. Register today and get free private projects through 2014.
Evaluate IBM software and solutions, and transform challenges into opportunities.