A code modification method and device

By generating inherited code and modifying the object implementation code in the project code, the problem that the code modification function in the existing technology does not meet expectations is solved, and the accuracy of code development and the security of the source code library are improved.

CN114942751BActive Publication Date: 2025-05-13CSC FINANCIAL CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202210648590.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-06-09
Publication Date
2025-05-13
Estimated Expiration
2042-06-09

AI Technical Summary

Technical Problem

After modifying the code in the source code library, the functions of multiple project objects do not meet expectations, resulting in low accuracy in code development.

Method used

By determining the object to be modified in the project code and the object that has been changed in the source code library, the object call chain is obtained, the inheritance code is generated, and the implementation code of the first object is modified according to the inheritance code, and at the same time, the implementation code of the second object is restored to the state before the change.

Benefits of technology

Improve the accuracy of code development, ensure that the modified first object can realize the expected functions of the project, and keep the original functions of the source code library unchanged.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114942751B_ABST
    Figure CN114942751B_ABST
Patent Text Reader

Abstract

The embodiment of the present invention provides a code modification method and device, which relates to the field of software development technology. The method includes: determining a first object to be modified in the project code and a second object whose code has been modified in the source code library; obtaining an object call chain in which the first object calls the second object; generating inheritance code including implementation codes of objects at all levels in the object call chain according to the calling sequence of objects in the object call chain; modifying the implementation code of the first object according to the generated inheritance code, and restoring the implementation code of the second object to the implementation code before the modification after the modification is completed. Applying the scheme provided by the embodiment of the present invention to modify the code can improve the accuracy of code development.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of software development, and in particular to a code modification method and device. Background Art

[0002] The project objects in the project code developed by the software developers can call the source code objects in the source code library so that the project objects can implement the functions provided by the called source code objects. However, the functions provided by the source code objects are usually relatively fixed and difficult to meet the needs of different projects.

[0003] To solve this problem, in the prior art, the implementation code of the source code object in the source code library is usually modified according to the needs of the project, so that the project object can call the source code object with the modified code, so that the project object can achieve the expected function. However, since multiple project objects in the project code can call the same source code object, there may be one or more objects among the multiple project objects whose expected functions are the functions provided by the source code object before the modification. When the code in the source code library is modified, the functions implemented by the one or more objects are not their expected functions, resulting in low accuracy in code development. Summary of the invention

[0004] The purpose of the embodiment of the present invention is to provide a code modification method and device to improve the accuracy of code development. The specific technical solution is as follows:

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

[0006] Determine a first object in the project code to be modified and a second object in the source code library whose code has been modified;

[0007] Obtain an object call chain in which the first object calls the second object;

[0008] Generate inheritance code including implementation codes of objects at each level in the object calling chain according to the calling sequence of objects in the object calling chain;

[0009] The implementation code of the first object is modified according to the generated inheritance code, and after the modification is completed, the implementation code of the second object is restored to the implementation code before the modification.

[0010] In one embodiment of the present invention, obtaining the object call chain in which the first object calls the second object includes:

[0011] Determine, according to the project code, a third object called by the first object, wherein the third object is an object stored in the source code library and provided with an external calling interface;

[0012] Obtaining, according to the code in the source code library, candidate call chains for calling the second object;

[0013] Searching for a target call chain containing the third object in each candidate call chain;

[0014] According to the calling subchain from the third object to the second object in the target calling chain, an object calling chain of the first object calling the second object is obtained.

[0015] In one embodiment of the present invention, obtaining candidate call chains for calling the second object according to the code in the source code library includes:

[0016] For each code file in the code files where each object in the source code library is located, determine the file where the child object called by the object in the code file is located according to the source code library file directory and the file header information of the code file, and determine the child object in the determined file according to the file body information of the code file;

[0017] The calling relationship between each object in the source code library and the child object called by it is obtained, and each candidate calling chain for calling the second object is generated according to the calling relationship.

[0018] In one embodiment of the present invention, generating inheritance code including implementation codes of objects at each level in the object call chain according to the calling order of objects in the object call chain includes:

[0019] The implementation codes of the objects at each level in the object call chain are obtained in the following manner, and the inheritance code including the obtained implementation codes is generated according to the calling sequence of the objects in the object call chain:

[0020] Get the original implementation code of the object;

[0021] If the original implementation code contains a calling code for the derived object, obtaining the implementation code of the derived object;

[0022] The calling code in the original implementation code is replaced with the implementation code of the derived object to obtain the implementation code of the object.

[0023] In one embodiment of the present invention, generating inheritance code including implementation codes of objects at each level in the object call chain according to the calling order of objects in the object call chain includes:

[0024] For each object in the object call chain, determine whether the object is an instance object, if not, obtain the implementation code of the object; if yes, obtain the implementation code of the subclass object generated for the object;

[0025] Generate inheritance code including the obtained implementation code according to the calling sequence of objects in the object calling chain.

[0026] In one embodiment of the present invention, the step of modifying the implementation code of the first object according to the generated inheritance code includes:

[0027] The inheritance code file where the inheritance code is located is stored in the project file where the project code is located, and the code import path recorded in the file header of the file where the first object is located is modified to the storage path of the inheritance code file, so that the inheritance code file is called when the implementation code of the first object is run.

[0028] In one embodiment of the present invention, the determining of the first object to be modified in the project code and the second object in the source code library whose code has been modified includes:

