A method for extracting reusable components

By hierarchical processing of software source code and component analysis, potential reusable modules are determined, and code is extracted and reconstructed, the problem of automatic extraction of reusable code in the existing technology is solved, the reusability and quality of the code is improved, and the realization of the code is realized. Efficient maintenance of the code base.

CN117806624BActive Publication Date: 2024-12-31BEIJING BEIDA SOFTWARE ENG DEV CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311691375.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-12-11
Publication Date
2024-12-31
Estimated Expiration
2043-12-11

AI Technical Summary

Technical Problem

In software development, it is difficult for the existing technology to extract reusable code efficiently and automatically, causing developers to spend a lot of time and effort to find and abstract reusable code, resulting in code redundancy and inconsistency, reducing the maintainability of the code and reusability.

Method used

By hierarchically processing the software source code, the code base is built at the hierarchy, the potential reusable modules are determined using component analysis method, and module extraction, abstraction and reconstruction operations are performed to obtain reusable components, and unit testing and integration testing are performed. , add to the template library to ensure it is reusable.

Benefits of technology

It improves code reusability, reduces code redundancy, improves code quality and development efficiency, automatically extracts reusable code, reduces the time and energy of manually finding reusable code, and improves the maintainability of the code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117806624B_ABST
    Figure CN117806624B_ABST
Patent Text Reader

Abstract

The present invention relates to the field of component analysis, and discloses a method for extracting reusable components, comprising: hierarchically processing the source code of software, constructing a code base according to the hierarchy, using component analysis to determine the potential reusable modules of the project to be analyzed, extracting modules from the potential reusable modules, and obtaining reusable components. The method can automatically extract reusable codes and components, reduce the time and effort of manually searching for reusable codes, improve the reusability and quality of codes, and thus improve development efficiency and code maintainability. The method can be widely used in software development.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to the field of component analysis, and in particular to a method for extracting reusable components. Background Art

[0002] In software development, code reuse is a widely adopted practice. Reusing code can help developers improve code quality, speed up development, reduce costs, etc. However, in practice, many code reuse practices are often done manually, requiring developers to spend a lot of time and energy to find and abstract reusable code. This not only wastes developers' time and energy, but also may lead to code redundancy and inconsistency, thereby reducing the maintainability and reusability of the code.

[0003] To solve this problem, some techniques and methods have been proposed to automatically extract reusable code. One of them is component analysis, which exploits the similarity and repetition in the code, decomposes the code into smaller components, and then compares these parts to find commonalities. However, in practice, how to effectively use component analysis technology to accurately extract reusable software modules remains a challenge. Summary of the invention

[0004] The purpose of the present invention is to overcome one or more of the above-mentioned existing technical problems and provide a method for extracting reusable components.

[0005] To achieve the above object, the present invention provides a method for extracting a reusable component, comprising:

[0006] Process the source code of the software in different levels and build the code base according to the levels;

[0007] Use component analysis to identify potential reusable modules of the project to be analyzed;

[0008] Extract potentially reusable modules and obtain reusable components.

[0009] According to one aspect of the present invention, the source code of the software is hierarchically constructed into a code library according to classes, methods and modules, wherein a method is an independent function and a module is a code fragment contained in a method.

[0010] According to one aspect of the present invention, the code library includes class information, the class information includes fingerprint information of software source code, the fingerprint information includes the fingerprint of the original file, the fingerprint of the cleaned file and the fingerprint of the replacement file, wherein the cleaned file is obtained by code cleaning the original file, and the replacement file is obtained by replacing the variable name or function name of the cleaned file;

[0011] The code library also includes method information, the method information includes fingerprint information of the method, the method fingerprint information includes the fingerprint of the original method, the fingerprint of the cleaned method and the fingerprint of the replacement method, wherein the cleaned method is obtained by performing a code cleaning operation on the original method, and the replacement method is obtained by replacing the variable name contained in the cleaned method;

[0012] The code base also includes module information, which includes location information, file information, length information, and index information;

[0013] The code cleaning operation includes removing comments and blank lines in the code.

[0014] According to one aspect of the present invention, the process of determining a potential reusable module includes:

[0015] Layer the analysis items according to classes, methods and modules;

[0016] According to the fingerprint characteristics of the class information, determine the correlation between the class files in the project to be analyzed and the class files in the code base;

