Generating UML class diagrams and sequence diagrams from Java code reverse engineering

Source: Internet
Author: User

from:http://blog.itpub.net/14780914/viewspace-588975/

This article is intended for those software architects, designers and developers who want to use IBM? Rational? Software Architect from Java? Source code to reverse engineer the generation of UML classes and sequence diagrams. Reverse engineering is often used to derive lost design documents from existing source code in an abstract model UML format, which can be used to study the static structure and dynamic behavior of a system and to extend new features to the product. The authors detail the limitations of using IBM Rational software Architect for reverse engineering, and describe techniques to overcome these limitations. You will benefit from using these technical tips and tricks to identify components and generate high-level abstractions like UML classes and sequence diagrams from Java classes.

Software Architects, developers, and testers are familiar with the Unified Modeling Language (UML), which applies to documented use cases, class diagrams, sequence diagrams, and other diagrams. There are many other software tools available to help software engineers to do the work, either forward engineering or reverse engineering .

    • Forward Engineering is a traditional process of high-level abstraction, logic and independence design for a system physical structure implementation.
    • Reverse engineering is the analysis and processing of an existing system to identify its components and their intrinsic relations, so as to build a framework of a system with high-level abstraction. In most cases, reverse engineering is used to extract the lost design files from the existing source code in an abstract model UML format, so as to know the static structure and dynamic behavior of a system at the same time.

The essence of class and sequence diagram problems

Ibm? Rational? Software Architect is widely used in many industries because it offers a number of features to help reverse engineer. The problem is when you use Java? The Rational software Architect cannot automatically produce useful UML classes and sequence diagrams when the code constructs UML classes and sequence diagrams in reverse. But there is already a technique to improve the output of Rational software Architect. This article demonstrates how to use the technical techniques presented here to identify its components from Java code and to abstract high levels of UML types and sequence diagrams.

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

    • Discovering their abstract classes and identifying their hierarchical structure
    • A class diagram that produces high-level abstraction with aggregation and association relationships
    • Building a sequence diagram

The next section provides a solution for each problem and demonstrates how to produce meaningful classes and sequence diagrams. This example shows you how to identify a system's inheritance and components from a source code that has been given to a Java project in order to abstract high-level UML class diagrams and sequence diagrams.

Note:
The examples in this article arise from the Rational software Architect 7.0 version

Identify an inheritance tree for a UML class diagram

The inheritance relation is a kind of universal object type pattern. 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. It is quite useful to find the entire inheritance tree structure from an existing system, as it exposes you to what is the top 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 the Rational software Architect in the following three ways during the exploration process.

    • Discover its abstract structure from a workplace or work set
    • 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 class in an existing system. This allows you to use these classes as pointcuts to study classes in the inheritance tree. You can do this by following these steps.

    1. Open the Diagram Navigation view in Rational software Architect.
    2. Under object-oriented Pattern , right-click Abstraction, and then click Discover Architecture(1).

This can reveal the architecture of the entire workspace.


Figure 1: Discover the architecture of the entire workspace


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

    1. Open the Content menu by right-clicking on the class Car under abstraction .
    2. Displays the Car class diagram in the right panel by selecting the show Diagram option in the menu.
    3. Open the Content menu by selecting and right-clicking the class diagram Car in the right panel.
    4. Create a tree structure of the abstract class diagram by selecting the Explore in Browse Diagram option in the Content menu.

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


Figure 2: Discovering its abstract structure from the entire workplace


The result has the following drawbacks:

    • The same class and subclasses of the discovered classes in the tree may be lost.
    • Except for the discovered abstract classes, other classes have no attributes or operations.

Additional steps are required to get the tree structure produced by the sixth step. You need to increase the degree of separation (shown in 3), which determines the layer that is extended from the discovered class.


Figure 3: Changing the degree of separation


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

The second problem is that except for the classes found in the tree structure, other classes do not have properties and operations. This is not conducive to the use of the user for the purpose of re-use of the existing universal model.

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

    1. Load a Java project into the Rational software Architect.
    2. Switch to the Diagram Navigation view, as described in the previous steps, to discover its abstract structure from the workplace.
    3. Find an abstract class from the second step you are interested in.
    4. By looking for a class in the Model Finder, find the hierarchical tree structure class, double-click the class in the editor and press F4 to open the hierarchy tree. Make sure that type hierarchy is selected.
    5. Right-click on the class and turn it into a visual class diagram by selecting visualize > Add to New Diagram File > class Diagram, as shown in 4.
    6. Add the remaining classes to the current diagram by right-clicking and selecting visualize > Add to Diagram .

