A code reconstruction method, device, equipment and storage medium

By extracting and processing code reconstruction instructions, and using text classifiers and code attributes to generate duplicate code files, the time-consuming and labor-intensive problem of code reconstruction in the existing technology is solved, and rapid and accurate code reconstruction and efficiency improvement are achieved.

CN113238796BActive Publication Date: 2024-09-20BEIJING JINGDONG ZHENSHI INFORMATION TECH CO LTD

Patent Information

Application Number
CN202110534889.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-05-17
Publication Date
2024-09-20
Estimated Expiration
2041-05-17

AI Technical Summary

Technical Problem

The existing code reconstruction methods are time-consuming and labor-intensive, easy to miss changes, and have low reconstruction efficiency. Especially when there is a lot of historical code and duplicate codes, how to quickly and accurately extract and reconstruct duplicate codes is a big challenge.

Method used

Code reconstruction is achieved by detecting code reconstruction instructions, obtaining code files to be reconstructed, extracting and determining code attributes of duplicate code texts, generating duplicate code files, and adjusting the calling logic of code files to be reconstructed based on these files. The method includes using a text classifier to extract duplicate code, generating duplicate code files based on code attributes, and automating the code refactoring process.

Benefits of technology

It achieves fast and accurate code reconstruction, reduces omissions, shortens code reconstruction time, improves reconstruction efficiency, and significantly speeds up code reconstruction.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113238796B
    Figure CN113238796B
Patent Text Reader

Abstract

The embodiment of the present invention discloses a code refactoring method, apparatus, device and storage medium, the method comprising: in response to a detected code refactoring instruction, obtaining a code file to be refactored corresponding to the code refactoring instruction; extracting repeated code text in the code file to be refactored, and determining the code attribute of the repeated code text; generating a repeated code file corresponding to the repeated code text based on the code attribute, and performing code refactoring of the code file to be refactored according to the repeated code file. The method provided by the embodiment of the present invention automatically extracts repeated code books in the code file to be refactored to perform code refactoring, thereby realizing rapid extraction of repeated and redundant parts in the code file to be refactored, avoiding omission of unextracted files, accelerating code refactoring speed, shortening code refactoring time, and improving code refactoring efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] Embodiments of the present invention relate to the field of computer technology, and in particular to a code reconstruction method, apparatus, device and storage medium. Background Art

[0002] During the testing process, it is often necessary to implement automated code writing, and refactoring is often involved. Usually, for similar automated code use cases, there are many repetitive code statements. According to iterative writing, if there is a lot of historical code that has accumulated for a long time, how to quickly find the repeated code and extract it independently is a huge workload.

[0003] The most common way at present is to read through, extract and delete, and finally reconstruct the latest code. In the process of implementing the present invention, the inventor found that there are at least the following technical problems in the prior art: the existing code reconstruction method is time-consuming and labor-intensive, easy to miss changes, and has low reconstruction efficiency. Summary of the invention

[0004] The embodiments of the present invention provide a code reconstruction method, apparatus, device and storage medium to achieve fast and accurate completion of code reconstruction.

[0005] In a first aspect, an embodiment of the present invention provides a code reconstruction method, comprising:

[0006] In response to the detected code refactoring instruction, obtaining a to-be-refactored code file corresponding to the code refactoring instruction;

[0007] Extracting repeated code texts in the code file to be refactored, and determining code attributes of the repeated code texts;

[0008] A duplicate code file corresponding to the duplicate code text is generated based on the code attribute, and code refactoring of the code file to be refactored is performed according to the duplicate code file.

[0009] Optionally, based on the above solution, duplicate code text in the code file to be refactored is extracted, including:

[0010] The code file to be refactored is input into a pre-built text classifier to obtain the repeated code text extracted by the text classifier.

[0011] Optionally, based on the above solution, the code attributes of the repeated code text are determined, including:

[0012] The position of the repeated code text in the test case is obtained, and the code attribute is determined based on the position.

[0013] Optionally, based on the above solution, the code attributes of the repeated code text are determined, including:

[0014] Code keywords are extracted from repeated code texts, and code attributes are determined based on the code keywords.

[0015] Optionally, based on the above solution, a duplicate code file corresponding to the duplicate code text is generated based on the code attributes, including:

[0016] When the code attribute is a logical attribute, a repeated code file corresponding to the repeated code text is generated.