[0017] In response to the class file to be analyzed having a correlation with the class files in the code base, determining that the class file to be analyzed is potentially reusable;

[0018] In response to the class file and the class file in the code base not having a correlation, confirming the correlation between the method of the project to be analyzed and the method in the code base according to the fingerprint characteristics of the method;

[0019] Build a module index based on the modules of the project to be analyzed, and confirm the correlation based on the module index and the index information of the module information in the code base;

[0020] Based on the correlation between the methods and modules to be analyzed and the methods and modules in the code base, it is determined that the corresponding methods and modules are potentially reusable.

[0021] According to one aspect of the present invention, extracting a potentially reusable module to obtain a reusable component includes:

[0022] Identify categories of potentially reusable modules;

[0023] Perform abstract operations or refactoring operations on reusable modules to obtain reusable components;

[0024] Abstract operations include generalizing parameters, variables, or types in a module;

[0025] Refactoring operations include refactoring methods with the same functional classification into classes, or refactoring modules into methods.

[0026] According to one aspect of the present invention, unit testing or integration testing is performed on reusable components.

[0027] According to one aspect of the present invention, a reusable component is added to a template library, and the template library is constructed based on a version control system.

[0028] According to one aspect of the present invention, the process of obtaining the source code of the software includes:

[0029] The project files are obtained from the code repository, the original code files are obtained after removing the non-code files, and the source code of the source code is obtained after removing the duplicate code files from the original code files.

[0030] To achieve the above objectives, the present invention provides an electronic device, including a processor, a memory, and a computer program stored in the memory and executable on the processor, wherein the computer program implements the above extraction method for a reusable component when executed by the processor.

[0031] To achieve the above object, the present invention provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the above extraction method for a reusable component is implemented.

[0032] Based on this, the beneficial effects of the present invention are:

[0033] A method for extracting reusable software modules using multi-level component analysis technology is provided. The method uses component analysis technology to decompose the code at multiple levels, uses code cloning, similarity analysis and comparison technology to identify and extract reusable code, abstracts and reconstructs the extracted code modules to create a general code library and module, performs unit testing and integration testing on the extracted reusable code blocks to ensure that they can work properly, and performs maintenance to keep them updated. Code reusability can be improved, code redundancy can be reduced, and thus code quality and development efficiency can be improved. Reusable code and components can be automatically extracted, reducing the time and effort of manually searching for reusable code, improving code reusability and quality, and thus improving development efficiency and code maintainability. The method can be widely used in software development. BRIEF DESCRIPTION OF THE DRAWINGS

[0034] Figure 1 The present invention is a flowchart of a method for extracting a reusable component. DETAILED DESCRIPTION

[0035] The present invention will now be discussed with reference to exemplary embodiments. It should be understood that the embodiments discussed are only for enabling those skilled in the art to better understand and thereby implement the present invention, rather than implying any limitation on the scope of the present invention.

[0036] As used herein, the term “including” and variations thereof are to be interpreted as open-ended terms meaning “including but not limited to.” The term “based on” is to be interpreted as “based, at least in part, on,” and the terms “one embodiment” and “an embodiment” are to be interpreted as “at least one embodiment.”

[0037] According to one embodiment of the present invention, Figure 1 is a flow chart of a method for extracting a reusable component in the present invention, such as Figure 1 As shown, a method for extracting a reusable component includes:

[0038] To achieve the above object, the present invention provides a method for extracting a reusable component, comprising:

[0039] Process the source code of the software in different levels and build the code base according to the levels;

[0040] Use component analysis to identify potential reusable modules of the project to be analyzed;

[0041] Extract potentially reusable modules and obtain reusable components.

[0042] According to one embodiment of the present invention, the source code of the software is hierarchically constructed into a code library according to classes, methods and modules, wherein a method is an independent function and a module is a code fragment contained in a method.

[0043] According to one embodiment of the present invention, the code library includes class information, the class information includes fingerprint information of the software source code, the fingerprint information includes the fingerprint of the original file, the fingerprint of the cleaned file and the fingerprint of the replacement file, wherein the cleaned file is obtained by code cleaning the original file, and the replacement file is obtained by replacing the variable name or function name of the cleaned file;