[0029] Scan the project code to obtain an object marked with a first preset annotation in the project code as a first object;

[0030] The implementation code of each object in the source code library is scanned to obtain an object in the source code library marked with a second preset annotation as a second object.

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

[0032] An object determination module, used to determine a first object in the project code to be modified and a second object in the source code library whose code has been modified;

[0033] A call chain obtaining module, used to obtain an object call chain in which the first object calls the second object;

[0034] A code generation module, used to generate inheritance code including implementation codes of objects at each level in the object call chain according to the calling order of objects in the object call chain;

[0035] The code modification module is used to modify the implementation code of the first object according to the generated inheritance code, and after the modification is completed, restore the implementation code of the second object to the implementation code before the modification.

[0036] In one embodiment of the present invention, the call chain obtaining module includes:

[0037] An object determination submodule, configured to determine, according to the project code, a third object called by the first object, wherein the third object is an object stored in the source code library and provided with an external calling interface;

[0038] A first obtaining submodule, configured to obtain, according to the code in the source code library, each candidate call chain for calling the second object;

[0039] A call chain search submodule, used to search for a target call chain containing the third object in each candidate call chain;

[0040] The second obtaining submodule is used to obtain the object calling chain of the first object calling the second object according to the calling subchain from the third object to the second object in the target calling chain.

[0041] In one embodiment of the present invention, the first obtaining submodule is specifically used for:

[0042] For each code file in the code files where each object in the source code library is located, determine the file where the child object called by the object in the code file is located according to the source code library file directory and the file header information of the code file, and determine the child object in the determined file according to the file body information of the code file;

[0043] The calling relationship between each object in the source code library and the child object called by it is obtained, and each candidate calling chain for calling the second object is generated according to the calling relationship.

[0044] In one embodiment of the present invention, the code generation module is specifically used to:

[0045] The implementation codes of the objects at each level in the object call chain are obtained in the following manner, and the inheritance code including the obtained implementation codes is generated according to the calling sequence of the objects in the object call chain:

[0046] Get the original implementation code of the object;

[0047] If the original implementation code contains a calling code for the derived object, obtaining the implementation code of the derived object;

[0048] The calling code in the original implementation code is replaced with the implementation code of the derived object to obtain the implementation code of the object.

[0049] In one embodiment of the present invention, the code generation module is specifically used to:

[0050] For each object in the object call chain, determine whether the object is an instance object, if not, obtain the implementation code of the object; if yes, obtain the implementation code of the subclass object generated for the object;

[0051] Generate inheritance code including the obtained implementation code according to the calling sequence of objects in the object calling chain.

[0052] In one embodiment of the present invention, the code modification module is specifically used to:

[0053] The inheritance code file where the inheritance code is located is stored in the project file where the project code is located, and the code import path recorded in the file header of the file where the first object is located is modified to the storage path of the inheritance code file, so that the inheritance code file is called when the implementation code of the first object is run, and after the modification is completed, the implementation code of the second object is restored to the implementation code before the modification.

[0054] In one embodiment of the present invention, the object determination module is specifically used to:

[0055] Scan the project code to obtain an object marked with a first preset annotation in the project code as a first object;

[0056] The implementation code of each object in the source code library is scanned to obtain an object in the source code library marked with a second preset annotation as a second object.

[0057] In a third aspect, an embodiment of the present invention further provides an electronic device, including a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus;

[0058] Memory, used to store computer programs;

[0059] The processor is used to implement any method step described in the first aspect when executing the program stored in the memory.

[0060] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method steps described in any one of the first aspects are implemented.

[0061] Beneficial effects of the embodiments of the present invention:

[0062] As can be seen from the above, when the solution provided by the embodiment of the present invention is applied to modify the code, after determining the first object and the second object, the object call chain of the first object calling the second object is obtained, and according to the calling order of the objects in the object call chain, an inheritance code including the implementation codes of the objects at each level in the object call chain is generated, so that according to the inheritance code, the implementation code of the first object is modified, so that the modified first object can realize the expected function of the project, and after the modification is completed, the implementation code of the second object is restored to the implementation code before the modification, so as to ensure that the functions implemented by other objects in the project code that call the second object are still the functions provided before the second object is modified. Therefore, applying the solution provided by the embodiment of the present invention to modify the code can improve the accuracy of code development. BRIEF DESCRIPTION OF THE DRAWINGS

[0063] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present invention. For ordinary technicians in this field, other embodiments can also be obtained based on these drawings.

[0064] Figure 1 A schematic diagram of a flow chart of a first code modification method provided by an embodiment of the present invention;

[0065] Figure 2 A schematic diagram of a flow chart of a second code modification method provided by an embodiment of the present invention;

[0066] Figure 3 A schematic diagram of a flow chart of a third code modification method provided in an embodiment of the present invention;

[0067] Figure 4a A schematic diagram of a flow chart of a fourth code modification method provided in an embodiment of the present invention;

[0068] Figure 4b A schematic diagram of the structure of a first code file provided in an embodiment of the present invention;

[0069] Figure 4c A schematic diagram of the structure of a second code file provided by an embodiment of the present invention;

[0070] Figure 4d A schematic diagram of the structure of a candidate call chain provided by an embodiment of the present invention;

[0071] Figure 5 A schematic diagram of a fifth code modification method provided in an embodiment of the present invention;

[0072] Figure 6 A schematic diagram of a sixth code modification method provided by an embodiment of the present invention;