[0017] Optionally, based on the above solution, a duplicate code file corresponding to the duplicate code text is generated, including:

[0018] A repeated code file containing repeated code text is generated, and a file attribute of the repeated code file corresponding to the repeated code text is determined according to the code attribute of the repeated code text.

[0019] Optionally, based on the above solution, code refactoring of the code file to be refactored is performed according to the duplicate code file, including:

[0020] The calling logic of the non-duplicate code text in the code file to be refactored is adjusted based on the file attribute of the duplicate code file, and the code refactoring of the code file to be refactored is completed.

[0021] In a second aspect, an embodiment of the present invention further provides a code reconstruction device, comprising:

[0022] A file acquisition module, configured to, in response to a detected code refactoring instruction, acquire a to-be-refactored code file corresponding to the code refactoring instruction;

[0023] A repeated text extraction module is used to extract repeated code text in the code file to be refactored and determine the code attributes of the repeated code text;

[0024] The code refactoring module is used to generate a duplicate code file corresponding to the duplicate code text based on the code attributes, and perform code refactoring of the code file to be refactored according to the duplicate code file.

[0025] In a third aspect, an embodiment of the present invention further provides a computer device, the device comprising:

[0026] one or more processors;

[0027] A storage device for storing one or more programs;

[0028] When one or more programs are executed by one or more processors, the one or more processors implement the code reconstruction method provided by any embodiment of the present invention.

[0029] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements a code reconstruction method as provided in any embodiment of the present invention.

[0030] The embodiment of the present invention obtains the code file to be reconstructed corresponding to the code reconstructing instruction in response to the detected code reconstructing instruction; extracts the repeated code text in the code file to be reconstructed, and determines the code attribute of the repeated code text; generates the repeated code file corresponding to the repeated code text based on the code attribute, and performs code reconstructing of the code file to be reconstructed according to the repeated code file, and performs code reconstructing by automatically extracting the repeated code book in the code file to be reconstructed, thereby realizing fast extraction of repeated and redundant parts in the code file to be reconstructed, avoiding omission of unextracted files, accelerating the code reconstruction speed, shortening the code reconstruction time, and improving the code reconstruction efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0031] Figure 1 is a flow chart of a code refactoring method provided by the first embodiment of the present invention;

[0032] FIG2 is a flow chart of a code reconstruction method provided by Embodiment 2 of the present invention;

[0033] Figure 3 It is a structural schematic diagram of a code reconstruction device provided by Embodiment 3 of the present invention;

[0034] Figure 4 It is a structural diagram of a computer device provided in Embodiment 4 of the present invention. DETAILED DESCRIPTION

[0035] The present invention will be further described in detail below in conjunction with the accompanying drawings and embodiments. It is to be understood that the specific embodiments described herein are only used to explain the present invention, rather than to limit the present invention. It should also be noted that, for ease of description, only parts related to the present invention, rather than all structures, are shown in the accompanying drawings.

[0036] Embodiment 1

[0037] Figure 1 This is a flowchart of a code refactoring method provided in the first embodiment of the present invention. This embodiment is applicable to situations when code refactoring is performed, and is particularly applicable to situations when code structure has obvious characteristics (such as code based on selenium+testng or other existing frameworks, used for long-term code accumulation, and excessive repetition of the same code beginning or ending characteristics). This method can be executed by a code refactoring device, and the code refactoring device can be implemented in software and / or hardware. For example, the code refactoring device can be configured in a computer device. Figure 1As shown, the method includes:

[0038] S110 . In response to the detected code refactoring instruction, obtain a to-be-refactored code file corresponding to the code refactoring instruction.

[0039] In this embodiment, the code refactoring instruction may be an instruction initiated by the user to instruct code refactoring. Exemplarily, after the user adds the information of the code file to be refactored in the operation terminal, he can click the code refactoring control to trigger the code refactoring request. After the operation terminal detects the code refactoring request initiated by the user, it obtains the information associated with the code refactoring request (such as the information of the code file to be refactored, user information), etc. to generate the code refactoring instruction, and sends the code refactoring instruction to the code refactoring device. After the code refactoring device detects the code refactoring instruction sent by the operation terminal, it parses the code refactoring instruction to obtain the code file information to be refactored corresponding to the code refactoring instruction, and then obtains the code file to be refactored. Among them, the code file information to be refactored can be a specific file, or it can be the uniform resource locator (Uniform Resource Locator, URL) of the code file to be refactored. If the code file information to be refactored is a specific file, the code file to be refactored can be directly parsed from the code refactoring instruction to obtain; if the code file information to be refactored is the URL of the code file to be refactored, the code file to be refactored is obtained from the URL.