[0044] The code library also includes method information, the method information includes fingerprint information of the method, the method fingerprint information includes the fingerprint of the original method, the fingerprint of the cleaned method and the fingerprint of the replacement method, wherein the cleaned method is obtained by performing a code cleaning operation on the original method, and the replacement method is obtained by replacing the variable name contained in the cleaned method;

[0045] The code base also includes module information, which includes location information, file information, length information, and index information;

[0046] The code cleaning operation includes removing comments and blank lines in the code.

[0047] When the source code also includes redundant information content such as macro definitions, the code cleaning operation may also include removing redundant and repeated content.

[0048] According to one embodiment of the present invention, the process of determining a potential reusable module includes:

[0049] Layer the analysis items according to classes, methods and modules;

[0050] According to the fingerprint characteristics of the class information, determine the correlation between the class files in the project to be analyzed and the class files in the code base;

[0051] In response to the class file to be analyzed having a correlation with the class files in the code base, determining that the class file to be analyzed is potentially reusable;

[0052] In response to the class file and the class file in the code base not having a correlation, confirming the correlation between the method of the project to be analyzed and the method in the code base according to the fingerprint characteristics of the method;

[0053] Build a module index based on the modules of the project to be analyzed, and confirm the correlation based on the module index and the index information of the module information in the code base;

[0054] Based on the correlation between the methods and modules to be analyzed and the methods and modules in the code base, it is determined that the corresponding methods and modules are potentially reusable.

[0055] According to one embodiment of the present invention, extracting a potentially reusable module to obtain a reusable component includes:

[0056] Identify categories of potentially reusable modules;

[0057] Perform abstract operations or refactoring operations on reusable modules to obtain reusable components;

[0058] Abstract operations include generalizing parameters, variables, or types in a module;

[0059] Refactoring operations include refactoring methods with the same functional classification into classes, or refactoring modules into methods.

[0060] According to one embodiment of the present invention, unit testing or integration testing is performed on the reusable components.

[0061] According to one embodiment of the present invention, a reusable component is added to a template library, and the template library is constructed based on a version control system.

[0062] According to one embodiment of the present invention, the process of obtaining the source code of the software includes:

[0063] The project files are obtained from the code repository, the original code files are obtained after removing the non-code files, and the source code of the source code is obtained after removing the duplicate code files from the original code files.

[0064] According to one embodiment of the present invention, source code projects of domestic and foreign open source websites and communities are collected, including GITHUB, GITEE, GOOGLE CODE, APACHE and other open source websites and communities. Since the number of source code projects is too large and many open source projects are sample projects or student graduation projects, these projects are basically not relied on by other projects. For the sake of time efficiency and space saving, priority can be given to collection based on popularity and popularity, such as the star and fork indexes in GITHUB.

[0065] According to one embodiment of the present invention, the code files in the source code project are screened out, and more than 2,500 code file suffix information corresponding to more than 600 programming languages ​​are included by setting a whitelist, and non-code files in formats such as .txt .png .md are filtered out to reduce the pressure on disk space occupancy, and duplicate code files between different versions of the same component are removed, thereby improving the speed of further data processing and the efficiency of warehousing.

[0066] According to one embodiment of the present invention, a project is decomposed into three levels: class files, functions, and code snippets. First, the project is grouped according to different programming languages ​​to obtain class files in the source code project. Then, the function is decomposed according to the function definition rules of different languages, and the minimum function line threshold is set to 5. Functions with fewer lines than the threshold are ignored because functions with too few lines are not characteristic. Taking JAVA as an example, an entity class will generate some setters, getter functions, and class constructors that have no practical meaning. Finally, the code snippets are decomposed according to the content of the source code file.

