Disclosure of Invention
The application provides a code package reference method and device, which are used for solving the problem that script codes cannot run normally due to the fact that a code package with a wrong version is referred.
In order to achieve the purpose, the technical scheme is as follows:
in a first aspect, the present application provides a method for referencing a code package, the method comprising:
the computer equipment analyzes the script codes and determines the name of a first code packet referred by the script codes and attribute information of a first method; searching first code packets of M versions according to the names of the first code packets, wherein M is a positive integer; according to the attribute information of the first method, determining a first code packet of a target version from first code packets of M versions, wherein the first code packet of the target version comprises the first method; the script code is instructed to reference a first code package of the target version.
Based on the technical scheme, the computer device can determine the first code packet of the target version from the first code packets of the M versions (the first code packet of the target version comprises the first method), and the problem that the computer device cannot determine which version of the first code packet has the first method and further cannot determine the first code packet of the target version is solved. Moreover, the first code packet of the target version comprises the first method, so that the script codes quote the first code packet of the target version, normal jumping of the script codes to the first method in the first code packet can be realized, and the script codes can run normally.
In one possible design, checking whether a first code packet of an ith version contains a method with attribute information identical to attribute information of the first method, wherein the ith version is any one of M versions, and i is a positive integer less than or equal to M; and if the first code packet of the ith version contains the method with the attribute information being the same as the attribute information of the first method, determining that the first code packet of the ith version is the first code packet of the target version.
In one possible design, the attribute information of the first method includes a name of the first method; if the first code packages of the M versions do not include the method with the attribute information being the same as that of the first method, calculating the name similarity of each method in the N methods and the first method, wherein the name similarity is used for indicating the similarity of the two methods on the name, and the N methods are all the methods included by the first code packages of the M versions; determining a method with the highest name similarity from the N methods; and taking the first code packet containing the method with the highest name similarity as the first code packet of the target version.
In one possible design, the method for referencing the code package further includes: determining the name of a second code packet called by the first method and attribute information of the second method; determining P versions of second code packets according to the names of the second code packets, wherein P is a positive integer; determining a second code packet of a reference version from the second code packets of the P versions according to the attribute information of the second method; the second code package of the referenced version includes the second method; the second code package indicating that the script code references the version.
In a second aspect, the present application provides an apparatus for referencing a code package, the apparatus comprising:
the analysis unit is used for analyzing the script codes and determining the names of the first code packets quoted by the script codes and the attribute information of the first method; the processing unit is used for searching the first code packets of M versions according to the names of the first code packets, wherein M is a positive integer; according to the attribute information of the first method, determining a first code packet of a target version from first code packets of M versions, wherein the first code packet of the target version comprises the first method; and the reference unit is used for indicating the script code to reference the first code package of the target version.
In one possible design, the processing unit is further configured to check whether the first code packet of the ith version includes a method whose attribute information is the same as the attribute information of the first method, where the ith version is any one of M versions, and i is a positive integer less than or equal to M; and if the first code packet of the ith version contains the method with the attribute information being the same as the attribute information of the first method, determining that the first code packet of the ith version is the first code packet of the target version.
In one possible design, the processing unit is further configured to calculate a name similarity between each of the N methods and the first method if none of the M versions of the first code packages includes a method whose attribute information is the same as the attribute information of the first method, where the name similarity is used to indicate a similarity between the two methods on the name, and the N methods are all methods included in the M versions of the first code packages; determining a method with the highest name similarity from the N methods; and taking the first code packet containing the method with the highest name similarity as the first code packet of the target version.
In one possible design, the parsing unit further determines a name of a second code packet called by the first method and attribute information of the second method; the processing unit is also used for determining P versions of second code packets according to the names of the second code packets, wherein P is a positive integer; determining a second code packet of a reference version from the second code packets of the P versions according to the attribute information of the second method; the second code package of the referenced version includes the second method. And the reference unit is also used for indicating the script code to reference the second code package of the reference version.
In a third aspect, the present application provides a computer device comprising: a processor and a communication interface; the communication interface is coupled to a processor for executing a computer program or instructions for implementing the method of referencing a code package as described in the first aspect and any possible implementation form of the first aspect.
In a fourth aspect, the present application provides a computer-readable storage medium having stored therein instructions that, when executed on a computer, cause the computer to perform the method of referencing the code package described in any one of the possible implementations of the first aspect and the first aspect described above.
In a fifth aspect, the present application provides a computer program product comprising instructions which, when run on a computer, cause the computer to perform the method of referencing a code package as described in the first aspect and any one of the possible implementations of the first aspect.
In a sixth aspect, the present application provides a chip comprising a processor and a communication interface, the communication interface being coupled to the processor, the processor being configured to execute a computer program or instructions to implement the method of referencing a code package as described in the first aspect and any one of the possible implementations of the first aspect.
In the above-mentioned solution, for technical problems that can be solved by the code packet referencing device, the computer storage medium, the computer program product, or the chip, and technical effects that can be achieved by the code packet referencing device, the computer storage medium, the computer program product, or the chip, reference may be made to the technical problems and the technical effects that are solved by the first aspect, and details are not described herein again.
Detailed Description
The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are only a part of the embodiments of the present application, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
The character "/" herein generally indicates that the former and latter associated objects are in an "or" relationship. For example, A/B may be understood as A or B.
The terms "first" and "second" in the description and claims of the present application are used for distinguishing between different objects and not for describing a particular order of the objects.
Furthermore, the terms "including" and "having," and any variations thereof, as referred to in the description of the present application, are intended to cover non-exclusive inclusions. For example, a process, method, system, article, or apparatus that comprises a list of steps or modules is not limited to the listed steps or modules but may alternatively include other steps or modules not listed or inherent to such process, method, article, or apparatus.
In addition, in the embodiments of the present application, words such as "exemplary" or "for example" are used to mean serving as examples, illustrations or explanations. Any embodiment or design described herein as "exemplary" or "e.g.," is not necessarily to be construed as preferred or advantageous over other embodiments or designs. Rather, use of the word "exemplary" or "e.g.," is intended to present concepts in a concrete fashion.
The technical scheme provided by the embodiment of the application can be executed by computer equipment. The computer device is a device installed with a programming tool that supports programming using a programming language with a code package sharing platform, a programming language in which a reference declaration is present for a code package, and a programming language in which a reference method is present (e.g., Java language, Python language), for example, intellj IDEA, eclipse. The computer device may store script code written by a developer or automatically generate script code generated by scripting software. The computer device may be a tablet computer, a notebook computer, a desktop computer, a portable computer, and the like, which is not limited in this application.
As shown in fig. 1, a method for referencing a code package provided by an embodiment of the present application includes the following steps:
s101, the computer equipment analyzes the script codes and determines the names of the first code packages and the attribute information of the first method, wherein the first code packages are referred by the script codes.
The script code may be a script code written by a developer, or a script code generated by automatically generating script software, which is not limited in this embodiment of the present application.
The script code includes a reference declaration and attribute information of the first method. The reference declaration includes the name of the code package referenced by the reference word (e.g., import) and the script code. For example, the citation claim may be that: and the import PackageA refers to the import word and refers to the name of the code packet referred by the script code as PackageA. The attribute information of the first method includes: name of the first method, parameter type of the first method, number of parameters of the first method, return value type of the first method.
In one possible implementation, the computer device parses the script code to determine the citation claim and attribute information for the first method. Thereafter, the computer device determines a name of a first code package referenced by the script code based on the reference declaration.
S102, the computer device finds the first code packages of the M versions according to the names of the first code packages.
Wherein M is a positive integer.
According to one possible implementation manner, the computer device reads the environment variable and finds the first code packages of the M versions according to the names of the first code packages. The environment variable includes M versions of a first code package.
It should be noted that, because the methods in the code package may change with the update of the technology or the change of the requirement, the computer device may store the code packages of different versions, so as to achieve the purpose that different requirements can be realized by different versions of one code package.
S103, the computer device determines the first code packet of the target version from the first code packets of the M versions according to the attribute information of the first method.
Wherein the first code package of the target version includes a first method.
S104, the computer device instructs the script code to refer to the first code packet of the target version.
It will be appreciated that since the target version of the first code package includes the first method, the computer device instructs the script code to reference the target version of the first code package, enabling the script code to jump normally into the first method in the first code package.
Based on the technical scheme shown in fig. 1, the computer device can determine the first code packet of the target version from the first code packets of the M versions (the first code packet of the target version includes the first method), and the problem that the computer device cannot determine which version of the first code packet has the first method and cannot determine the first code packet of the target version is solved. Moreover, the first code packet of the target version comprises the first method, so that the script codes quote the first code packet of the target version, normal jumping of the script codes to the first method in the first code packet can be realized, and the script codes can run normally.
As shown in fig. 2, step S103 may be embodied as the following steps:
s201, the computer device checks whether the first code packet of the ith version contains the method of which the attribute information is the same as the attribute information of the first method.
The ith version is any one of M versions, and i is a positive integer less than or equal to M.
Note that the attribute information of the method is the same as the attribute information of the first method, which means that the name, the parameter type, the number of parameters, and the return value type of the method are all the same as the attribute information of the first method.
Illustratively, the attribute information of the first method is: function1(3, 5). The name of the first method is Function1, the parameters of the first method are 3 and 5 of integer type (int), and the return value type of the first method is int.
The computer device parses the attribute information of the method in the version code packet from the first code packet of version 1.0. The method of the first code package of version 1.0 is as follows:
the name of the method in the first code packet of version 1.0 is Function1, the parameter types of the method are all integer types, the number of the parameters of the method is 2, and the type of the returned value is int. Therefore, the first code package of version 1.0 contains the same method as the attribute information of the first method.
S202, if the first code packet of the ith version contains the method of which the attribute information is the same as the attribute information of the first method, the computer equipment determines that the first code packet of the ith version is the first code packet of the target version.
Based on the technical scheme shown in fig. 2, the computer device can determine the first code packet of the target version in different ways by judging whether the first code packet of the ith version contains the method that the attribute information is the same as the attribute information of the first method, so that the problem that the computer device cannot determine which version of the first code packet contains the method called by the script code and cannot determine the first code packet of the target version is solved. In addition, the first code packet of the target version contains the method called by the script code, so that the script code refers to the first code packet of the target version, the method that the script code normally jumps to the first code packet can be realized, and the script code can normally run.
If none of the first code packets of the M versions contains a method whose attribute information is the same as the attribute information of the first method, as shown in fig. 3, step S103 may be further specifically implemented as:
s301, the computer device calculates the name similarity of each method in the N methods and the first method.
The name similarity is used for indicating the similarity of the two methods on the name, and the N methods are all methods included in the first code packet of the M versions.
According to one possible implementation manner, the computer device analyzes N methods from the first code packets of the M versions, and calculates the name similarity of each method in the N methods and the first method.
It should be noted that whether the name components of the two methods are the same, whether the name components of the two methods are arranged in the same order, and the case of the letter all affect the similarity of the names. The following is illustrated by three examples.
Example one, the name of the first method is Function1, the name of the method 2 is Function2, and the computer device calculates the similarity of the names of the method 2 and the method 1 to be 8/9.
Example two, the name of method 1 is Function1, the name of method 2 is F2 action, and the computer device calculates the similarity between the names of method 2 and method 1 to be 1/9.
Example three, method 1 is named Function1, method 2 is named Function2, and the computer device calculates the similarity between method 2 and method 1 as 7/9.
It can be understood that the name similarity needs to consider multiple factors of whether the name components of the two methods are the same or not, so that the computer device can acquire the method with higher name similarity to the first method.
S302, the computer equipment determines the method with the highest name similarity from the N methods.
In one possible implementation, the computer device compares the N name similarities and determines the method with the highest name similarity.
Illustratively, table 1 shows that the first code package (e.g., PackageA) has 2 versions, and that the 2 versions of the first code package contain 4 methods. One row in table 1 is a piece of data representing the method contained by each version of PackageA. The first method is named Function 1.
TABLE 1
The name similarity between the Function method and the Function1 is 8/9, the name similarity between the Function1 and the Function1 is 3/9, the name similarity between the Function1 is 5/9, and the name similarity between the Function aaaa1 and the Function1 is 4/9. 8/9>5/9>4/9> 3/9. That is, the Function method has the highest similarity with the Function1 method.
S303, the computer device takes the first code packet containing the method with the highest name similarity as the first code packet of the target version.
It can be understood that, when the attribute information of the first method is incorrect and the computer device cannot acquire a method with the attribute information being the same as the attribute information of the first method from the first code packets of the M versions, the computer device reduces the search condition and determines the method most similar to the first method according to the name similarity, so that the computer device can determine the first code packet of the target version.
Based on the technical scheme shown in fig. 3, when none of the M versions of the first code packets includes a method whose attribute information is the same as the attribute information of the first method, the computer device reduces the search condition, searches for a method similar to the first method, and determines the method with the highest name similarity (i.e., the method with the highest similarity to the first method), thereby avoiding the problem that the first code packet of the target version cannot be determined if the attribute information of the first method is incorrect.
After the computer device determines the target version of the first code package, the script code in the first code package also references a second code package containing a referenced version of the second method. And the second code packet has a plurality of versions, and if the reference version of the second code packet cannot be determined, the computer equipment still has the risk of referring to the code packet with the wrong version, so that the script code cannot jump normally, and the problem of abnormal running of the script code is caused.
In order to avoid the problem that a script code cannot jump normally due to the reference of a code packet with an incorrect version, as shown in fig. 4, a method for referencing a code packet provided by an embodiment of the present application includes the following steps:
s401, the computer device determines the name of the second code packet called by the first method and attribute information of the second method.
In one possible implementation, the computer device parses the first code packet of the target version, and determines the name of the second code packet called by the first method and the attribute information of the second method.
Wherein the first code package of the target version includes a name of the second code package and attribute information of the second method. The attribute information of the second method includes: the name of the second method, the parameter type of the second method, the number of parameters of the second method, the second, the return value type of the method.
S402, the computer equipment determines P versions of second code packages according to the names of the second code packages.
Wherein P is a positive integer.
In one possible implementation, the computer device reads the environment variable and finds the second code packages of the P versions according to the names of the second code packages. The environment variable includes P versions of the second code package installed by the computer device.
S403, the computer device determines the second code package of the reference version from the second code packages of the P versions according to the attribute information of the second method.
Wherein the second code package of the referenced version includes a second method.
In one possible implementation, the computer device checks whether the kth version of the second code package contains a method whose attribute information is the same as the attribute information of the second method. When the kth version of the second code package contains a method whose attribute information is the same as the attribute information of the second method, the computer device determines the kth version of the second code package as a reference version of the second code package. Wherein the kth version is one of the P versions.
The specific implementation of this method can refer to the descriptions of steps S201-S202, which are not described herein again.
In another possible implementation manner, when none of the second code packages of the P versions contains a method whose attribute information is the same as the attribute information of the second method, the computer device calculates the name similarity between each of the Q methods and the second method. The computer device determines the method with the highest name similarity from the Q methods. The computer device takes the second code package containing the method with the highest name similarity as the second code package of the referenced version. Wherein, the Q methods are all methods included in the second code packet of the P versions.
For specific implementation, reference may be made to the description of steps S301 to S303, which is not described herein again.
S404, the computer device indicates the script code to refer to the second code packet of the reference version.
It will be appreciated that since the second code package of the referenced version includes the second method, the computer device instructs the script code to reference the second code package of the referenced version, which may enable normal jumping of the script code from the first method to the second method in the second code package of the referenced version.
Based on the technical scheme shown in fig. 4, the computer device can determine the second code package of the referenced version (which includes the second method) from the second code packages of the P versions, which solves the problem that the computer device cannot determine which version of the second code package has the second method and cannot determine the second code package of the referenced version. Moreover, the first code packet of the target version comprises the first method, so that the script codes quote the first code packet of the target version, normal jumping of the script codes to the first method in the first code packet can be realized, and the script codes can run normally.
It should be noted that the embodiment shown in fig. 4 is a process for referencing a second code package (a second code package of a referenced version) to a first code package, and is specifically described. However, the scripting code in the second code package may also reference the third method in the third code package, and the computer device cannot determine which version of the third code package exists with the third method. That is, when a code package refers to a code package, there is a problem that the code package of the reference version cannot be determined. Thus, the embodiment shown in FIG. 4 may be referenced when a code package references a code package.
In the embodiment of the present application, the server may be divided into the functional modules or the functional units according to the above method examples, for example, each functional module or functional unit may be divided corresponding to each function, or two or more functions may be integrated into one processing module. The integrated module may be implemented in a form of hardware, or may be implemented in a form of a software functional module or a functional unit. The division of the modules or units in the embodiment of the present application is schematic, and is only a logic function division, and there may be another division manner in actual implementation.
As shown in fig. 5, an embodiment of the present invention provides a device for referencing a code package, including:
the analysis unit 101 is used for analyzing the script codes and determining the names of the first code packets referred by the script codes and the attribute information of the first method;
the processing unit 102 is configured to find the first code packages of M versions according to names of the first code packages, where M is a positive integer; according to the attribute information of the first method, determining a first code packet of a target version from first code packets of M versions, wherein the first code packet of the target version comprises the first method;
and the reference unit 103 is used for indicating the script code to reference the first code package of the target version.
Optionally, the processing unit 102 is further configured to check whether the first code packet of the ith version contains a method whose attribute information is the same as the attribute information of the first method, where the ith version is any one of M versions, and i is a positive integer less than or equal to M; and if the first code packet of the ith version contains the method with the attribute information being the same as the attribute information of the first method, determining that the first code packet of the ith version is the first code packet of the target version.
Optionally, the processing unit 102 is further configured to calculate a name similarity between each of the N methods and the first method if none of the M versions of the first code packages includes a method whose attribute information is the same as the attribute information of the first method, where the name similarity is used to indicate a similarity between the two methods on the name, and the N methods are all methods included in the M versions of the first code packages; determining a method with the highest name similarity from the N methods; and taking the first code packet containing the method with the highest name similarity as the first code packet of the target version.
Optionally, the parsing unit 101 further determines a name of the second code packet called by the first method and attribute information of the second method;
the processing unit 102 is further configured to determine, according to names of second code packets, P versions of the second code packets, where P is a positive integer; determining a second code packet of a reference version from the second code packets of the P versions according to the attribute information of the second method; the second code package of the referenced version includes the second method;
and the reference unit 103 is also used for indicating the script code to reference the second code package of the reference version.
Fig. 6 shows yet another possible structure of the computer device involved in the above-described embodiment. The computer device includes: a processor 201 and a communication interface 202. The processor 201 is used to control and manage the actions of the device, for example, to perform the various steps in the method flows shown in the above-described method embodiments, and/or to perform other processes for the techniques described herein. The communication interface 202 is used to support communication of the computer device with other network entities. The computer device may further comprise a memory 203 and a bus 204, the memory 203 being used for storing program codes and data of the apparatus.
The processor 201 may implement or execute various exemplary logical blocks, units and circuits described in connection with the present disclosure. The processor may be a central processing unit, general purpose processor, digital signal processor, application specific integrated circuit, field programmable gate array or other programmable logic device, transistor logic device, hardware component, or any combination thereof. Which may implement or perform the various illustrative logical blocks, units, and circuits described in connection with the present disclosure. A processor may also be a combination of computing functions, e.g., comprising one or more microprocessors, a DSP and a microprocessor, or the like.
Memory 203 may include volatile memory, such as random access memory; the memory may also include non-volatile memory, such as read-only memory, flash memory, a hard disk, or a solid state disk; the memory may also comprise a combination of memories of the kind described above.
The bus 204 may be an Extended Industry Standard Architecture (EISA) bus or the like. The bus 204 may be divided into an address bus, a data bus, a control bus, and the like. For ease of illustration, only one thick line is shown in FIG. 6, but this is not intended to represent only one bus or type of bus.
Through the above description of the embodiments, it is clear to those skilled in the art that, for convenience and simplicity of description, the foregoing division of the functional modules is merely used as an example, and in practical applications, the above function distribution may be completed by different functional modules according to needs, that is, the internal structure of the device may be divided into different functional modules to complete all or part of the above described functions. For the specific working processes of the system, the apparatus and the unit described above, reference may be made to the corresponding processes in the foregoing method embodiments, and details are not described here again.
Embodiments of the present application provide a computer program product containing instructions that, when run on a computer, cause the computer to perform the method of referencing the code package in the above-described method embodiments.
The present application further provides a computer-readable storage medium, in which instructions are stored, and when the instructions are executed on a computer, the computer is caused to execute a method for referencing a code package in a method flow shown in the above method embodiment.
The computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples (a non-exhaustive list) of the computer readable storage medium would include the following: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a Read-Only Memory (ROM), an Erasable Programmable Read-Only Memory (EPROM), a register, a hard disk, an optical fiber, a portable Compact Disc Read-Only Memory (CD-ROM), an optical storage device, a magnetic storage device, any suitable combination of the above, or any other form of computer readable storage medium known in the art. An exemplary storage medium is coupled to the processor such the processor can read information from, and write information to, the storage medium. Of course, the storage medium may also be integral to the processor. The processor and the storage medium may reside in an Application Specific Integrated Circuit (ASIC). In embodiments of the present application, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device.
Since the computer device, the computer-readable storage medium, and the computer program product in the embodiments of the present invention may be applied to the method described above, for technical effects that can be obtained by the method, reference may also be made to the method embodiments described above, and details of the embodiments of the present invention are not described herein again.
The above is only an embodiment of the present application, but the scope of the present application is not limited thereto, and any changes or substitutions within the technical scope of the present disclosure should be covered by the scope of the present application. Therefore, the protection scope of the present application shall be subject to the protection scope of the claims.