[0040] The code file to be refactored may include multiple test cases, and the same code text exists in at least two test cases.

[0041] S120: extract the repeated code text in the code file to be refactored, and determine the code attributes of the repeated code text.

[0042] In this embodiment, the repeated code text in the code file to be reconstructed is extracted by a similar text extraction algorithm. Considering that the repeated code text is a specific function implementation part, it cannot be used as a public file. Therefore, after the repeated code text is extracted, it is necessary to determine the code attribute of the repeated code text, and determine whether to generate the corresponding repeated code file as a public file based on the code attribute. The code attribute of the repeated code text can be determined manually or based on the text features of the repeated code text.

[0043] Optionally, there is no limitation on the specific similar text extraction algorithm. For example, the text features in the code file to be reconstructed can be extracted through a feature extraction model (such as a trained neural network model), and the texts with the same text features can be regarded as duplicate code texts; the literal similarity of the text in the code file to be reconstructed can also be directly calculated through an existing similarity calculation method, and the texts with the same literal similarity can be regarded as duplicate code texts, etc.

[0044] In one embodiment of the present invention, the repeated code text in the code file to be reconstructed is extracted, including: inputting the code file to be reconstructed into a pre-built text classifier to obtain the repeated code text extracted by the text classifier. Optionally, an existing text classifier (such as any classifier such as a naive Bayes classifier, a decision tree, a maximum entropy classifier, etc.) can be used to extract the repeated code text in the code file to be reconstructed. It can be understood that the code file to be reconstructed is input into the text classifier, the code file to be reconstructed is used as an input parameter of the text classifier, the classification logic of the text classifier is executed, and the output result of the text classifier is obtained as the repeated code text. Using a text classifier to extract repeated code text in the code file to be reconstructed makes the extraction of repeated code text faster and more accurate.

[0045] In one embodiment, determining the code attributes of repeated code text includes: obtaining the position of the repeated code text in the test case, and determining the code attributes based on the position. In general, the position of the code in the test case can characterize its attributes, such as the code at the beginning of the test case is generally the reference part and the initialization part, the code in the middle part of the test case is generally the implementation part, and the code at the end of the test case is generally the verification part. It is understandable that the reference part, the initialization part and the verification part can be extracted and called as common text. Optionally, when the repeated code text is at the beginning (such as the first M lines of the test case) or the end (such as the last N lines of the test case) of the test case, its code attribute is determined to be a logical attribute; when the repeated code text is in the middle part of the test case (such as the Mth line to the Nth line of the test case), its code attribute is determined to be an implementation attribute. The logic of determining the code attributes of the repeated code text based on the position of the repeated code text is simple, but the accuracy may be poor.

[0046] In one embodiment, determining the code attribute of a repeated code text includes: extracting code keywords from the repeated code text, and determining the code attribute based on the code keywords. Generally, the same keywords exist in the same part of the code, and a keyword library of the corresponding part of the code attribute can be pre-set. After extracting the repeated code text, the code keywords of the repeated code text are extracted, and the code keywords are matched with the keywords in the keyword library. If the match is successful, the code attribute of the repeated code text is determined to be a logical attribute. If the match fails, the code text of the repeated code text is determined to be an implementation attribute. Among them, the keywords of the repeated code text can be extracted using an existing keyword extraction method, which will not be repeated here. The accuracy of determining the code attribute of the repeated code text based on the keywords of the repeated code text is relatively high, but the setting of the keyword library is more critical.

[0047] S130: Generate a duplicate code file corresponding to the duplicate code text based on the code attribute, and perform code reconstructing of the code file to be reconstructed according to the duplicate code file.

[0048] In this embodiment, after the code attribute is determined, it is determined whether to generate a duplicate code file as a public file based on the code attribute until all duplicate code texts are determined / generated.

[0049] Optionally, generating a duplicate code file corresponding to the duplicate code text based on the code attribute includes: when the code attribute is a logical attribute, generating a duplicate code file corresponding to the duplicate code text. When the code attribute of the duplicate code text is a logical attribute, it can be called as a public file, so when the code attribute is a logical attribute, a duplicate code file corresponding to the duplicate code text is generated; when the code attribute is an implementation attribute (that is, when the duplicate code text is a specific implementation code of the use case), the duplicate code file is not generated as a public file.