[0067] According to an embodiment of the present invention, project decomposition is performed to decompose the project into three levels: class files, functions, and code snippets. First, the project is grouped according to different programming languages ​​to obtain class files in the source code project. Then, the function is decomposed according to the function definition rules of different languages, and the minimum function line threshold is set to 5. Functions with lines less than the threshold are ignored because functions with too small lines are not characteristic. Taking JAVA as an example, the entity class will generate some setters, getter functions, and class constructors that have no practical meaning. Finally, the code snippets are decomposed according to the content of the source code file. A component analysis database is constructed, and the decomposed results are used to process and extract database information used for component analysis at different levels. The database entries generated for class files contain basic file information such as file name, path, project, etc. In addition, it also contains three types of fingerprint information, namely original file fingerprint, file fingerprint after removing comment blank lines, and fingerprints such as replacement variable name and function name; the database entries generated for functions contain basic function information such as function name, number of lines, file, etc. In addition, it also contains three types of fingerprint information similar to file fingerprint information; the database entry information generated for code snippets contains basic code snippet information such as starting line number, file location, number of snippet lines, and index information of code snippets. Note that code snippets no longer replace variable names and function names. Considering that code snippets are the finest granularity, generalization processing will increase the false alarm rate. The multi-level component analysis technology is used to identify potential reusable modules. There are two cases. The first case is to identify reusable modules in a given single project and the component analysis database. The second case is to identify reusable code modules between multiple projects. The analysis and identification steps of the two cases are similar, including the following steps: first, the project is decomposed into three levels: class files, functions, and code snippets. Each class file is analyzed and identified, and the original file fingerprint, the fingerprint after removing comments and blank lines, the fingerprint information of the replaced variable name and function name are used in turn to compare with the component analysis database or multiple projects, and the similar information of the comparison is recorded, including the number of similarities, the project to which the similar files belong, and the similar file path information. When no similar results are identified for a given class file, function identification is performed. Similar to file identification, various fingerprint features of the function are used in turn to compare and record the result information. Different from the function and file identification methods, code snippets are identified based on indexes, and the selected snippets are indexed and constructed. The index information is compared with the code snippet library. After the index comparison is successful, the line where the index is located is compared upward and downward until the dissimilar line is compared, so as to determine the start and end line information of the similar snippet. The comparison result information is also recorded.

[0068] According to one embodiment of the present invention, after the potential reusable module is identified, it needs to be further processed including classification, abstraction and reconstruction, etc., which mainly includes the following steps. The present invention has been pre-classified according to the hierarchy when identifying and saving the results. At the same time, it can be further classified according to the module function. The module can be classified into categories such as database operation, file operation, thread operation, etc. according to the code function using a machine learning method. The module can also be classified according to the type of programming language. The purpose of classification is to make the module easier to maintain and use. The implementation and use of the module are separated to make the module more versatile and reusable. The abstraction and reconstruction method used in the present invention is mainly to generalize the parameters, variables, types, etc. in the module, and to extract the code segment into a function, extract the same method according to the functional classification into a class, and apply the design pattern to reconstruct the class module. Unit testing and integration testing are performed on the abstracted and reconstructed modules to ensure that the extracted modules can be used normally. The template library of the present invention uses the Git version control system to assist in construction. Adding to the template library requires code submission and review. At the same time, the version control system can be used to perform a rollback operation when an error submission occurs to ensure the quality and maintainability of the code. After adding reusable modules to the module library, other developers can use these modules when developing new software systems. Using reusable modules can reduce development time and cost, and improve code quality and maintainability. When using reusable modules, you need to pay attention to the module usage and interface to ensure the correct use of the module.

[0069] To achieve the above-mentioned purpose of the invention, the present invention also provides an electronic device, which includes: a processor, a memory, and a computer program stored in the memory and executable on the processor, and when the computer program is executed by the processor, the above-mentioned method for extracting a reusable component is implemented.

[0070] To achieve the above-mentioned purpose of the invention, the present invention also provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the above-mentioned method for extracting a reusable component is implemented.

[0071] Based on this, the beneficial effect of the present invention is that a method for extracting reusable software modules using multi-level component analysis technology is provided. The method uses component analysis technology to perform multi-level decomposition of the code, uses code cloning, similarity analysis and comparison technology to identify and extract reusable code, and abstracts and reconstructs the extracted code modules to create a general code library and module, performs unit testing and integration testing on the extracted reusable code blocks to ensure that they can work properly, and maintains them to keep them updated. Code reusability can be improved, code redundancy can be reduced, thereby improving code quality and development efficiency. Reusable code and components can be automatically extracted, reducing the time and energy of manually searching for reusable code, improving the reusability and quality of the code, thereby improving development efficiency and code maintainability. The method can be widely used in software development.

[0072] Those of ordinary skill in the art will appreciate that the modules and algorithm steps described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.

[0073] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described devices and equipment can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0074] In the embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the modules is only a logical function division. There may be other division methods in actual implementation, such as multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or modules, which can be electrical, mechanical or other forms.