[0073] Figure 7 A schematic diagram of the structure of a first code modification device provided by an embodiment of the present invention;

[0074] Figure 8 A schematic diagram of the structure of a second code modification device provided by an embodiment of the present invention;

[0075] Fig. 9 A schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0076] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field based on the present invention belong to the scope of protection of the present invention.

[0077] The following is a description of the execution subject of the embodiment of the present invention.

[0078] The execution subject of the embodiment of the present invention may be a data processing device such as a server or an electronic computer. In this case, the above data processing device may modify the project code by applying the solution provided by the embodiment of the present invention.

[0079] In addition, a code modification tool may be installed in the above-mentioned data processing device, and the data processing device may implement code modification using the code modification tool.

[0080] The code modification method and device provided by the embodiments of the present invention are described in detail below through specific embodiments.

[0081] See also Figure 1 , Figure 1 This is a flow chart of a first code modification method provided by an embodiment of the present invention. The method includes the following steps S101-S104.

[0082] Step S101: determining a first object in a project code to be modified and a second object in a source code library whose code has been modified.

[0083] The above project code and the code in the source code library may be written in Python, or in Java or other computer programming languages.

[0084] The first object may be understood as a code segment in the project code to be modified, and the second object may be understood as a modified code segment in the source code library.

[0085] For example, in a project code written in Python, the first object can be considered as a method, function, etc. in the project code, and the second object can be considered as a method, function, etc. in a source code library.

[0086] The implementation method for determining the first object and the second object can be found in the subsequent embodiments and will not be described in detail here.

[0087] Step S102: Obtain an object call chain in which a first object calls a second object.

[0088] The above source code library may include multiple objects, and the multiple objects may call each other, wherein an object that calls other objects may be called a parent object, and the called object may be called a child object.

[0089] For example, if object m calls object n, object m can be called the parent object of object n, and object n can be called the child object of object m. In addition, if object k calls object m, object m can be called the first-level parent object of object n, and object k can be called the second-level parent object of object n, and so on. According to the calling relationship between each object, the parent object of object n at a higher level can be found.

[0090] The first object may directly call the second object. In this case, the object call chain only includes the first object and the second object.

[0091] The first object may also indirectly call the second object by calling the parent object of the second object. In this case, the object call chain includes the first object, the second object and the parent object of the second object.

[0092] Specifically, the above object call chain can be obtained through any one of the following two implementation methods.

[0093] In a first implementation manner, each call chain for calling the second object in the source code library may be obtained, and then based on each obtained call chain, an object call chain for the first object to call the second object may be further obtained.

[0094] The specific implementation method of obtaining the above-mentioned object call chain based on each call chain of calling the second object can be found in the subsequent Figure 3 Steps S102A-S102D in the illustrated embodiment are not described in detail here.

[0095] In the second implementation method, the child objects called by the first object can be determined according to the implementation code of the first object. The child objects may be one or more. After the above child objects are determined, the next-level child objects can be determined according to the implementation codes of the determined child objects. The next-level child objects may also be one or more. This recursive method is continuously used to obtain various call chains with the first object as the top-level parent object. After obtaining the multiple call chains, the call chain including the above-mentioned second object can be searched in each call chain to determine that the call chain from the first object to the second object is the above-mentioned object call chain.

[0096] Step S103: Generate inheritance code including implementation codes of objects at each level in the object calling chain according to the calling sequence of objects in the object calling chain.

[0097] Specifically, for each object except the second object in the object call chain, the implementation code of the object contains the calling code for calling the next level child object. When generating the inheritance code, the implementation code of each object in the object call chain can be obtained, and according to the calling order of the objects in the object call chain, the calling code included in the implementation code of each object is replaced with the implementation code of the child object of the object in turn, so as to obtain the replaced inheritance code.

[0098] For example, if the above-mentioned object call chain includes the first object, object a and the second object, and the calling order is the first object calling object a, and object a calling the second object, then when generating the above-mentioned inheritance code, the implementation codes of the first object, object a and the second object can be obtained. For the implementation code of the first object, the calling code of the first object calling object a in the implementation code is replaced with the implementation code of object a, and the replaced code includes the calling code of object a calling the second object. In this way, the implementation code of the second object can be used to perform code replacement on the replaced code again, thereby obtaining the second-replaced code as the inheritance code including the implementation codes of objects at all levels in the object call chain.

[0099] Step S104: modify the implementation code of the first object according to the generated inheritance code, and after the modification is completed, restore the implementation code of the second object to the implementation code before the modification.

[0100] Specifically, after the above inheritance code is generated, the implementation code of the first object may be modified in any one of the following two implementation methods.

[0101] In the first implementation mode, the implementation code of the first object in the project code may be directly replaced with the inheritance code.

[0102] In the second implementation, you can also use the subsequent Figure 6In the illustrated embodiment, steps S104A-S104B modify the implementation code of the first object, which will not be described in detail here.

[0103] After modifying the implementation code of the first object, it is necessary to restore the implementation code of the second object to the implementation code before the modification.

[0104] For example, before modifying the implementation code of the second object, the implementation code of the second object may be backed up, so that when restoring, the implementation code of the second object may be restored to the implementation code before modification based on the backup code.