[0050] Among them, generating a repeated code file corresponding to the repeated code text includes: generating a repeated code file containing the repeated code text, and determining the file attributes of the repeated code file corresponding to the repeated code text according to the code attributes of the repeated code text. Specifically, the repeated code text is treated as a separate repeated code file, and the file attributes of the repeated code file corresponding to the repeated code text, such as attribute information such as the file name, are determined according to the code attributes of the repeated code text. Exemplarily, assuming that the code attribute of the repeated code text is the reference part in the logical attribute, a file name representing the reference part is generated as the file name of the repeated code file, so that the call / reference relationship can be adjusted based on the file name during subsequent code combing.

[0051] In one embodiment, code refactoring of a code file to be refactored is performed based on a duplicate code file, including: adjusting the call logic of a non-duplicate code text in the code file to be refactored based on the file attributes of the duplicate code file, and completing the code refactoring of the code file to be refactored. Before the code file to be refactored is refactored, a direct reference method is used in the non-duplicate code text. After the duplicate code file is generated from the duplicate code text, the previous reference method needs to be adjusted to a call file method, that is, adjusting the reference / call logic relationship of the non-duplicate code text with respect to the parameters in the duplicate code text, and completing the code refactoring of the code file to be refactored.

[0052] The embodiment of the present invention obtains the code file to be reconstructed corresponding to the code reconstructing instruction in response to the detected code reconstructing instruction; extracts the repeated code text in the code file to be reconstructed, and determines the code attribute of the repeated code text; generates the repeated code file corresponding to the repeated code text based on the code attribute, and performs code reconstructing of the code file to be reconstructed according to the repeated code file, and performs code reconstructing by automatically extracting the repeated code book in the code file to be reconstructed, thereby realizing fast extraction of repeated and redundant parts in the code file to be reconstructed, avoiding omission of unextracted files, accelerating the code reconstruction speed, shortening the code reconstruction time, and improving the code reconstruction efficiency.

[0053] Embodiment 2

[0054] This embodiment provides a preferred embodiment based on the above scheme. The code refactoring method provided by the embodiment of the present invention is mainly applicable to the unification of the automated code framework, the code structure has obvious characteristics, such as based on selenium+testng or other existing frameworks, and is applied to the long-term code accumulation, too many test cases, and the same code beginning or end is repeated, and it is desired to achieve refactoring and extract unified files.

[0055] Figure 2a is a structural diagram of a code reconstruction system provided by the second embodiment of the present invention. Figure 2a As shown in the figure, the core of the code refactoring system is composed of four modules: refactoring / optimizing code structure, text classifier, code extraction module and automatic code module. The four modules are independent of each other and will not mix the codes together. The calls between services are based on interfaces or services.

[0056] In general, code input mainly includes automation engineering, including several automation methods. Each engineering file for executing automation is independent. As the input of the text classifier, the use case part of the implementation is mainly input as the key input, which is also the main part that needs to be extracted. The text classifier can be implemented by a program to find the same code parts in multiple files, but it is necessary to distinguish whether the repeated parts are code logic parts or implementation parts. Code extraction is based on the repeated code files obtained in the previous step, and extracts them according to the similarity and the location of the code in the file, such as configuration files, common methods, end statements, etc. Different files are formed according to different code attributes. Refactoring / optimizing the code structure The overall code structure is re-planned according to the extracted files. In addition to the extracted parts, the remaining main automation code implementation logic is combed. And the extracted common files are redefined and combed, and finally a relatively neat and complete code structure is formed.

[0057] Figure 2b FIG. 1 is a flow chart of a code refactoring method provided by Embodiment 2 of the present invention. Figure 2b As shown, the code refactoring method includes:

[0058] (1) First, the use case to be reconstructed is taken as input and input into the pre-constructed text identity extractor;

[0059] (2) According to the classification of automation use cases, identify whether extraction is needed;

[0060] The files are compared one by one using a pre-set similarity classification method.

[0061] (3) Extract the same files, perform text recognition based on the characteristics of the files and the locations where they appear, and form a unified file;

