Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate UML Class Diagram and sequence Diagram from Java Code reverse Engineering

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about how to generate UML class diagrams and sequence diagrams from Java code reverse engineering, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Software architects, developers, and testers are familiar with the Unified Modeling language (UML), which is suitable for documenting use cases, class diagrams, sequence diagrams, and other diagrams. You can also use many other software aids to help software engineers do this work, or

Forward engineering or reverse engineering.

Forward engineering is a traditional process of high-level abstract, logical and independent design of the physical structure of a system.

Reverse engineering is the analysis and processing of an existing system to identify its components and their internal relations, so as to build a system framework with high-level abstraction. In most cases, reverse engineering is used to extract lost design files from existing source code in abstract model UML format, so that the static structure and dynamic behavior of a system can be obtained at the same time.

The essence of Class and sequence Diagram problem

IBM ®Rational ®Software Architect is widely adopted in many industries because it provides many features to help reverse engineers. The problem is that when you reverse-build UML classes and sequence diagrams in Java ™code, Rational Software Architect does not automatically generate useful UML classes and sequence diagrams. But there are already technologies to improve the output of Rational Software Architect. This article demonstrates how to use the techniques described here to identify its components from Java code and to make a high-level abstraction of UML types and sequence diagrams.

With reverse engineering, you can easily get what you want from forward engineering. This article describes the problems in the following areas of reverse engineering:

Discover their abstract classes and identify their hierarchical structure

Generate high-level abstract class diagrams with aggregation and association relationships

Build sequence diagram

The next section provides a solution to each problem and demonstrates how to generate meaningful class and sequence diagrams. The example shows you how to identify the inheritance relationships and components of a system from the source code of a given Java project for high-level abstraction of UML class diagrams and sequence diagrams.

Note:

The examples in this article are produced in Rational Software Architect version 7. 0

Identify an inheritance tree of a UML class diagram

Inheritance relationship is a universal object-oriented model. It allows a group of classes to share common state and behavior, so that subclasses can inherit common state and behavior from the parent class. Discovering the entire inheritance tree structure from an existing system is useful because it reveals to you what is a top-level class and what is a subclass in the tree. Also, you can identify what common states and behaviors are in an inheritance tree, and how these common behaviors work. You can use Rational Software Architect in three ways during exploration.

Discover its abstract structure from a workplace or workset

Select a class from the abstract structure table to display its abstract class diagram

Study the tree structure in a browse table and you will find a series of abstract classes displayed under Abstraction.

The first step is to automatically get its top-level class in an existing system. This allows you to use these classes as an entry point to study the classes in the inheritance tree. You can do this by following these steps.

Open the Diagram Navigation view in Rational Software Architect.

Under Object-oriented Pattern, right-click Abstraction, and then click

Discover Architecture (shown in figure 1).

This reveals the architecture of the entire workspace.

Figure 1. Discover the architecture of the entire workspace

Figure 2 shows the remaining steps required to get the tree structure of an abstract class diagram:

Open the content menu by right-clicking the class Car under Abstraction.

Display the Car class diagram in the right panel by selecting the Show Diagram option in the menu.

Open the content menu by selecting and right-clicking the class diagram Car in the right panel.

The tree structure of the abstract class diagram is generated by selecting the Explore in Browse Diagram option in the content menu.

After step 6, you will see the tree structure of the Car class diagram in the right panel.

Figure 2. Discover its abstract structure from the entire workplace

The result has the following defects:

The same class and subclasses of the found class in the structure tree may be lost.

Except for the abstract classes that are found, other classes have no properties and operations.

To get the tree structure generated by step 6, other steps are needed. You need to increase the degree of separation (as shown in figure 3), which determines the layer that extends from the discovered class.

Figure 3. Change the degree of separation

The default degree is 1, which is why some subclasses are lost in the inheritance tree. In this case, the degree of separation is increased to 2.

The second problem is that except for the classes found in the tree structure, other classes have no properties and operations. This is not conducive to users to study the existing universal patterns for the purpose of reuse.

The next example will show you how to identify the entire inheritance tree with arbitrary properties and operations.

Load a Java project into Rational Software Architect.

Switch to the Diagram Navigation view and discover its abstract structure from the workplace as described in the steps above.

Find an abstract class from the second step that interests you.

Find the hierarchical tree class by looking for the class in the model searcher, double-click the class in the editor, and press F4 to open the hierarchical tree. Make sure that the type hierarchy is selected.

Right-click the class and turn it into a visual class diagram by selecting Visualize > Add to New Diagram File > Class Diagram, as shown in figure 4.

Add the remaining classes to the current diagram by right-clicking and selecting Visualize > Add to Current Diagram.

Figure 4. Visualize the class as a new class diagram

Figure 5 shows the process of generating an inheritance tree class diagram:

Open and press F4 to display the level of the class.

Select each class and add it to the class diagram.

Check and complete the picture on the right.

Figure 5. The mechanism of generating an inheritance tree class diagram

The class diagram is generated in the Rational Software Architect default format. There are several useful changes that can help you visualize the chart. For example, you can modify the connection path style to use a tree-style path, and you can right-click in the workspace to open the content menu and click Arrange all. The generated class diagram so far looks better than the automatically generated one. As shown in figure 6.

