UML time sequence diagram automatic generation method based on Java source code analysis
By automatically generating UML sequence diagrams for Java code, it solves the professional barriers and legal risks of understanding and maintaining complex Java systems, enables intuitive system structure display and behavior analysis, and improves code readability and maintenance efficiency.
Patent Information
- Application Number
- CN202411242804.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-05
- Publication Date
- 2025-10-14
AI Technical Summary
Existing technologies require a lot of manual analysis of source code when understanding and maintaining complex Java software systems. Reverse engineering also has professional barriers and legal risks, and there is a lack of automated tools to help developers intuitively understand the system structure and behavior.
By converting Java code into an abstract syntax tree, extracting class declaration nodes and method call information, and using the Plant UML tool to automatically generate UML sequence diagrams, the method call relationships between classes can be intuitively displayed.
It improves the readability and maintainability of Java code, reduces dependence on source code, and simplifies the system understanding process.
Smart Images

Figure CN120780351A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application belongs to the field of software design, and relates to a UML timing diagram automatic generation method based on Java source code analysis. BACKGROUND
[0002] With the rapid development of computer technology, the scale and complexity of computer systems have increased dramatically, making the number of computer software rapidly and dramatically expand at an alarming rate, and many software projects have reached tens of thousands of lines or even millions of lines of code. How to improve the generation efficiency of software and the quality of software products has become the most important problem in software engineering.
[0003] However, in view of the actual situation, the maintenance of the old system occupies a large amount of time in the software development process. According to the description in the Software Engineering Encyclopedia in 1994, the cost for software maintenance in the late 1980s accounted for about 75% of the entire software production cycle, and rose to 90% in the early 1990s. Software engineers spend at least 50% of their time understanding existing systems, and most of the time in software maintenance is spent on understanding the target system, making the actual efficiency of the entire software development even more unsatisfactory, which also promotes the research and development of software understanding technology to develop rapidly.
[0004] The current mainstream method is to restore the design and implementation of software through reverse engineering, which is very useful for understanding complex systems or missing source code. However, reverse engineering requires certain professional knowledge and technology, and has a certain threshold for ordinary developers, and may involve legal and ethical issues, as it may infringe the copyright and intellectual property rights of software.
[0005] Even with reverse engineering, it is still time-consuming and laborious to fully and carefully understand a software system. Therefore, there is an urgent need for an automated tool to help developers understand software. Visualization technology, as part of software understanding technology, uses graphics, charts and other forms to display the structure, relationship and behavior of software systems, enabling developers to more intuitively understand and analyze software, and helping developers to find problems and bottlenecks in software systems, such as performance bottlenecks, logical errors, etc., thereby improving the quality and performance of software.
[0006] UML, with its well-defined, convenient system description, powerful functionality, and wide applicability, has become a key tool in visualization technology. UML dynamic model diagrams are used to depict the interactions between modules in a system. UML sequence diagrams, a type of dynamic model diagram, are a modeling tool used in software engineering to describe the interactions and temporal sequences between objects. They demonstrate the message passing and sequential flow between objects in a system, clearly expressing the interactions and temporal relationships between objects, helping developers better understand and design system behavior. Summary of the Invention
[0007] The purpose of the present invention is to provide a UML sequence diagram generation method for Java language source code in order to solve the problems existing in the above-mentioned prior art.
[0008] To achieve the object of the present invention, the present invention provides a method for automatically generating a UML sequence diagram based on Java source code analysis, comprising the following steps:
[0009] Step S1: Convert the Java code into its corresponding abstract syntax tree (AST), filter out all class declaration nodes in the AST, traverse the class declaration nodes and method subnodes through a traversal algorithm to obtain class information and method call information;
[0010] Step S2: Based on the class information and method call information extracted from the source code, the class name, method name and method call information between classes are integrated through the Plant UML code generation algorithm to obtain Plant UML code that can automatically generate UML sequence diagrams;
[0011] Step S3: Use the Plan UML tool to draw a sequence diagram of the Java source code.
[0012] Wherein, step S1 includes:
[0013] We use the Java language toolkit to obtain the corresponding AST of the source code and filter out all class declaration nodes in the AST. We then traverse the class declaration nodes and their method child nodes along the AST path, storing the method names in a list. After each class declaration node traversal, we append the current class name and method list to a dictionary, with the class name as the key and the method list as the value.
[0014] Wherein, step S2 includes:
[0015] Based on the class information and method call information extracted from the source code, class names are declared as objects in the sequence diagram. Method call relationships between classes are represented by message passing using arrows, with the arrow starting from the calling class and ending at the called class. Two nested loops are used to traverse the dictionary: the outer loop retrieves the class name and method list, while the inner loop iterates over each method in the method list, adding message passing between classes to the Plan UML code.
[0016] Compared with the prior art, the present invention has the following beneficial effects:
[0017] The UML sequence diagram automatic generation technology based on Java source code analysis of the present invention can intuitively reflect the program flow, avoids the need to view cumbersome source code to obtain class information and class call relationships, and improves the readability and maintainability of the code. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] Figure 1 This is the program's running flow chart.
[0019] Figure 2 It is a specific flow chart of the present invention. DETAILED DESCRIPTION
[0020] The present invention is further described below with reference to the accompanying drawings and examples. The present invention includes but is not limited to the following examples.
[0021] As shown in Figure 1, the automatic generation method of UML sequence diagram based on Java source code analysis includes the following steps:
[0022] Step S1: Convert the Java code into its corresponding abstract syntax tree, filter out all class declaration nodes in the abstract syntax tree, traverse the class declaration nodes and method subnodes through a traversal algorithm to obtain class information and method call information;
[0023] Step S2: Based on the class information and method call information extracted from the source code, the class name, method name and method call information between classes are integrated through the Plant UML code generation algorithm to obtain Plant UML code that can automatically generate UML sequence diagrams;
[0024] Step S3: Use the Plan UML tool to draw a sequence diagram of the Java source code.
[0025] Specific examples Figure 2, use the Java language toolkit to obtain the corresponding abstract syntax tree of the source code, and filter out all class declaration nodes in the abstract syntax tree, traverse the class declaration node and its method child nodes along the abstract syntax tree path, and store the method names in a list. After each class declaration node traversal is completed, the name of the current class and the method list are added to the dictionary, where the class name is used as the key and the method list is used as the value.
[0026] Abstract: In order to solve the problem of message passing between classes, a P l ant UML code based on the class information and method calling information extracted from the source code is proposed. The class name is declared as an object in the sequence diagram. The calling relationship of methods between classes is represented by arrows, the starting point of the arrow is the class that calls the method, and the end point is the class of the called method. Two nested loops are used to traverse the dictionary. The outer loop obtains the class name and method list, and the inner loop traverses each method in the method list, and the message passing between classes is added to the P l ant UML code.
[0027] By calling the Plant UML tool and using the Plant UML sequence diagram code returned above, you can draw the sequence diagram of the Java code.
[0028] The above content is merely an example and explanation of the structure of the present invention. Those skilled in the art may make various modifications or additions to the described specific embodiments or replace them in a similar manner. As long as they do not deviate from the structure of the invention or exceed the scope defined by the claims, they should all fall within the scope of protection of the present invention.
Claims
1. A method for automatically generating UML sequence diagrams based on Java source code analysis, characterized by: The following steps are involved: Step S1: Convert the Java code into its corresponding abstract syntax tree, filter out all class declaration nodes in the abstract syntax tree, traverse the class declaration nodes and method subnodes through a traversal algorithm to obtain class information and method call information; Step S2: Based on the class information and method call information extracted from the source code, the class name, method name and method call information between classes are integrated through the Plant UML code generation algorithm to obtain the Plant UML code that can automatically generate the UML sequence diagram; Step S3: Use Plant UML tool to draw a sequence diagram of the Java source code.
2. The method for automatically generating a UML sequence diagram based on Java source code analysis according to claim 1, characterized in that: Step S1 includes: Use the Javalang toolkit to obtain the corresponding abstract syntax tree of the source code, filter out all class declaration nodes in the abstract syntax tree, traverse the class declaration node and its method child nodes along the abstract syntax tree path, and store the method names in a list. After each class declaration node traversal is completed, add the name of the current class and the method list to the dictionary, where the class name is the key and the method list is the value.
3. The method for automatically generating a UML sequence diagram based on Java source code analysis according to claim 2 is characterized in that: Step S2 includes: Based on the class information and method call information extracted from the source code, the class name is declared as an object in the sequence diagram. The calling relationship of methods between classes is represented by arrows to represent message passing. The starting point of the arrow is the class that calls the method, and the end point is the class of the called method. Two nested loops are used to traverse the dictionary. The outer loop obtains the class name and method list, and the inner loop traverses each method in the method list, and the message passing between classes is added to the Plant UML code.