[0062] For example, a method for printing box numbers has a test case that includes a reference part, an initialization part, a method implementation part, and a verification part. The reference part, initialization part, and verification part can be extracted and customized into a configuration file, which can be called when writing a test case.

[0063] (4)Refactor the code.

[0064] After extraction, it is necessary to conduct a unified check and redefine and modify the test cases. Without changing the original test cases, reduce the redundant parts of each test case.

[0065] The embodiment of the present invention uses a text similarity algorithm to quickly extract repeated and redundant parts of the automated code, avoids missing unextracted files, speeds up code reconstruction, and shortens reconstruction time; in addition, it is built in a modular form with low coupling and easy maintenance, so if there is a problem in a certain link, it can be quickly located.

[0066] Embodiment 3

[0067] Figure 3 Schematic diagram of the structure of a code reconstruction device provided by the third embodiment of the present invention. The code reconstruction device can be implemented in software and / or hardware, for example, the code reconstruction device can be configured in a computer device. Figure 3 As shown, the device includes a file acquisition module 310, a repeated text extraction module 320 and a code reconstruction module 330, wherein:

[0068] A file acquisition module 310, configured to acquire, in response to a detected code refactoring instruction, a code file to be refactored corresponding to the code refactoring instruction;

[0069] A repeated text extraction module 320 is used to extract repeated code texts in the code file to be refactored and determine the code attributes of the repeated code texts;

[0070] The code refactoring module 330 is used to generate a duplicate code file corresponding to the duplicate code text based on the code attributes, and perform code refactoring of the code file to be refactored according to the duplicate code file.

[0071] The embodiment of the present invention obtains the code file to be reconstructed corresponding to the code reconstructing instruction through the file acquisition module in response to the detected code reconstructing instruction; the repeated text extraction module extracts the repeated code text in the code file to be reconstructed, and determines the code attribute of the repeated code text; the code reconstruction module generates a repeated code file corresponding to the repeated code text based on the code attribute, performs code reconstructing of the code file to be reconstructed according to the repeated code file, and performs code reconstructing by automatically extracting the repeated code book in the code file to be reconstructed, thereby realizing fast extraction of repeated and redundant parts in the code file to be reconstructed, avoiding omission of unextracted files, accelerating the code reconstruction speed, shortening the code reconstruction time, and improving the code reconstruction efficiency.

[0072] Optionally, based on the above solution, the repeated text extraction module 320 is specifically used for:

[0073] The code file to be refactored is input into a pre-built text classifier to obtain the repeated code text extracted by the text classifier.

[0074] Optionally, based on the above solution, the repeated text extraction module 320 is specifically used for:

[0075] The position of the repeated code text in the test case is obtained, and the code attribute is determined based on the position.

[0076] Optionally, based on the above solution, the repeated text extraction module 320 is specifically used for:

[0077] Code keywords are extracted from repeated code texts, and code attributes are determined based on the code keywords.

[0078] Optionally, based on the above solution, the code reconstruction module 330 is specifically used for:

[0079] When the code attribute is a logical attribute, a repeated code file corresponding to the repeated code text is generated.

[0080] Optionally, based on the above solution, the code reconstruction module 330 is specifically used for:

[0081] A repeated code file containing repeated code text is generated, and a file attribute of the repeated code file corresponding to the repeated code text is determined according to the code attribute of the repeated code text.

[0082] Optionally, based on the above solution, the code reconstruction module 330 is specifically used for:

[0083] The calling logic of the non-duplicate code text in the code file to be refactored is adjusted based on the file attribute of the duplicate code file, and the code refactoring of the code file to be refactored is completed.

[0084] The code reconstruction device provided in the embodiment of the present invention can execute the code reconstruction method provided in any embodiment of the present invention, and has the functional modules and beneficial effects corresponding to the execution method.

[0085] Embodiment 4

[0086] Figure 4 It is a structural diagram of a computer device provided in Embodiment 4 of the present invention. Figure 4 It is a structural diagram of a computer device provided in Embodiment 4 of the present invention. Figure 4 A block diagram of an exemplary computer device 412 suitable for use in implementing embodiments of the present invention is shown. Figure 4 The computer device 412 shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.

[0087] like Figure 4 As shown, the computer device 412 is in the form of a general-purpose computing device. The components of the computer device 412 may include, but are not limited to: one or more processors 414, a system memory 428, and a bus 418 that connects different system components (including the system memory 428 and the processor 414).