[0105] As can be seen from the above, when the scheme provided by the embodiment of the present invention is applied to modify the code, after determining the first object and the second object, the object call chain of the first object calling the second object is obtained, and according to the calling order of the objects in the object call chain, an inheritance code including the implementation codes of the objects at each level in the object call chain is generated, so that according to the inheritance code, the implementation code of the first object is modified, so that the modified first object can realize the expected function of the project, and after the modification is completed, the implementation code of the second object is restored to the implementation code before the modification, so as to ensure that the functions implemented by other objects in the project code that call the second object are still the functions provided before the second object is modified. Therefore, the code modification scheme provided by the embodiment of the present invention can improve the accuracy of code development.

[0106] In addition, in the code modification scheme provided by the embodiment of the present invention, there is no need for the user to participate in the processes of obtaining the object call chain, generating inheritance code, modifying the implementation code of the first object, etc. Therefore, the application of the code modification scheme provided by the embodiment of the present invention can reduce the requirements for user participation in code modification and the user's workload, improve the code modification efficiency, and because the implementation code of the second object is restored after the code modification, it can be considered that no pollution is caused to the source code library, thereby ensuring the security of the source code library.

[0107] The following describes an implementation method for determining the first object and the second object.

[0108] In one embodiment of the present invention, see Figure 2 , a flow chart of a second code modification method is provided. In this embodiment, the above step S101 can be implemented by the following steps S101A-S101B.

[0109] Step S101A: Scan the project code to obtain an object marked with a first preset annotation in the project code as a first object.

[0110] Among them, the above-mentioned first preset annotation can be marked by the user according to project requirements.

[0111] The first preset comment may be of various types. For example, the first preset comment may be a #over comment.

[0112] The marking position of the first preset annotation may be a code entry position of the implementation code of the object, a position of a calling code included in the implementation code, or any other position in the implementation code.

[0113] Specifically, by scanning the project code line by line, the code marked with the first preset comment can be determined. After the code is determined, the project code can be reversely scanned starting from the determined code to find the object name of the object where the determined code is located. For example, the object name can be a method name, a function name, etc., thereby determining that the object where the object name is located is the first object.

[0114] Step S101B: Scan the implementation code of each object in the source code library to obtain the object marked with the second preset comment in the source code library as the second object.

[0115] The second preset annotation may be a mark made by the user on the modified objects in the source code library according to project requirements.

[0116] The second preset annotation may also have multiple annotation types. For example, the first preset annotation may be a #changed annotation.

[0117] The marking position of the second preset annotation may be a code entry position of the implementation code of the object, a modified position in the implementation code, or any other position in the implementation code.

[0118] The method of scanning the implementation code of each object in the source code library is similar to the above step S101A, which will not be repeated here.

[0119] As can be seen from the above, when the scheme provided by the embodiment of the present invention is applied to modify the code, since the first preset annotation and the second preset annotation can be marked by the user according to the project requirements, the first object and the second object can be accurately determined by scanning the object marked with the first preset annotation in the project code and the object marked with the second preset annotation in the source code library, so that the inheritance code can be accurately generated based on the first object and the second object, and the implementation code of the first object can be modified. Therefore, the code modification scheme provided by the embodiment of the present invention can improve the accuracy of code modification.

[0120] In addition to determining the first object and the second object by code scanning, an object in the project code manually specified by the user may be determined as the first object, and an object in the source code library manually specified by the user may be determined as the second object.

[0121] The specific implementation method of obtaining the above-mentioned object call chain based on each call chain of calling the second object is described below.

[0122] In one embodiment of the present invention, see Figure 3 , a flow chart of a third code modification method is provided. In this embodiment, the above step S102 can be implemented by the following steps S102A-S102D.

[0123] Step S102A: Determine the third object called by the first object according to the project code.

[0124] The third object is an object stored in the source code library and provided with an external calling interface.

[0125] The third object is a next-level sub-object included in the source code library and called by the first object. For example, the third object may be a top-level calling function in the source code library, or may be another function in the source code library that provides an external calling interface, so that the first object can call it according to the external calling interface provided by it.

[0126] Specifically, the implementation code of the first object includes a calling code for the first object to call the third object. The calling code usually includes specific calling characters and object information of the third object. Therefore, when determining the above-mentioned third object, the implementation code of the first object can be determined in the above-mentioned project code, and then the calling code including the specific calling characters can be searched in the determined implementation code of the first object, so as to determine the third object according to the found calling code.

[0127] Step S102B: Obtain candidate call chains for calling the second object according to the code in the source code library.

[0128] Specifically, each candidate call chain for calling the second object may be obtained by any one of the following two implementations.

[0129] In the first implementation, the following Figure 4a In the illustrated embodiment, steps S102B1 - S102B2 obtain candidate call chains for calling the second object, which will not be described in detail here.

[0130] In the second implementation, since the source code library is obtained in advance, the call chains of all objects in the source code library can be determined in advance, and then after the second object is determined, the candidate call chains for calling the second object are retrieved from the predetermined call chains.

[0131] In addition, if the second object calls other objects in the source code library, then in the predetermined call chain, the second object is not the last level of the call chain. When obtaining the above candidate call chain, the entire call chain including the second object can be obtained as a candidate call chain, and the call chain with the second object as the last level child object can also be obtained as a candidate call chain.

[0132] For example, if there is a call chain: object C-object d-second object-object e, the entire call chain from object c to object e can be obtained as a candidate call chain, and the call chain from object c to the second object can also be obtained as a candidate call chain.

[0133] Step S102C: Search for a target call chain containing the third object in each candidate call chain.