Figure 6. Inheritance tree view and tree path connection with attributes

The class in figure 6 shows both properties and actions. The advantage of attribute and action display is that you can study their common state and behavior to learn more about how an existing system is implemented, which will help the system reuse.

Generate a high-level UML class diagram

Rational Software Architect allows you to generate class diagrams by selecting multiple Java files from a Java project.

In the model searcher, use Visualize to add them to a new diagram or to the current class diagram.

If multiple classes have been added to the current diagram, the relationship between them will also be displayed.

Figure 7 is an example of a class diagram that is automatically generated from Java code

Figure 7. An automatically generated class diagram

As shown in figure 7, you can select multiple Java files from the model searcher to visualize them in the new class diagram. If you want to add more classes, you can select more Java source code to visualize them in the current class diagram. This figure shows that classes are included in the project and their basic relationships. This facilitates the automatic discovery of UML classes in the project, but the automatic discovery of relationships is of little use here.

In figure 7, almost all relationships are use excluding inheritance relationships. Usage relationships are too common to give useful design information, and more and more special aggregation and composition relationships are hidden even when all relationships appear in the diagram. The aggregation relationship is represented as an one-to-many relationship when a class contains many items of other classes, the composition relationship is used to describe an one-to-one relationship when a class contains only one example of another class. This high-level abstraction means more precise discovery of relationships between classes and provides useful information for the execution of this design. This class diagram will no longer be as useful as it used to be without the details of abstract relationships.

Here, we try and explore a high-level abstraction scheme for generating UML class diagrams in a semi-automatic way. UML classes are discovered with the same technology as before, and the relationships between classes are artificially specified. The high-level abstract scheme is based on the knowledge necessary to study the existing source code.

The example in figure 8 shows how to apply this method to get a high-level UML class diagram.

Figure 8. Create a blank model using the UML Model Wizard

To get a high-level class diagram, you must first build a blank model.

Follow the steps in figure 8 to create a new blank model:

Under File Type, select UML Modeling.

Under the template, select Blank Model.

In the file name area, enter Blank Model.

For the destination folder, enter example.

The default picture check box "Create a default diagram in the new model" should be checked.

For the default graph category, select Freeform. Diagram .

Click Finish.

The next step is to summarize the selected classes from the automatic production class diagram. The summary in Rational Software Architect allows you to copy a class from one class and paste it into another class diagram, which must be done in a blank model. If you paste the summarized class into the same class diagram, or in another class diagram other than this blank model, the properties and functionality of the class will be lost.

Summarize the selected Java classes from the automatically generated class diagram using the steps shown in figure 9:

Select the class FuelTank,Engine,Passenger from classdiagram2.dnx and

Car

Right-click on a selected class to open the content menu

Select the Harvest menu

Paste the summarized class into the separate class diagram created in step 2.

Create aggregation and composition relationships between classes.

Figure 9. A summary class in a class diagram

Next, create an association between classes, which allows you to select one of the aggregation and composition relationships. Figure 10 shows a high-level class diagram.

Figure 10. High-level abstraction of UML class diagram generated by semi-automatic method

By comparing figure 10 with figure 9, you can find that the semi-automatic method can accurately display the relationship between classes. This diagram can be used for independent design and implementation documents, or for long-term improvement of the existing system.

Focus on:

Without summarization, aggregation and composition relationships cannot be applied to Rational Software Architect.

Create a sequence diagram

Sequence diagram is the most widely used UML dynamic modeling method. It is dedicated to identifying the behavior of a system. A sequence diagram is usually applied to modeling use cases to show the methods and functions of a system.

Rational Software Architect cannot automatically create a sequence diagram from Java code. The following steps show you how to create one:

Create a blank model.

Create a sequence diagram:

Right-click on Blank Model.

From the drop-down (context) menu, select Add Diagram and then Sequence Diagram (figure 11).

Add a class to the sequence diagram.

Sort the method tags between the two classes.

Save the sequence diagram.

Figure 11. Create a sequence diagram

When you have finished creating a sequence diagram, a sequence file is generated under the Collaboration: Interaction tag. You can add classes to the sequence diagram from the Java code. Both are shown in figure 12.

The main workspace of figure 12 shows an example of a sequence diagram.

Figure 12. Sequence diagrams generated from Java source files

A method call means that information is passed from the convener to the called. The called is the method owner, and it receives a message from the method convener. Information can be one-way or two-way. A sequence diagram consists of a series of method tags between a set of method owners and an initial initiator. The first tag must start at the place where the first method tag is initiated.

A hierarchical class diagram represents a way to discover hierarchical relationships between classes in a project or workset. This will help developers extend or add new functionality to an existing system. The high-level class diagram can directly show the aggregation and composition relationship between classes. This helps developers extend or modify an existing design. This is of great significance for developers to develop large-scale application systems. The sequence diagram shows the dynamic method tags between classes when performing specific tasks. This provides a clear operating schematic diagram for the system.

After reading the above, do you have any further understanding of how to reverse engineer UML class diagrams and sequence diagrams from Java code? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report