[0088] Bus 418 represents one or more of several types of bus structures, including a memory bus or memory controller, a peripheral bus, an accelerated graphics port, a local bus to processor 414, or a bus structure using any of a variety of bus architectures. By way of example, these architectures include, but are not limited to, an Industry Standard Architecture (ISA) bus, a Micro Channel Architecture (MAC) bus, an Enhanced ISA bus, a Video Electronics Standards Association (VESA) local bus, and a Peripheral Component Interconnect (PCI) bus.

[0089] The computer device 412 typically includes a variety of computer system readable media. These media can be any available media that can be accessed by the computer device 412, including volatile and non-volatile media, removable and non-removable media.

[0090] System memory 428 may include computer system readable media in the form of volatile memory, such as random access memory (RAM) 430 and / or cache memory 432. Computer device 412 may further include other removable / non-removable, volatile / non-volatile computer system storage media. By way of example only, storage device 434 may be used to read and write non-removable, non-volatile magnetic media ( Figure 4 not shown, usually called a "hard drive"). Although Figure 4Not shown in the figure, a disk drive for reading and writing to a removable non-volatile disk (e.g., a "floppy disk"), and an optical disk drive for reading and writing to a removable non-volatile optical disk (e.g., a CD-ROM, a DVD-ROM, or other optical media) may be provided. In these cases, each drive may be connected to the bus 418 via one or more data medium interfaces. The memory 428 may include at least one program product having a set (e.g., at least one) of program modules that are configured to perform the functions of the various embodiments of the present invention.

[0091] A program / utility 440 having a set (at least one) of program modules 442 may be stored, for example, in memory 428, such program modules 442 including, but not limited to, an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment. Program modules 442 generally perform the functions and / or methods of the embodiments described herein.

[0092] The computer device 412 may also communicate with one or more external devices 414 (e.g., keyboard, pointing device, display 424, etc.), may also communicate with one or more devices that enable a user to interact with the computer device 412, and / or communicate with any device that enables the computer device 412 to communicate with one or more other computing devices (e.g., network card, modem, etc.). Such communication may be performed via input / output (I / O) interface 422. Furthermore, the computer device 412 may also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN) and / or public network, such as the Internet) via network adapter 420. As shown, the network adapter 420 communicates with other modules of the computer device 412 via bus 418. It should be understood that, although not shown in the figure, other hardware and / or software modules may be used in conjunction with the computer device 412, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.

[0093] The processor 414 executes various functional applications and data processing by running the program stored in the system memory 428, for example, implementing the code reconstruction method provided in the embodiment of the present invention, which includes:

[0094] In response to the detected code refactoring instruction, obtaining a to-be-refactored code file corresponding to the code refactoring instruction;

[0095] Extracting repeated code text in the code file to be reconstructed, and determining code attributes of the repeated code text;

[0096] A duplicate code file corresponding to the duplicate code text is generated based on the code attribute, and code reconstruction of the code file to be reconstructed is performed according to the duplicate code file.

[0097] Of course, those skilled in the art can understand that the processor can also implement the technical solution of the code reconstruction method provided by any embodiment of the present invention.

[0098] Embodiment 5

[0099] Embodiment 5 of the present invention further provides a computer-readable storage medium, on which a computer program is stored. When the program is executed by a processor, the code reconstruction method provided in the embodiment of the present invention is implemented. The method includes:

[0100] In response to the detected code refactoring instruction, obtaining a to-be-refactored code file corresponding to the code refactoring instruction;

[0101] Extracting repeated code text in the code file to be reconstructed, and determining code attributes of the repeated code text;

[0102] A duplicate code file corresponding to the duplicate code text is generated based on the code attribute, and code reconstruction of the code file to be reconstructed is performed according to the duplicate code file.

[0103] Of course, the computer program stored on a computer-readable storage medium provided by an embodiment of the present invention is not limited to the above method operations, but can also execute related operations of the code reconstruction method provided by any embodiment of the present invention.

[0104] The computer storage medium of the embodiment of the present invention can adopt any combination of one or more computer-readable media. The computer-readable medium can be a computer-readable signal medium or a computer-readable storage medium. The computer-readable storage medium can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples (non-exhaustive list) of computer-readable storage media include: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this document, a computer-readable storage medium can be any tangible medium containing or storing a program, which can be used by an instruction execution system, device or device or used in combination with it.