[0134] Specifically, after obtaining each candidate call chain that calls the second object, each object in each candidate call chain can be obtained, and the third object can be searched for in the obtained objects, so that after finding the third object, the target call chain including the third object can be determined.

[0135] Step S102D: Obtain an object call chain in which the first object calls the second object according to the call subchain from the third object to the second object in the target call chain.

[0136] Specifically, since the third object may be any level parent object of the second object, after finding the target call chain including the third object, the calling subchain from the third object to the second object can be determined in the target call chain, and the parent object of the third object, that is, the first object, can be added to the determined calling subchain to obtain the added object call chain.

[0137] For example, if the above target call chain is: object f-third object-object g-second object, it can be determined that the call subchain from the third object to the second object is: third object-object g-second object. After determining the above call subchain, the first object can be added to the call subchain, and the object call chain after addition is: first object-third object-object g-second object.

[0138] It can be seen from the above that when the scheme provided by the embodiment of the present invention is applied to modify the code, when obtaining the object call chain of the first object calling the second object, based on the code in the source code library, each candidate call chain calling the second object can be accurately obtained, so that in each candidate call chain, the target call chain containing the third object can be accurately found, so that according to the call subchain from the third object to the second object in the target call chain, the above-mentioned object call chain can be accurately obtained, and then the inheritance code can be accurately generated and the implementation code of the first object can be modified. Therefore, the code modification scheme provided by the embodiment of the present invention can improve the accuracy of code modification.

[0139] When obtaining each candidate call chain for calling the second object, in addition to the method mentioned in step S102B, a subsequent Figure 4a In the illustrated embodiment, steps S102B1 - S102B2 obtain a candidate call chain.

[0140] In one embodiment of the present invention, see Figure 4a , a flowchart of a fourth code modification method is provided. In this embodiment, the above step S102B can be implemented by the following steps S102B1-S102B2.

[0141] Step S102B1: For each code file in the code files where each object in the source code library is located, determine the file where the child object called by the object in the code file is located based on the source code library file directory and the file header information of the code file, and determine the child object in the determined file based on the file body information of the code file.

[0142] See also Figure 4b , Figure 4b It is a schematic diagram of the structure of a code file. Figure 4b In the figure, file 1 is a file in the source code library. The code in the upper rectangular area of ​​file 1 can be regarded as the file header of file 1, and the code in the lower rectangular area can be regarded as the file body of file 1. The first line of code in the file header of file 1 is from numpy importndenumerate, which means that the sub-object called by file 1 is the numpy file in the source code library. In this way, the numpy file can be found according to the source code library file directory. The first line of code in the file body of file 1 is ndenumerate.mro(), which means that the sub-object called by file 1 is the ".mro()" object in the numpy file.

[0143] See also Figure 4c , Figure 4c This is a structural diagram of another code file. Figure 4cIn the figure, the code in the upper rectangular area of ​​the numpy file can be regarded as the file header of the numpy file, and the code in the lower rectangular area can be regarded as the file body of the numpy file. The first line of code in the file header of the numpy file is from Pandas ImporrtPeriodDtype, indicating that the sub-object called by the numpy file is the pandas file in the source code library. In this way, according to the source code library file directory, the pandas file can be found. The first line of code in the file body of the numpy file is PeriodDtype.freq, indicating that the sub-object called by the numpy file is the ".freq" object in the numpy file.

[0144] Step S102B2: Obtain the calling relationship between each object in the source code library and the child objects it calls, and generate candidate calling chains for calling the second object according to the calling relationship.

[0145] Specifically, by analyzing the file header and file body of the code file where each object in the source code library is located, the child objects called by each object can be determined, thereby establishing a calling relationship between each object and the child objects it calls. After obtaining the calling relationship between each object and the child objects it calls, the first-level parent object that calls the second object can be searched according to the calling relationship. After finding the first-level parent object, the second-level parent object that calls the first-level parent object can be searched again according to the calling relationship. In this way, the recursive search is continuously performed until the top-level parent object that calls the second object is obtained, thereby generating a calling chain from the top-level parent object to the second object as a candidate calling chain for calling the second object.

[0146] See also Figure 4d , Figure 4d is a schematic diagram of the structure of a candidate call chain, from Figure 4d It can be seen that according to the calling relationship between each object in the source code library and the child objects it calls, the first-level parent objects of the second object can be found to be object h and object i. After finding object h and object i, it is also possible to find the first-level parent objects of object h to be object j and object k according to the above calling relationship, and find the first-level parent object of object i to be object l. The found objects j, k and l are the second-level parent objects of the second object. In this way, three call chains can be generated, namely, a call chain from object j to the second object, a call chain from object k to the second object, and a call chain from object l to the second object, as three candidate call chains for calling the second object.

[0147] In the above Figure 4dIn the example, the top-level parent object that calls the second object is the second-level parent object that calls the second object, which can be understood as no other object in the source code library that calls the second-level parent object. If there is a call to the other object, the other object can be searched again to determine that the call chain from the other object to the second object is a candidate call chain.

[0148] As can be seen from the above, when the scheme provided by the embodiment of the present invention is applied to modify the code, the file header of the code file where the object is located usually includes reference information of the file where the sub-object called by the object is located, and the file body usually includes reference information of the sub-object called by the object. Therefore, based on the file header information and file body information of the code file where each object in the source code library is located, the sub-object called by each object can be accurately determined, so as to accurately obtain the calling relationship between each object and the sub-object it calls. According to the obtained calling relationship, each candidate calling chain for calling the second object can be accurately generated, thereby improving the accuracy of code modification.