[0075] The modules described as separate components may or may not be physically separated, and the components shown as modules may or may not be physical modules, that is, they may be located in one place or distributed on multiple network modules. Some or all of the modules may be selected according to actual needs to achieve the purpose of the embodiments of the present invention.

[0076] In addition, each functional module in the embodiment of the present invention may be integrated into one processing module, or each module may exist physically separately, or two or more modules may be integrated into one module.

[0077] If the functions are implemented in the form of software function modules and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the energy-saving signal sending / receiving method of each embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as USB flash drives, mobile hard disks, ROM, RAM, magnetic disks or optical disks.

[0078] The above description is only a preferred embodiment of the present application and an explanation of the technical principles used. Those skilled in the art should understand that the scope of the invention involved in the present application is not limited to the technical solution formed by a specific combination of the above technical features, but should also cover other technical solutions formed by any combination of the above technical features or their equivalent features without departing from the inventive concept. For example, the above features are replaced with the technical features with similar functions disclosed in this application (but not limited to) by each other to form a technical solution.

[0079] It should be understood that the size of the serial numbers of each step in the content of the invention and the embodiments of the present invention does not absolutely mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.

Claims

1. A method for extracting a reusable component, characterized in that: include: Process the source code of the software in different levels and build the code base according to the levels; The source code of the software is hierarchically structured into code libraries according to classes, methods, and modules. Methods are independent functions, and modules are code snippets contained in methods. Use component analysis to identify potential reusable modules of the project to be analyzed; Extract potentially reusable modules and obtain reusable components; The code base contains class information, and the class information contains fingerprint information of the software source code. The fingerprint information includes the fingerprint of the original file, the fingerprint of the cleaned file, and the fingerprint of the replacement file. The cleaned file is obtained by code cleaning the original file, and the replacement file is obtained by replacing the variable name or function name of the cleaned file. The code library also includes method information, the method information includes fingerprint information of the method, the method fingerprint information includes the fingerprint of the original method, the fingerprint of the cleaned method and the fingerprint of the replacement method, wherein the cleaned method is obtained by performing a code cleaning operation on the original method, and the replacement method is obtained by replacing the variable name contained in the cleaned method; The code base also includes module information, which includes location information, file information, length information, and index information; The code cleaning includes removing comments and blank lines in the code.

2. A method for extracting a reusable component as claimed in claim 1, characterized in that: The process of identifying potentially reusable modules includes: Layer the analysis items according to classes, methods and modules; According to the fingerprint characteristics of the class information, determine the correlation between the class files in the project to be analyzed and the class files in the code base; In response to the class file to be analyzed having a correlation with the class files in the code base, determining that the class file to be analyzed is potentially reusable; In response to the class file and the class file in the code base not having a correlation, confirming the correlation between the method of the project to be analyzed and the method in the code base according to the fingerprint characteristics of the method; Build a module index based on the modules of the project to be analyzed, and confirm the correlation based on the module index and the index information of the module information in the code base; Based on the correlation between the methods and modules to be analyzed and the methods and modules in the code base, it is determined that the corresponding methods and modules are potentially reusable.

3. A method for extracting a reusable component as claimed in claim 2, characterized in that: Extract the potentially reusable modules and obtain the reusable components including: Identify categories of potentially reusable modules; Perform abstract operations or refactoring operations on reusable modules to obtain reusable components; Abstract operations include generalizing parameters, variables, or types in a module; Refactoring operations include refactoring methods with the same functional classification into classes, or refactoring modules into methods.

4. A method for extracting a reusable component as claimed in claim 3, characterized in that: Perform unit or integration testing on reusable components.

5. A method for extracting a reusable component as claimed in claim 4, characterized in that: Add reusable components to the template library, which is built on top of a version control system.

6. A method for extracting a reusable component as claimed in claim 5, characterized in that: The process of obtaining the software source code includes: The project files are obtained from the code repository, the original code files are obtained after removing the non-code files, and the source code of the source code is obtained after removing the duplicate code files from the original code files.

7. An electronic device, characterized in that: The invention comprises a processor, a memory and a computer program stored in the memory and executable on the processor, wherein when the computer program is executed by the processor, a method for extracting a reusable component as claimed in any one of claims 1 to 6 is implemented.

8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method for extracting a reusable component according to any one of claims 1 to 6 is implemented.