[0105] Computer-readable signal media may include data signals propagated in baseband or as part of a carrier wave, which carry computer-readable program code. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than a computer-readable storage medium, which may send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, apparatus, or device.

[0106] Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.

[0107] Computer program code for performing the operations of the present invention may be written in one or more programming languages ​​or a combination thereof, including object-oriented programming languages ​​such as Java, Smalltalk, C++, and conventional procedural programming languages ​​such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., through the Internet using an Internet service provider).

[0108] Note that the above are only preferred embodiments of the present invention and the technical principles used. Those skilled in the art will understand that the present invention is not limited to the specific embodiments herein, and that various obvious changes, readjustments and substitutions can be made by those skilled in the art without departing from the scope of protection of the present invention. Therefore, although the present invention has been described in more detail through the above embodiments, the present invention is not limited to the above embodiments, and may include more other equivalent embodiments without departing from the concept of the present invention, and the scope of the present invention is determined by the scope of the appended claims.

Claims

1. A code refactoring method, characterized in that: include: In response to the detected code refactoring instruction, obtaining a to-be-refactored code file corresponding to the code refactoring instruction; Extracting repeated code text in the code file to be reconstructed, and determining code attributes of the repeated code text; Generate a repeated code file corresponding to the repeated code text based on the code attribute, and perform code reconstructing of the code file to be reconstructed according to the repeated code file; The generating the repeated code file corresponding to the repeated code text based on the code attribute includes: When the code attribute is a logical attribute, generating a repeated code file corresponding to the repeated code text; The generating of the repeated code file corresponding to the repeated code text includes: Generate a repeated code file containing the repeated code text, and determine the file attribute of the repeated code file corresponding to the repeated code text according to the code attribute of the repeated code text; The code refactoring of the code file to be refactored according to the duplicate code file comprises: Adjust the calling logic of the non-duplicate code text in the code file to be reconstructed based on the file attribute of the duplicate code file, and complete the code reconstruction of the code file to be reconstructed; When the repeated code text is at the beginning or the end of the test case, the code attribute of the repeated code text is determined to be a logical attribute.

2. The method according to claim 1, characterized in that The step of extracting repeated code text from the code file to be refactored includes: The code file to be reconstructed is input into a pre-built text classifier to obtain the repeated code text extracted by the text classifier.

3. The method according to claim 1, characterized in that The step of determining the code attribute of the repeated code text comprises: The position of the repeated code text in the test case is obtained, and the code attribute is determined based on the position.

4. The method according to claim 1, characterized in that: The step of determining the code attribute of the repeated code text comprises: Code keywords in the repeated code text are extracted, and the code attributes are determined based on the code keywords.

5. A code reconstruction device, characterized in that: include: A file acquisition module, configured to, in response to a detected code refactoring instruction, acquire a to-be-refactored code file corresponding to the code refactoring instruction; A repeated text extraction module, used to extract repeated code text in the code file to be reconstructed, and determine the code attributes of the repeated code text; A code refactoring module, used to generate a repeated code file corresponding to the repeated code text based on the code attribute, and perform code refactoring of the code file to be refactored according to the repeated code file; The code reconstruction module is specifically used for: When the code attribute is a logical attribute, a repeated code file containing repeated code text is generated, and a file attribute of the repeated code file corresponding to the repeated code text is determined according to the code attribute of the repeated code text; Adjust the calling logic of the non-duplicate code text in the code file to be refactored based on the file attributes of the duplicate code file, and complete the code refactoring of the code file to be refactored; When the repeated code text is at the beginning or the end of the test case, the code attribute of the repeated code text is determined to be a logical attribute.

6. A computer device, characterized in that: The device comprises: one or more processors; A storage device for storing one or more programs; When the one or more programs are executed by the one or more processors, the one or more processors implement the code reconstruction method as described in any one of claims 1 to 4.

7. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the code reconstruction method as described in any one of claims 1 to 4 is implemented.

Citation Information

Patent Citations

  • Software refactoring method through clustering

    CN103559025A

  • Code file optimizing method, system and server

    CN104063208A

  • Method for partitioning and running software codes of controller

    CN104866334A

  • Source code generation and provision device, and source code generation and provision method

    CN106068496A

  • Function reconstruction method for code similarity comparison based on cosine law

    CN107506204A

Cited By

  • Automatic reconstruction kernel code generation method

    CN121387357A