[0149] When generating inheritance code including implementation codes of objects at each level in an object call chain, an object in the object call chain may call other objects in addition to calling child objects at the next level, and the other objects may be called derived objects.

[0150] For example, in an object call chain, the child object of object p is object q. In addition to object q, object p also calls object r, and object r is a derived object.

[0151] In view of the above situation, in one embodiment of the present invention, when generating the above inheritance code, the implementation code of objects at each level in the object call chain is obtained according to the following steps 1 to 3, and the inheritance code including the obtained implementation code is generated according to the calling order of the objects in the object call chain.

[0152] Step 1: Get the original implementation code of the object.

[0153] Specifically, the implementation code for implementing the object function can be obtained from the above project code as the original implementation code of the object.

[0154] Step 2: If there is a calling code for the derived object in the original implementation code, obtain the implementation code of the derived object.

[0155] Specifically, after obtaining the original implementation code of the object, it can be determined whether there are multiple lines of calling code in the original implementation code. If so, it means that the above-mentioned object not only calls the next-level child object of the object in the object call chain, but also calls other derived objects. At this time, the derived object called by the object can be determined and the implementation code of the derived object can be obtained; if not, it means that the above-mentioned object does not call the derived object.

[0156] Step 3: Replace the calling code in the original implementation code with the implementation code of the derived object to obtain the implementation code of the object.

[0157] As can be seen from the above, when the scheme provided by the embodiment of the present invention is applied to modify the code, when obtaining the implementation code of objects at each level in the object call chain, if the object in the object call chain calls a derived object, the calling code in the original implementation code of the object can be replaced with the implementation code of the derived object. The implementation code of the replaced object includes other codes in the original implementation code except the calling code and the implementation code of the derived object. In this way, it is only necessary to execute the generated inheritance code to realize the expected function of the first object without calling other objects. Since there is no need to consider the object calling situation, the inheritance code is more flexible, so that according to the inheritance code, the convenience of modifying the implementation code of the first object can be improved.

[0158] In one embodiment of the present invention, see Figure 5 , a flowchart of a fifth code modification method is provided. In this embodiment, the above step S103 can be implemented by the following steps S103A-S103D.

[0159] Step S103A: for each object in the object call chain, determine whether the object is an instance object, if not, execute step S103B, if yes, execute step S103C.

[0160] The instance object is a type of object in the project code, and the instance object can be called by the instance in the project code.

[0161] Specifically, it can be determined by judging whether the implementation code of the object complies with the code development rules of the instance object type. If so, it means that the object is an instance object; if not, it means that the object is not an instance object.

[0162] In one embodiment of the present invention, it can be determined whether the object parameters of the object include the parameters required by the instance object type. If so, it means that the object is an instance object; if not, it means that the object is not an instance object.

[0163] In addition, you can also use the code development rules of other instance object types to determine whether an object is an instance object.

[0164] Step S103B: Obtain the implementation code of the object.

[0165] Specifically, the code for realizing the function of the object can be obtained from the above project code as the implementation code of the object.

[0166] Step S103C: Obtain the implementation code of the subclass object generated for the object.

[0167] Specifically, the implementation code of the object may be obtained, and then based on the implementation code of the object, the implementation code of the subclass object of the object may be generated.

[0168] The implementation code for generating subclass objects can be implemented through existing subclass generation technology, which will not be described in detail here.

[0169] Step S103D: Generate inheritance code including the obtained implementation code according to the calling order of objects in the object calling chain.

[0170] This step is similar to the above step S103 and will not be described in detail here.

[0171] As can be seen from the above, when the solution provided by the embodiment of the present invention is applied to modify the code, if the object in the object call chain is an instance object, a subclass object of the object is generated, and the implementation code of the generated subclass object is obtained. Since the subclass object generated for the object can inherit and extend the function of the object, when the inheritance code is generated, the implementation code of the instance object is replaced with the implementation code of the subclass object of the instance object, which can expand the function of the inheritance code, so that after the implementation code of the first object is modified according to the inheritance code, the function of the first object can be expanded.

[0172] When modifying the implementation code of the first object, in addition to the method mentioned in step S104 above, the following method can also be used Figure 6 In the illustrated embodiment, step S104A modifies the implementation code of the first object.

[0173] In one embodiment of the present invention, see Figure 6 , a flow chart of a sixth code modification method is provided. In this embodiment, the above step S104 can be implemented by the following steps S104A-S104B.

[0174] Step S104A: Store the inheritance code file where the inheritance code is located in the project file where the project code is located, and modify the code import path recorded in the file header of the file where the first object is located to the storage path of the inheritance code file, so that the inheritance code file is called when the implementation code of the first object is run.

[0175] Specifically, an inheritance code file including the inheritance code can be generated, and a folder for storing the inheritance code file can be created in the project file where the project code is located. The generated inheritance code file can be placed in the folder, and in the file header of the file where the first object is located, the code import path can be modified to the storage path of the inheritance code file. In this way, when the implementation code of the first object is run, the inheritance code file in the folder can be accessed based on the storage path of the inheritance code file, and the inheritance code in the inheritance code file can be run.

[0176] Step S104B: After the modification is completed, the implementation code of the second object is restored to the implementation code before the modification.