Figure 4: Visualize a class as a new class diagram


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

    1. Open and press F4 to display the class level.
    2. Select each of the classes and add them to the class diagram.
    3. Check and complete the diagram on the right.

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


The class diagram is produced in the Rational software Architect default format. There are several useful modifications that can help you visualize your diagram. For example, you can modify the connection path style to use the tree style path, and you can open the Content menu by right-clicking in the workspace and clicking Arrange All. The resulting class diagram will look better than the automatic generation. As shown in 6.


Figure 6: Inheritance tree with attributes and tree path connection


The class in Figure 6 shows both the property and the operation. The benefits of properties and operations display are that you can study their common state and behavior to further understand how an existing system is being implemented, which will help reuse the system.

Generate a high-level UML class diagram

Rational software Architect enables you to generate class diagrams by selecting multiple Java files from a single Java project.

    1. In the Model Finder, use visualize to add them to a new diagram or to the current class diagram.

If more than one class has 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 auto-generated class diagram


As shown in 7, you can select multiple Java files from the Model finder to visualize them in the new class diagram. If you want to add more classes, you can choose more Java source code to visualize them in the current class diagram. This figure shows that the classes are included in the project and their basic relationships. This facilitates the automatic discovery of UML classes in the project, but the auto-discovery relationships are not useful here.

In Figure 7, almost all of the relationships are use to remove an inheritance relationship. The use of relationships is 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 behaves as a one-to-many relationship when a class contains many items of other classes, the composition relationship is used to describe one more relationship when a class contains only one example of another class. This high-level abstraction implies a more precise relationship discovery 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 the abstract relationship.

Here, we try and explore a high-level abstraction scheme that produces UML class diagrams in a semi-automatic way. UML classes are found in the same technology as before, and relationships between classes are specified by humans. The high-level abstraction is based on the knowledge necessary to study the existing source code.

The example in Figure 8 shows how to use this method to get a UML class diagram of a high level.


Figure 8: Creating a blank model using the UML Model Wizard


In order to get a high-level class diagram, you must first create a blank model.

    1. Create a new blank model as described in Figure 8:
      1. Under File type, select UML Modeling.
      2. Under Templates, select Blank Model.
      3. In the file name area, enter Blank Model.
      4. For destination folder, enter example.
      5. The default diagram check box "Create a default diagram in the new model" should be checked.
      6. For the default diagram type, select Freeform. Diagram.
      7. 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 another class diagram other than this blank model, then the properties and functionality of the class are lost.

    1. Summarize the selected Java classes from the automatically generated class diagram using the steps shown in Figure 9:
      1. Select Class Fueltank,Engine,passenger and Car from Classdiagram2.dnx
      2. Right-click on a selected class to open the Content menu
      3. Select Harvest Menu
    2. Paste the summarized class into the detached class diagram that was created in step 2.
    3. Create aggregation and composition relationships between classes.

Figure 9. Summary class in a class diagram


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


Figure 10: High-level abstraction of UML class diagrams produced by semi-automatic methods


By comparing Figure 10 with Figure 9, you can see that the semi-automatic method can accurately display the inter-class relationship. This diagram can be used to design execution files independently, or to improve long-term systems.

The Focus:
If not summarized, aggregation and composition relationship will not 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 typically 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 will show you how to create a:

    1. Create a blank model.
    2. Create a sequence diagram:
      1. Right-click on the Blank Model .
      2. From the Drop-down menu, select Add Diagram and then select Sequence Diagram(11).
    3. Adds a class to a sequence diagram.
    4. The method tags are sorted between two classes.
    5. Save the sequence diagram.

Figure 11. Create a sequence diagram


When you are finished creating a sequence diagram, a sequence file is generated under the collaboration:interaction tab. You can add classes to a sequence diagram from Java code. Both are seen in Figure 12.

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


Figure 12 sequence diagram generated from the Java source file


A method invocation means that the information is transmitted from the convener to the summoned person. The summoned person is the method owner, which receives information from the method convener. The information can be pipettes or double-channel. A sequence diagram consists of a series of method tokens that are between a set of method owners and an initial initiator. The first tag must start where the first method tag was initiated.

Summarize

This article demonstrates how to create UML classes and sequence diagrams from Java code using reverse engineering by applying the Rational software Architect v7.0. A hierarchical class diagram represents a way to discover a project or work set,

Generating UML class diagrams and sequence diagrams from Java code reverse engineering

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.