[0177] The method of restoring the implementation code of the second object is the same as that of the above step S104, which will not be repeated here.

[0178] It can be seen from the above that when the solution provided by the embodiment of the present invention is used to modify the code, the inheritance code file where the inheritance code is located is stored in the project file where the project code is located. When the implementation code of the first object is run, the inheritance code file can be called. In this way, there is no need to modify the implementation code of the first object itself, thereby avoiding damage to the code structure of the original project code. In addition, when maintaining the code at a later stage, the maintenance personnel can perform maintenance based on the implementation code of the first code and the inheritance code, which can improve the accuracy of maintenance.

[0179] In addition, after the inheritance code file is stored in the project file, if other objects in the project code need to implement the functions provided by the inheritance code, it can be achieved by establishing a calling relationship between other objects and the objects of the inheritance code without modifying the implementation code of other objects. Therefore, the code modification scheme provided by the embodiment of the present invention can improve the convenience of code modification.

[0180] Corresponding to the above code modification method, an embodiment of the present invention further provides a code modification device.

[0181] In one embodiment of the present invention, see Figure 7 , a schematic diagram of the structure of a first code modification device is provided, the device comprising:

[0182] An object determination module 701 is used to determine a first object in the project code to be modified and a second object in the source code library whose code has been modified;

[0183] A call chain obtaining module 702, configured to obtain an object call chain in which the first object calls the second object;

[0184] A code generation module 703, for generating inheritance code including implementation codes of objects at each level in the object call chain according to the calling sequence of objects in the object call chain;

[0185] The code modification module 704 is used to modify the implementation code of the first object according to the generated inheritance code, and after the modification is completed, restore the implementation code of the second object to the implementation code before the modification.

[0186] In one embodiment of the present invention, see Figure 8, a schematic diagram of the structure of a second code modification device is provided. In this embodiment, the call chain obtaining module 702 includes:

[0187] The object determination submodule 702A is used to determine, according to the project code, a third object called by the first object, wherein the third object is an object stored in the source code library and provided with an external calling interface;

[0188] A first obtaining submodule 702B is used to obtain candidate call chains for calling the second object according to the code in the source code library;

[0189] A call chain search submodule 702C is used to search for a target call chain containing the third object in each candidate call chain;

[0190] The second obtaining submodule 702D is used to obtain the object calling chain of the first object calling the second object according to the calling subchain from the third object to the second object in the target calling chain.

[0191] In one embodiment of the present invention, the first obtaining submodule 702B is specifically used to:

[0192] For each code file in the code files where each object in the source code library is located, determine the file where the child object called by the object in the code file is located according to the source code library file directory and the file header information of the code file, and determine the child object in the determined file according to the file body information of the code file;

[0193] The calling relationship between each object in the source code library and the child object called by it is obtained, and each candidate calling chain for calling the second object is generated according to the calling relationship.

[0194] In one embodiment of the present invention, the code generation module 703 is specifically used for:

[0195] The implementation codes of the objects at each level in the object call chain are obtained in the following manner, and the inheritance code including the obtained implementation codes is generated according to the calling sequence of the objects in the object call chain:

[0196] Get the original implementation code of the object;

[0197] If the original implementation code contains a calling code for the derived object, obtaining the implementation code of the derived object;

[0198] The calling code in the original implementation code is replaced with the implementation code of the derived object to obtain the implementation code of the object.

[0199] In one embodiment of the present invention, the code generation module 703 is specifically used for:

[0200] For each object in the object call chain, determine whether the object is an instance object, if not, obtain the implementation code of the object; if yes, obtain the implementation code of the subclass object generated for the object;

[0201] Generate inheritance code including the obtained implementation code according to the calling sequence of objects in the object calling chain.

[0202] In one embodiment of the present invention, the code modification module 704 is specifically used to:

[0203] The inheritance code file where the inheritance code is located is stored in the project file where the project code is located, and the code import path recorded in the file header of the file where the first object is located is modified to the storage path of the inheritance code file, so that the inheritance code file is called when the implementation code of the first object is run, and after the modification is completed, the implementation code of the second object is restored to the implementation code before the modification.

[0204] In one embodiment of the present invention, the object determination module 701 is specifically configured to:

[0205] Scan the project code to obtain an object marked with a first preset annotation in the project code as a first object;

[0206] The implementation code of each object in the source code library is scanned to obtain an object in the source code library marked with a second preset annotation as a second object.

[0207] The embodiment of the present invention further provides an electronic device, such as Fig. 9 As shown, it includes a processor 901, a communication interface 902, a memory 903 and a communication bus 904, wherein the processor 901, the communication interface 902, and the memory 903 communicate with each other through the communication bus 904.

[0208] Memory 903, used for storing computer programs;

[0209] The processor 901 is used to execute the program stored in the memory 903 to implement the following steps:

[0210] Determine a first object in the project code to be modified and a second object in the source code library whose code has been modified;

[0211] Obtain an object call chain in which the first object calls the second object;

[0212] Generate inheritance code including implementation codes of objects at each level in the object calling chain according to the calling sequence of objects in the object calling chain;

[0213] The implementation code of the first object is modified according to the generated inheritance code, and after the modification is completed, the implementation code of the second object is restored to the implementation code before the modification.

[0214] Other schemes for implementing code modification by the processor 901 executing the program stored in the memory 903 are the same as those mentioned in the aforementioned method embodiment and will not be described in detail here.

[0215] The communication bus mentioned in the above electronic device can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.

[0216] The communication interface is used for communication between the above electronic device and other devices.

[0217] The memory may include a random access memory (RAM) or a non-volatile memory (NVM), such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.

[0218] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.

[0219] In another embodiment of the present invention, a computer-readable storage medium is provided, in which a computer program is stored. When the computer program is executed by a processor, the steps of any of the above code modification methods are implemented.

[0220] In another embodiment of the present invention, a computer program product including instructions is provided. When the computer program product is run on a computer, the computer executes any code modification method in the above embodiments.

[0221] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented by software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present invention is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a website site, computer, server or data center to another website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk Solid State Disk (SSD)), etc.

[0222] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.

[0223] Each embodiment in this specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device, electronic device, computer-readable storage medium, and computer program product embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiments.

[0224] The above description is only a preferred embodiment of the present invention and is not intended to limit the protection scope of the present invention. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention are included in the protection scope of the present invention.

Claims

1. A code modification method, characterized in that: The method comprises: Determine a first object in the project code to be modified and a second object in the source code library whose code has been modified; the second object is used to implement the expected function; Obtain an object call chain in which the first object calls the second object; According to the calling sequence of objects in the object calling chain, an inheritance code including the implementation codes of objects at each level in the object calling chain is generated; the generated inheritance code is obtained by replacing the calling code included in the implementation code of each object with the implementation code of the child object of the object; According to the generated inheritance code, the implementation code of the first object is modified, and after the modification is completed, the implementation code of the second object is restored to the implementation code before the modification; the restored implementation code is used to implement the function of the second object before the modification; the implementation code of the first object is modified according to the generated inheritance code, including: storing the inheritance code file where the inheritance code is located in the project file where the project code is located, and modifying the code import path recorded in the file header of the file where the first object is located to the storage path of the inheritance code file, so that the inheritance code file is called when the implementation code of the first object is run.

2. The method according to claim 1, characterized in that The obtaining of the object call chain of the first object calling the second object comprises: Determine, according to the project code, a third object called by the first object, wherein the third object is an object stored in the source code library and provided with an external calling interface; Obtaining, according to the code in the source code library, candidate call chains for calling the second object; Searching for a target call chain containing the third object in each candidate call chain; According to the calling subchain from the third object to the second object in the target calling chain, an object calling chain of the first object calling the second object is obtained.

3. The method according to claim 2, characterized in that The step of obtaining, according to the code in the source code library, candidate call chains for calling the second object includes: For each code file in the code files where each object in the source code library is located, determine the file where the child object called by the object in the code file is located according to the source code library file directory and the file header information of the code file, and determine the child object in the determined file according to the file body information of the code file; The calling relationship between each object in the source code library and the child object called by it is obtained, and each candidate calling chain for calling the second object is generated according to the calling relationship.

4. The method according to any one of claims 1 to 3, characterized in that The step of generating inheritance code including implementation codes of objects at all levels in the object calling chain according to the calling sequence of objects in the object calling chain comprises: The implementation codes of the objects at each level in the object call chain are obtained in the following manner, and the inheritance code including the obtained implementation codes is generated according to the calling sequence of the objects in the object call chain: Get the original implementation code of the object; If the original implementation code contains a calling code for the derived object, obtaining the implementation code of the derived object; The calling code in the original implementation code is replaced with the implementation code of the derived object to obtain the implementation code of the object.

5. The method according to any one of claims 1 to 3, characterized in that: The step of generating inheritance code including implementation codes of objects at all levels in the object calling chain according to the calling sequence of objects in the object calling chain comprises: For each object in the object call chain, determine whether the object is an instance object, if not, obtain the implementation code of the object; if yes, obtain the implementation code of the subclass object generated for the object; Generate inheritance code including the obtained implementation code according to the calling sequence of objects in the object calling chain.

6. The method according to any one of claims 1 to 3, characterized in that The determining of a first object in the project code to be modified and a second object in the source code library whose code has been modified includes: Scan the project code to obtain an object marked with a first preset annotation in the project code as a first object; The implementation code of each object in the source code library is scanned to obtain an object in the source code library marked with a second preset annotation as a second object.

7. A code modification device, characterized in that: The device comprises: An object determination module, used to determine a first object in the project code to be modified and a second object in the source code library whose code has been modified; the second object is used to implement the expected function; A call chain obtaining module, used to obtain an object call chain in which the first object calls the second object; A code generation module is used to generate inheritance code including implementation codes of objects at each level in the object call chain according to the calling sequence of objects in the object call chain; the generated inheritance code is obtained by replacing the calling code included in the implementation code of each object with the implementation code of the child object of the object; A code modification module is used to modify the implementation code of the first object according to the generated inheritance code, and after the modification is completed, restore the implementation code of the second object to the implementation code before the modification; the restored implementation code is used to implement the function of the second object before the modification; the code modification module is specifically used to store the inheritance code file where the inheritance code is located in the project file where the project code is located, and modify the code import path recorded in the file header of the file where the first object is located to the storage path of the inheritance code file, so that the inheritance code file is called when the implementation code of the first object is run.

8. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, for implementing the method steps described in any one of claims 1 to 6 when executing a program stored in a memory.

9. 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 steps described in any one of claims 1 to 6 are implemented.

Citation Information

Patent Citations

  • Code self-modifying method and device and electronic equipment

    CN109614773A