CN111352631B - Interface compatibility detection method and device - Google Patents

Interface compatibility detection method and device Download PDF

Info

Publication number
CN111352631B
CN111352631B CN202010107627.2A CN202010107627A CN111352631B CN 111352631 B CN111352631 B CN 111352631B CN 202010107627 A CN202010107627 A CN 202010107627A CN 111352631 B CN111352631 B CN 111352631B
Authority
CN
China
Prior art keywords
file
class
class file
target
files
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202010107627.2A
Other languages
Chinese (zh)
Other versions
CN111352631A (en
Inventor
冯俊
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Tencent Music Entertainment Technology Shenzhen Co Ltd
Original Assignee
Tencent Music Entertainment Technology Shenzhen Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Tencent Music Entertainment Technology Shenzhen Co Ltd filed Critical Tencent Music Entertainment Technology Shenzhen Co Ltd
Priority to CN202010107627.2A priority Critical patent/CN111352631B/en
Publication of CN111352631A publication Critical patent/CN111352631A/en
Application granted granted Critical
Publication of CN111352631B publication Critical patent/CN111352631B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/43Checking; Contextual analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/43Checking; Contextual analysis
    • G06F8/436Semantic checking
    • G06F8/437Type checking

Abstract

The embodiment of the application discloses an interface compatibility detection method, which comprises the following steps: determining a class file name referenced by each of the one or more target class files; searching a first class file corresponding to the class file name referenced by each target class file in the one or more target class files; if searching the first class file in the one or more target class files fails, recording and feeding back the class file name and a second class file referring to the class file name, wherein the first class file is one or more of the one or more target class files. By implementing the embodiment of the application, the compatibility problem can be exposed in advance in the compiling period for a developer to solve, so that the problem of passively waiting for online abnormality is avoided, and the stability of an application program is improved.

Description

Interface compatibility detection method and device
Technical Field
The present invention relates to the field of computer technologies, and in particular, to a method and an apparatus for detecting interface compatibility.
Background
In the Android development process, the main engineering can call other modules or third party class libraries by referring to binary archive (Android Archive Library, AAR) files of Android library projects so as to expand functions of the main engineering. When the number of AAR files referenced in the main project gradually increases, a plurality of problems may occur in the version number specified by the same AAR. For example, assume that there are two AARs (a file and B file, respectively), where version 1.0.0 of the B file depends on version 1.0.0 of the a file; the B file invokes a method from class M in the a file. Code writers declare in the project that version 1.0.0 of the B file and version 2.0.0 of the A file are referenced. When the project compiling is triggered, the plug-in (such as Gradle) is responsible for analyzing the direct dependence and the transfer dependence of all AARs, and the version number of the A file in the project after analysis comprises 1.0.0 of the A file transferred by the B file and 2.0.0 of the A file appointed in the project.
For the problem that multiple calls exist in the same AAR file version number, the highest resolved version is generally used automatically, for example, version 2.0.0 of the A file and version 1.0.0 of the B file are used for participating in engineering packaging. However, in this scenario, if the foo () method of class M does not exist in version 2.0.0 of the a file, the code in version 1.0.0 of the B file that corresponds to the method is incompatible with the implementation inside the a file, which may cause the application program to flash back. Since the code in the AAR is a class file, the interface incompatibility problem cannot be found in the native compilation process because the compilation error problem cannot occur. If the interface call between AARs has the problem of interface incompatibility, but no problem is found in the compiling process of the Android native process, once the application program runs to the code of the interface incompatibility, the application flash back caused by the error in the running process finally occurs, the stability of the application program is reduced, and the online user experience is affected.
Therefore, how to detect the compatibility of the interface in advance and improve the stability of the application program is a problem to be solved.
Disclosure of Invention
The embodiment of the application provides an interface compatibility detection method and device, which aim to detect the compatibility of an interface in advance and improve the stability of an application program.
In a first aspect, an embodiment of the present application provides an interface compatibility detection method, where the method includes:
determining one or more target class files according to a main project and files referenced by the main project;
determining a class file name referenced by each of the one or more target class files;
searching a first class file corresponding to a class file name referenced by each target class file in the one or more target class files;
if searching the first class file in the one or more target class files fails, recording and feeding back the class file name and a second class file referring to the class file name.
In the embodiment of the application, the main engineering and the library files referenced by the main engineering are compiled to obtain the corresponding class files (i.e. class files). And determining the class file names referenced in each class file, and searching whether corresponding class files exist in a plurality of obtained target class files according to the determined class file names. Traversing all class files, and repeating the searching operation. If searching fails in one or more class files according to the class file names referenced by a certain class file, the class file name referenced by the certain class file is recorded and fed back. By implementing embodiments of the present application, compatibility issues for AAR interface calls are automatically detected and reported during engineering compilation. The compatibility problem can be exposed in advance in the compiling period for a developer to solve, so that the problem of passively waiting for online abnormality is avoided, and the stability of an application program is improved.
In one possible implementation, the file referenced by the main project is a compressed file; the one or more target class files comprise class files corresponding to the main engineering and class files corresponding to the referenced files; the determining one or more target class files according to the main project and the files referenced by the main project comprises the following steps: compiling the main project to obtain a class file corresponding to the main project; downloading the referenced file of the main project, and obtaining the class file corresponding to the referenced file by decompressing the referenced file.
In one possible implementation manner, the determining one or more target class files according to the main project and the files referenced by the main project includes: compiling the main project to obtain a class file corresponding to the main project; downloading the file referenced by the main engineering, wherein the file referenced by the main engineering is a compressed file; obtaining a class file corresponding to the cited file by decompressing the cited file; and determining the class file corresponding to the main project and the class file corresponding to the cited file as the target class file.
In one possible implementation manner, the determining the class file name referenced by each of the one or more target class files includes parsing a file structure of each of the target class files and saving the file structure to a target object corresponding to the target class file, where the file structure includes one or more class file names referenced by the target class file; and determining one or more class file names referenced by each class file according to the class file names in the target object corresponding to each class file.
In one possible implementation, the method further includes: if the searching of the first type file in the one or more target type files is successful, one or more methods referenced in the first type file are obtained; relevant information of each method is determined, wherein the relevant information comprises one or more of a generic file name, contained method information and contained field information.
In one possible implementation manner, if the related information of the method includes the name of the generic file, the interface compatibility detection method further includes: searching a third class file corresponding to the class file name of a certain method in the one or more target class files; if the third class file is failed to be searched, recording and feeding back the name of the class file belonging to the certain method and the third class file belonging to the certain method.
In one possible implementation, if the related information of the method includes: the method information comprises a method name and a method signature, and the interface compatibility detection method further comprises: searching methods corresponding to the method names and the method signatures in a certain method in the one or more target class files; if the corresponding method is failed to be searched, recording and feeding back a first type file to which the certain method belongs, a line referencing an expression of the certain method, a method name of the certain method and a method signature of the certain method.
In one possible implementation, if the related information of the method includes: and the field information comprises a field name and a field descriptor, and the interface compatibility detection method further comprises the following steps: searching fields corresponding to field names and field descriptors in a certain method in the one or more target class files; if the searching of the corresponding field fails, recording and feeding back a first type file to which the certain method belongs, a line of an expression referencing the field information, a field name of the certain method and a field descriptor of the certain method.
In a second aspect, an embodiment of the present application provides an interface compatibility detection apparatus, including:
the first determining unit is used for determining one or more target class files according to the main engineering and files referenced by the main engineering;
a second determining unit, configured to determine a class file name referenced by each of the one or more target class files;
the first searching unit is used for searching a first class file corresponding to a class file name referenced by each target class file in the one or more target class files;
And the feedback unit is used for recording and feeding back the class file name and the second class file referring to the class file name if the first class file fails to be searched in the one or more target class files.
In one possible implementation, the file referenced by the main project is a compressed file; the one or more target class files comprise class files corresponding to the main engineering and class files corresponding to the referenced files;
the first determining unit is specifically configured to: compiling the main project to obtain a class file corresponding to the main project; downloading the referenced file of the main project, and obtaining a class file corresponding to the referenced file by decompressing the referenced file; and determining the class file corresponding to the main project and the class file corresponding to the cited file as the target class file.
In a possible implementation manner, the apparatus further includes a parsing unit, configured to:
analyzing a file structure of each target class file, and storing the file structure to a target object corresponding to the target class file, wherein the file structure comprises one or more class file names referenced by the target class file; and determining one or more class file names referenced by each class file according to the class file names in the target object corresponding to each class file.
In a possible implementation manner, the apparatus further includes a third determining unit, configured to: if the searching of the first type file in the one or more target type files is successful, one or more methods referenced in the first type file are obtained; relevant information of each method is determined, wherein the relevant information comprises one or more of a generic file name, contained method information and contained field information.
In a possible implementation manner, the apparatus further includes a second search unit, configured to:
if the related information of the method comprises the name of the belonging class file, searching a third class file corresponding to the name of the belonging class file of a certain method in the one or more target class files; if the third class file is failed to be searched, recording and feeding back the name of the class file belonging to the certain method and the third class file belonging to the certain method.
In a possible implementation manner, the apparatus further includes a third search unit, configured to:
after the one or more methods referenced in the first type of file are acquired, determining method information contained in each of the one or more methods; and the method information includes a method name and a method signature, the interface compatibility detection method further includes: searching methods corresponding to the method names and the method signatures in a certain method in the one or more target class files; if the corresponding method is failed to be searched, recording and feeding back a first type file to which the certain method belongs, a line referencing an expression of the certain method, a method name of the certain method and a method signature of the certain method.
In a possible implementation manner, the apparatus further includes a fourth search unit, configured to:
after the one or more methods referenced in the first type file are acquired, determining field information referenced by each method, where the related information of the method includes: and the field information comprises a field name and a field descriptor, and the interface compatibility detection method further comprises the following steps: searching fields corresponding to field names and field descriptors in a certain method in the one or more target class files; if the searching of the corresponding field fails, recording and feeding back a first type file to which the certain method belongs, a line of an expression referencing the field information, a field name of the certain method and a field descriptor of the certain method.
In a third aspect, an embodiment of the present application provides a terminal device, including a processor, a communication device, a memory, an input device, and an output device, where the processor, the communication device, the memory, the input device, and the output device are connected to each other, where the memory is configured to store application program code, and the processor is configured to invoke the application program code to perform a method according to any one of the first aspect.
In a fourth aspect, embodiments of the present application provide a computer readable storage medium storing a computer program comprising program instructions which, when executed by a processor, cause the processor to perform the method of any one of the first aspects above.
In a fifth aspect, embodiments of the present application provide a computer program comprising program instructions which, when executed by a processor, cause the processor to perform the method of any of the first aspects above.
Drawings
The drawings that are used as needed in the embodiments of the present application will be described below.
Fig. 1 is a schematic diagram of an interface compatibility detection architecture according to an embodiment of the present application;
fig. 2 is a flow chart of an interface compatibility detection method according to an embodiment of the present application;
fig. 3 is a flow chart of another method for detecting interface compatibility according to an embodiment of the present application;
fig. 4 is a schematic diagram of an application scenario for interface compatibility detection provided in an embodiment of the present application;
fig. 5 is a schematic structural diagram of an interface compatibility detecting device according to an embodiment of the present application;
Fig. 6 is a schematic structural diagram of a terminal device according to an embodiment of the present application.
Detailed Description
In order that those skilled in the art will better understand the present invention, a technical solution in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present application, and it is apparent that the described embodiments are only some embodiments of the present invention, not all embodiments. All other embodiments, which can be made by those skilled in the art based on the embodiments of the present invention without making any inventive effort, shall fall within the scope of the present invention.
The following will describe in detail.
First, some terms in this application are explained for easy understanding by those skilled in the art.
(1) Android (Android), which is an open source code mobile operating system based on a Linux kernel, is one of the currently mainstream mobile phone operating systems; the Android development in the embodiment of the application refers to application development based on an Android system.
(2) The Android archive (Android Archive Library, AAR) is a package form of source codes, resource files and the like in Android, and is a compressed file in a zip format. AAR is a binary archive file of Android library items, and contains all resources such as class and res resource files. A third party library exists in Android. Mainly contains the code (class file) and resources of the third party library.
(3) Maven refers to a dependency management system responsible for version management of AAR. As it is typically built in a remote server, it may also be referred to as a Maven server.
(4) Plug-in technology is a dynamic technology based on an Android system, and is a technology for dynamically loading and executing certain codes (plug-ins) without independently installing other application packages or upgrading the current APK on the basis of installed application packages (Android application package, APK) of users.
(5) Classes (classes), which in object-oriented programming are constructs of an object-oriented computer programming language, are blueprints of created objects, describing properties and methods common to the created objects. A more stringent definition of a class is a cohesive package consisting of some specific metadata. It describes the behavior rules of objects, which are called instances of this class. Classes have interfaces and structures. The interface describes how to interoperate with classes and their instances by means of methods, while the structure describes how data is divided into multiple attributes in one instance. Whereas class files are a set of binary streams in units of 8-bit bytes.
(6) Interface calling refers to that different functional modules have a mutual calling relationship, in Java, each class can externally disclose some elements such as methods and fields, and other classes can call functions of other modules by calling the disclosed elements.
(7) Java files, a series of human-readable computer language instructions written using the Java language by a developer, are typically in the form of a text file with a Java suffix.
The following describes a system architecture for interface compatibility detection provided in the embodiments of the present application. Referring to fig. 1, fig. 1 is a schematic diagram of an interface compatibility detection architecture provided in an embodiment of the present application, including a main engineering file, a detection plug-in (in the embodiment of the present application, a gradle plug-in is taken as an example), and a detection result. The plug-in is applied to the main engineering file, and a program contained in the plug-in can run the method for detecting the interface compatibility provided by the embodiment of the application; interface compatibility issues may be automatically detected and reported during compilation, prompting developers to modify and adjust for erroneous or incompatible code.
It is understood that the illustration of fig. 1 is merely an exemplary implementation in the examples of the present application. The system architecture in the embodiments of the present application may include, but is not limited to, the above system architecture.
The following are embodiments of the method for detecting interface compatibility provided in the present application, which specifically analyze and solve the technical problems set forth in the present application.
Referring to fig. 2, fig. 2 is a schematic diagram of a flow of an interface compatibility detection method according to an embodiment of the present application, and will be described below with reference to fig. 2, which is a terminal for performing compatibility detection on an interface from a single side of a computer (i.e. storing a main project and running a plug-in). The method may comprise the following step S201-step S205, the optional steps comprising step S205.
Step S201: and determining one or more target class files according to the main project and the files referenced by the main project.
Specifically, all AAR files related to the main engineering reference are parsed, and the AAR files are downloaded and saved locally (i.e., the aforementioned terminal, in this embodiment, a computer is taken as an example). Determining paths of all AAR files, and decompressing class files in the paths. Class files obtained after compiling java files of the main engineering and class files obtained by decompressing the AAR files are used as target class files.
In one possible implementation, the file referenced by the main project is a compressed file; the one or more target class files comprise class files corresponding to the main engineering and class files corresponding to the referenced files; the determining one or more target class files according to the main project and the files referenced by the main project comprises the following steps: compiling the main project to obtain a class file corresponding to the main project; downloading the file referenced by the main engineering, wherein the file referenced by the main engineering is a compressed file; obtaining a class file corresponding to the cited file by decompressing the cited file; and determining the class file corresponding to the main project and the class file corresponding to the cited file as the target class file. For example, compiling a main project and a file referenced by the main project to obtain a class file corresponding to the main project; optionally, the referenced file is a compressed file. And taking the compiled plurality of class files as target class files.
Step S202: a class file name referenced by each of the one or more target class files is determined.
Specifically, for each target class file, all the class file names of the references in the target class file are determined, namely the reference relation between each target class file and other class files is defined. And determining the class file name referenced by each target class file in the one or more target class files according to the class file name in the target object corresponding to each class file.
In one possible implementation, the java ssist tool is used to parse the bytecode information of each class file for further analysis purposes. Alternatively, other tools or forms such as ASM may be used to parse the bytecode information of the class file. The embodiment of the application does not limit the way of resolving the byte code information in the class file.
Step S203: and searching a first class file corresponding to the class file name contained in each target class file in the one or more target class files.
Specifically, after the class file names (i.e., the referencing relationship between the target class file and other class files) contained in each target class file are determined; searching a corresponding first class file in one or more class files according to the determined class file names; for example, a class file name contained in one of the target class files is class a; the first class file is found to be the class file named a.
Step S204: if searching the first class file in the one or more target class files fails, recording and feeding back the class file name and a second class file referring to the class file name.
Specifically, if the searching of the first class file corresponding to the class file name fails in the aforementioned step S203, the class file name that cannot be searched and the second class file referencing the class file name are recorded. Wherein the first class file is one or more of the one or more target class files. It can be understood that the failure to find the first class file refers to failure to find the corresponding class file according to the class file name. For example, a class file name contained in one of the target class files is class a; searching a first type file with a type file name a fails; then record class file name a; and, all target class files are searched, and all second class files referring to the first class files (namely, class file name a) are screened and recorded.
Step S205: and if the searching of the corresponding first type file in the one or more target type files is successful, acquiring one or more methods referenced in the first type file.
Specifically, on the premise that searching for the corresponding first-class file is successful, one or more methods contained in the first-class file are determined in the successfully searched first-class file. For example, according to the name of the class file, searching the class file A successfully, and determining the method contained in the class file A; facilitating the subsequent detection of the presence of the method.
Optionally, relevant information for each of the methods is determined. Specifically, under the condition that the existence of the class file is determined, determining a method contained in the class file; and then, obtaining one or more kinds of information such as a method name, a method signature, a field name, a field descriptor and the like in the class to which the method belongs. And further verifying whether corresponding methods and fields exist or not according to the acquired information types and the acquired information quantities, wherein the related information comprises one or more of the file names of the belonging types, the contained method information and the contained field information.
In one possible implementation manner, after the obtaining one or more methods referenced in the first type of file, the method further includes: determining the class file name of each method in the one or more methods, wherein the class file name of each method and the first class file containing the one or more methods may be the same class file or different class files; searching a third class file corresponding to the class file name of a certain method in the one or more target class files; if the third class file is failed to be searched, recording and feeding back the name of the class file belonging to the certain method and the third class file belonging to the certain method.
For example, after the first-type file 1 is determined, the method foo (), which the first-type file 1 contains, is determined. Determining the class name to which the foo () belongs (the class name to which the foo belongs is not limited, i.e. the first class file 1 contains the method foo, but the class file to which the method foo belongs may be the class file 2 or the class file 3, etc.), according to the class name to which the method belongs, attempting to acquire the class to which the method expression refers by a specific search method (such as classpool. Get (classname)), and if the class does not exist, recording the class file containing the foo () and the class name of the called method in a specific list (such as an incompeblemethod). After all records are completed, list information is fed back to the developer, so that the developer can conveniently locate and solve the problem, and find out an interface with incompatible codes.
In one possible implementation manner, after the obtaining one or more methods referenced in the first type of file, the method further includes: searching methods corresponding to the method names and the method signatures in a certain method in the one or more target class files; if the corresponding method is failed to be searched, recording and feeding back a first type file to which the certain method belongs, a line referencing an expression of the certain method, a method name of the certain method and a method signature of the certain method. If the searching of the corresponding method fails, recording and feeding back the first type file, the row containing the expression of the method information, the type file of the corresponding method, the method name of the corresponding method and the method signature of the corresponding method.
It should be noted that, the first type file may refer to a plurality of methods, and each method includes method information; for example, a first type of file a references method B, in which there is a name of method B and a related reference code; in the process of executing the first type file A, a corresponding method B is called through the name of the method B. Then, searching whether the method B exists in the target class file by the name of the method B; if method B is not found, corresponding recording and feedback is performed.
Further optionally, after determining the class name to which the method referred by the first class file belongs, determining the method name and the method signature included in the method; and attempting to acquire the corresponding method according to the method name and the method signature. If the referenced method cannot be found, the class file containing the method, the line where the relevant code (i.e. the expression for calling the method) in the method is located, the name of the method and the signature of the method which fails to find are recorded.
In one possible implementation manner, after the obtaining one or more methods referenced in the first type of file, the method further includes: searching fields corresponding to field names and field descriptors in a certain method in the one or more target class files; if the searching of the corresponding field fails, recording and feeding back a first type file to which the certain method belongs, a line of an expression referencing the field information, a field name of the certain method and a field descriptor of the certain method. For example, in method B, field C is called by its name, where there is associated code or an expression contains the name of field C. Then, searching the related content of the field C in the target class file through the separated name of the field C, and if the related content of the field C is not found, recording the code line number of the first class file where the expression of the field C is located, the name of the first class file, the first class file to which the field belongs and other related information.
Further optionally, on the premise that the called method class exists, checking whether a field related to the method set exists. Based on the field name and the field descriptor, an attempt is made to acquire the corresponding field. If the corresponding field cannot be found, the field name, the field descriptor, the class called by the method, the line where the expression of the calling field is located, and the class file (i.e. the first class file) of the line where the expression is located are recorded.
Referring to fig. 3, fig. 3 is a schematic diagram of another flow chart of an interface compatibility detection method according to an embodiment of the present application, and the description will be made below with reference to fig. 3 from the computer side. The method may comprise the following step S301-step S306, optional steps comprising step S302 and step S306.
Step S301: and determining one or more target class files according to the main project and the files referenced by the main project.
Specifically, please refer to the aforementioned step S201.
Step S302, analyzing the file structure of each target class file, and storing the file structure to a target object corresponding to the target class file.
Specifically, the file structure of each target class file is analyzed through a program, and the file structure is stored in a target object corresponding to the target class file. The file structure includes one or more class file names referenced by the target class file. For example, creating a class pool object (i.e. a target object pool), traversing all class file directories under an all-class directory, analyzing the internal structure of each class file (i.e. a target class file) under the directory by using a java class tool, and storing the analyzed internal structure of each class file into a Ctss object (i.e. a target object); all CtClass objects are then saved to ClassPool.
Step S303: a class file name referenced by each of the one or more target class files is determined.
Specifically, according to the class file names in the target objects corresponding to each class file, determining one or more class file names referenced by each class file. For example, all CtClass objects are obtained from the Classpool and saved to allClassList. Traversing allClassList, taking out one CtClass object at a time, named currentClass. Class names of other class files referenced in all CtClass objects are obtained. For a detailed description, please refer to the aforementioned step S202.
Step S304: and searching a first class file corresponding to the class file name contained in each target class file in the one or more target class files.
Specifically, please refer to the aforementioned step S203.
Step S305: if searching the first class file in the one or more target class files fails, recording and feeding back the class file name and a second class file referring to the class file name.
Specifically, please refer to the aforementioned step S204.
Step S306: and if the searching of the corresponding first type file in the one or more target type files is successful, acquiring one or more methods referenced in the first type file.
Specifically, please refer to the aforementioned step S205.
Optionally, relevant information for each of the methods is determined. Specifically, the related information of each of the methods may include one or more of a generic file name of each method, contained method information, and field information.
In one possible implementation, each line of code in the code is traversed; if the method calls the class, checking the class file to which the method belongs; if the call to the method name and the method signature in the code expression is met, searching whether the corresponding method exists according to the method name and the method signature; if the call to the field name and the field descriptor in the code expression is encountered, searching whether the corresponding field exists according to the field name and the field descriptor.
Next, an application scenario of data protection provided in the present application is described. Referring to fig. 4, fig. 4 is a schematic diagram of an application scenario for interface compatibility detection according to an embodiment of the present application. The architecture of interface compatibility detection may be applied in the scenario shown in fig. 4. As shown in fig. 4, the overall implementation principle of the embodiment of the present application is shown. Embodiments of the present application may be implemented as a Gradle plugin "check-invoke". The developer can automatically detect and report the problem of interface compatibility at the compiling period by only applying the plug-in engineering. The specific implementation flow is as follows:
1. When Gradle starts compiling, analyzing and downloading all AARs, after downloading is completed, collecting local paths of all AAR files, decompressing to obtain class.jar files in the local paths, and continuing decompressing to obtain class files in each AAR file. The resulting class files are stored under an all-class/< AAR-name > directory, where AAR-name is the name of the AAR file. It can be understood that each aar file is stored under the corresponding class file directory.
2. And compiling all java files of the project to obtain a class file output directory. And copying all class files in the directory to an all-class/project-build directory.
3. Creating a class pool object, traversing all class file directories under an all-class directory, analyzing the internal structure of each class file under the directory by using a java class tool, and storing the analyzed internal structure of each class file into a Ctss object; all CtClass objects are then saved to ClassPool.
4. All CtClass objects were obtained from ClassPool in 3 and saved to allrasslist.
5. Traversing allClassList, taking out one CtClass object at a time, named currentClass, and executing the following steps:
5.1 checking if the call of the current class currentClass to the other class is correct:
5.1.1 calling CtClass. GetrEfClasse () method, obtaining other classes referenced by the current class, and storing the other classes in a refClassList list, wherein each item in the list is a class name in the form of a character string;
5.1.2 if the refClassList list is empty, then the current class is not referenced to other classes, no checking is required, and the following 5.1.2.1 steps are skipped; if the refClassList list is not empty, traversing the refClassList list to acquire class names className of the other referenced classes one by one, and executing 5.1.2.1;
5.1.2.1 calls a ClassPool. Get (className) method according to className, tries to obtain the CtClass object analyzed in 3, if the CtClass object obtained is empty, indicating that the referenced class named className does not exist, and adding currentClass and className into an incompeslass list;
5.2 checking whether the method call inside the current class currentClass is correct:
5.2.1 obtaining all methods implemented by the current class through CtClass.getDeclarusedmethods () method, and storing the methods into a list methodList;
5.2.2 if the methodList list is empty, indicating that the current class does not implement a method, there is no call to methods of other classes, no check is required, skip the following step 5.2.2.1; if the methodList list is not empty, traversing the methodList list, scanning each method by using javalist, and executing 5.2.2.1;
5.2.2.1 the entire method body of the current method is scanned using javaist, when an expression (e.g. method call, field access, object creation) is scanned, the following steps are performed:
5.2.2.1.1 obtaining three information of a class name, a method name and a method signature of a method called by the current line expression, and a field name and a field descriptor;
5.2.2.1.2 according to the class name obtained in the previous step, using a class name method to try to obtain the CtClass analyzed in the step 3, if the CtClass obtained is empty, indicating that the class where the method referenced by the current expression is located does not exist, and adding the currentClass and the class name of the step 5.2.2.1.1 into an incompeslass list;
5.2.2.1.3 if the CtClass object obtained in the previous step is not null, it indicates that the class of the called method exists, and it is necessary to continuously check whether the method exists. For this CtClass object, call the getMethod (methodName, methodSign) method, and parameters are the method name and method signature in 5.2.2.1.1, and attempt to obtain the corresponding method. If the return is empty, the method referenced by the current expression is not existed, and five items of information such as the line number where the current expression is located, the class called, the name of the called method, the signature of the method and the like are saved in an incompeblemethod list;
5.2.2.1.4 if the CtClass object obtained in step 5.2.2.1.2 is not null, it indicates that the class of the called method exists, and it is necessary to continuously check whether the called field exists. The getField (fieldDesc) method is called for this CtClass object, with parameters of field name and field descriptor in 5.2.2.1.1, attempting to acquire the corresponding field. If the return is empty, indicating that the field referenced by the current expression does not exist, and storing five items of information such as the line number where the current expression is located, the class called, the field name called, the field descriptor and the like into an incompeblefield list;
6. after the project compiling is finished, if any one of the three lists incompatibleClass, incompatibleMethod, incompatibleField is not empty, indicating that the interface incompatibility condition exists in the current project, throwing out an abnormal prompt developer, outputting information in the three lists, informing the developer of specific incompatible interface information, and facilitating positioning and solving the problem.
It can be appreciated that fig. 3 is only an exemplary implementation manner in the embodiment of the present invention, and the application scenario in the embodiment of the present invention includes, but is not limited to, the above implementation manner.
In order to facilitate better implementation of the foregoing solutions of the present application, the embodiments of the present application correspondingly provide an interface compatibility detection device, please refer to fig. 5, fig. 5 is a schematic structural diagram of an interface compatibility detection device provided by the embodiments of the present application; the following is a detailed description with reference to fig. 5:
as shown in fig. 5, the interface compatibility detecting device 50 may include a first determining unit 501, a second determining unit 502, a first searching unit 503, a feedback unit 504, a parsing unit 505, a third determining unit 506, a second searching unit 507, a third searching unit 508, and a fourth searching unit 509; wherein,
a first determining unit 501, configured to determine one or more target class files according to a main project and files referenced by the main project;
a second determining unit 502, configured to determine a class file name referenced by each of the one or more target class files;
a first searching unit 503, configured to search, in the one or more target class files, a first class file corresponding to a class file name referenced by each of the target class files;
and a feedback unit 504, configured to record and feed back the class file name and a second class file referencing the class file name if searching the first class file in the one or more target class files fails.
Optionally, the first class file is one or more of the one or more target class files
In one possible implementation, the file referenced by the main project is a compressed file; the one or more target class files comprise class files corresponding to the main engineering and class files corresponding to the referenced files; the first determining unit 501 is specifically configured to: compiling the main project to obtain a class file corresponding to the main project; downloading the referenced file of the main project, and obtaining a class file corresponding to the referenced file by decompressing the referenced file; and determining the class file corresponding to the main project and the class file corresponding to the cited file as the target class file.
In a possible implementation manner, the apparatus further includes a parsing unit 505, configured to:
after one or more target class files are determined according to the main project and the files referenced by the main project, analyzing the file structure of each target class file, and storing the file structure to a target object corresponding to the target class file, wherein the file structure comprises one or more class file names referenced by the target class file; and determining one or more class file names referenced by each class file according to the class file names in the target object corresponding to each class file.
In a possible implementation manner, the apparatus further includes a third determining unit 506, configured to: if the searching of the corresponding first type file in the one or more target type files is successful, one or more methods referenced in the first type file are obtained; relevant information of each method is determined, wherein the relevant information comprises one or more of a generic file name, contained method information and contained field information.
In a possible implementation manner, the apparatus further includes a second search unit 507, configured to:
after the one or more methods referenced in the first class file are acquired, if the related information of the method includes the class file name, searching a third class file corresponding to the class file name of a certain method in the one or more target class files; if the third class file is failed to be searched, recording and feeding back the name of the class file belonging to the certain method and the third class file belonging to the certain method.
In a possible implementation manner, the apparatus further includes a third search unit 508, configured to:
after the one or more methods referenced in the first type of file are acquired, determining method information contained in each of the one or more methods; and the method information includes a method name and a method signature, the interface compatibility detection method further includes: searching methods corresponding to the method names and the method signatures in a certain method in the one or more target class files; if the corresponding method is failed to be searched, recording and feeding back a first type file to which the certain method belongs, a line referencing an expression of the certain method, a method name of the certain method and a method signature of the certain method.
In a possible implementation manner, the apparatus further includes a fourth search unit 509, configured to:
after the one or more methods referenced in the first type file are acquired, determining field information referenced by each method, where the related information of the method includes: and the field information comprises a field name and a field descriptor, and the interface compatibility detection method further comprises the following steps: searching fields corresponding to field names and field descriptors in a certain method in the one or more target class files; if the searching of the corresponding field fails, recording and feeding back a first type file to which the certain method belongs, a line of an expression referencing the field information, a field name of the certain method and a field descriptor of the certain method.
It should be noted that, the specific implementation and beneficial effects of each unit in the interface compatibility detecting device 50 shown in fig. 5 may correspond to corresponding descriptions in the method embodiments shown in fig. 2-3, and are not repeated herein.
Referring to fig. 6, fig. 6 is a schematic structural diagram of a terminal device according to an embodiment of the present application; the terminal device 60 comprises a processor 601, a memory 602, a communication interface 603, an input means 606 and an output means 606, said processor 601, memory 602, communication interface 603, input means 606 and output means 606 being interconnected by a bus 604. The terminal device 60 may be a terminal device such as a tablet computer or a personal computer, the input device 606 may be a device such as a keyboard, a mouse, a voice input, a touch panel, and the output device may be a device such as a display.
Memory 602 includes, but is not limited to, random access memory (random access memory, RAM), read-only memory (ROM), erasable programmable read-only memory (erasable programmable read only memory, EPROM), or portable read-only memory (compact disc read-only memory, CD-ROM), and memory 602 is used for storage of related instructions and data. The communication interface 603 is used to receive and transmit data.
The processor 601 may be one or more central processing units (central processing unit, CPU), and in the case where the processor 601 is one CPU, the CPU may be a single-core CPU or a multi-core CPU.
The processor 601 in the terminal device 60 is configured to read the program code stored in the memory 602, and perform the following operations:
the processor 601 is configured to determine one or more target class files according to a main project and files referenced by the main project; determining a class file name referenced by each of the one or more target class files; searching a first class file corresponding to the class file name referenced by each target class file in the one or more target class files; if searching the first class file in the one or more target class files fails, recording and feeding back the class file name and a second class file referring to the class file name, wherein the first class file is one or more of the one or more target class files.
In one possible implementation, the file referenced by the main project is a compressed file; the one or more target class files comprise class files corresponding to the main engineering and class files corresponding to the referenced files; the processor 601 is configured to: compiling the main project to obtain a class file corresponding to the main project; downloading the referenced file of the main project, and obtaining the class file corresponding to the referenced file by decompressing the referenced file.
In one possible implementation, the processor 601 is configured to: analyzing a file structure of each target class file, and storing the file structure to a target object corresponding to each target class file, wherein the file structure comprises one or more class file names referenced by the target class file; and determining one or more class file names referenced by each class file according to the class file names in the target object corresponding to each class file.
In one possible implementation, the processor 601 is configured to: if the searching of the corresponding first type file in the one or more target type files is successful, one or more methods referenced in the first type file are obtained; one or more of the generic filename, contained method information, and field information for each of the one or more methods is determined.
In one possible implementation, the processor 601 is configured to: determining a class file name to which each of the one or more methods belongs; searching a third class file corresponding to the class file name of each method in the one or more target class files; if the searching of the second type file fails, recording and feeding back the name of the file belonging to the type and the first type file.
In one possible implementation, the processor 601 is configured to: determining method information contained by each of the one or more methods; the method information comprises a method name and a method signature; searching methods corresponding to the method names and the method signatures in each method in the one or more target class files; if the searching of the corresponding method fails, recording and feeding back the first type file, the row containing the expression of the method information, the type file of the corresponding method, the method name of the corresponding method and the method signature of the corresponding method.
In one possible implementation, the processor 601 is configured to: determining field information referenced by each method, wherein the field information comprises a field name and a field descriptor; searching corresponding fields in the one or more target class files according to the field names and the field descriptors of each method; if the searching of the corresponding field fails, recording and feeding back the first type file, the line of the expression containing the field information, the class file of the corresponding field, the field name of the corresponding field and the field descriptor of the corresponding field.
In the embodiment of the application, the main engineering and the library files referenced by the main engineering are compiled to obtain the corresponding class files (i.e. class files). And determining the class file names referenced in each class file, and searching whether corresponding class files exist in a plurality of obtained target class files according to the determined class file names. Traversing all class files, and repeating the searching operation. If searching fails in one or more class files according to the class file names referenced by a certain class file, the class file name referenced by the certain class file is recorded and fed back. By implementing embodiments of the present application, compatibility issues for AAR interface calls are automatically detected and reported during engineering compilation. The compatibility problem can be exposed in advance in the compiling period for a developer to solve, so that the problem of passively waiting for online abnormality is avoided, and the stability of an application program is improved.
It should be noted that the implementation and beneficial effects of the above operations may also correspond to the corresponding descriptions of the method embodiments shown in fig. 2-3.
Embodiments of the present application also provide a computer readable storage medium storing a computer program comprising program instructions that, when executed by a processor, implement the method flows shown in fig. 2-4.
Embodiments of the present application also provide a computer program comprising program instructions which, when executed by a processor, implement the method flows shown in fig. 2-3.
In the several embodiments provided by the present invention, it should be understood that the disclosed apparatus, device and method may be implemented in other manners. For example, the apparatus embodiments described above are merely illustrative, e.g., the division of the units is merely a logical function division, and there may be additional divisions when actually implemented, e.g., multiple units or components may be combined or integrated into another system, or some features may be omitted or not performed.
Finally, it should be noted that: the above embodiments are only for illustrating the technical solution of the present invention, and not for limiting the same; although the invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical scheme described in the foregoing embodiments can be modified or some or all of the technical features thereof can be replaced by equivalents; such modifications and substitutions do not depart from the spirit of the invention.

Claims (7)

1. An interface compatibility detection method, which is applied to a detection plug-in a main project, wherein the detection plug-in runs during compiling of the main project, and the method comprises the following steps:
determining one or more target class files according to a main project and files referenced by the main project, wherein the determining comprises the following steps: compiling the main project to obtain a class file corresponding to the main project; downloading the file referenced by the main engineering, wherein the file referenced by the main engineering is a compressed file; obtaining a class file corresponding to the cited file by decompressing the cited file; determining a class file corresponding to the main project and a class file corresponding to the cited file as the target class file;
determining a class file name referenced by each of the one or more target class files includes: analyzing a file structure of each target class file, and storing the file structure to a target object corresponding to the target class file, wherein the file structure comprises one or more class file names referenced by the target class file; determining one or more class file names referenced by each target class file according to the class file names in the target object corresponding to each target class file;
Searching a first class file corresponding to a class file name contained in each target class file in the one or more target class files;
if searching the first class file in the one or more target class files fails, recording and feeding back the class file name and all second class files referencing the class file name in the one or more target class files.
2. The interface compatibility detection method of claim 1, further comprising:
if the searching of the first type file in the one or more target type files is successful, one or more methods referenced in the first type file are obtained;
relevant information for each of the one or more methods is determined, the relevant information including one or more of a generic file name, contained method information, and contained field information.
3. The interface compatibility detecting method according to claim 2, wherein if the related information of the method includes the generic file name, the interface compatibility detecting method further includes:
searching a third class file corresponding to the class file name of a certain method in the one or more methods in the one or more target class files;
If the searching of the third class file fails, recording and feeding back the class file name of a certain method in the one or more methods and the third class file of the certain method in the one or more methods.
4. The method for detecting interface compatibility according to claim 2, wherein if the related information of the method includes: the method information comprises a method name and a method signature, and the interface compatibility detection method further comprises:
searching for a method corresponding to a method name and a method signature in a certain method in the one or more methods in the one or more target class files;
if the searching of the corresponding method fails, recording and feeding back a first type file to which a certain method of the one or more methods belongs, a line referencing an expression of the certain method of the one or more methods, a method name of the certain method of the one or more methods and a method signature of the certain method of the one or more methods.
5. The method for detecting interface compatibility according to claim 2, wherein if the related information of the method includes: and the field information comprises a field name and a field descriptor, and the interface compatibility detection method further comprises the following steps:
Searching fields corresponding to field names and field descriptors in a certain method in the one or more methods in the one or more target class files;
if the searching of the corresponding field fails, recording and feeding back a first type file to which a certain method of the one or more methods belongs, a line of an expression referencing the field information, a field name of the certain method of the one or more methods and a field descriptor of the certain method of the one or more methods.
6. An interface compatibility detection apparatus, characterized by a detection plug-in applied to a main project, the detection plug-in running during compilation of the main project, the apparatus comprising:
a first determining unit, configured to determine one or more target class files according to a main project and files referenced by the main project, where the determining unit includes: compiling the main project to obtain a class file corresponding to the main project; downloading the file referenced by the main engineering, wherein the file referenced by the main engineering is a compressed file; obtaining a class file corresponding to the cited file by decompressing the cited file; determining a class file corresponding to the main project and a class file corresponding to the cited file as the target class file;
A second determining unit, configured to determine a class file name referenced by each of the one or more target class files, including: analyzing a file structure of each target class file, and storing the file structure to a target object corresponding to the target class file, wherein the file structure comprises one or more class file names referenced by the target class file; determining one or more class file names referenced by each target class file according to the class file names in the target object corresponding to each target class file;
the searching unit is used for searching a first class file corresponding to the class file name referenced by each target class file in the one or more target class files;
and the recording and feedback unit is used for recording and feeding back the class file name and all second class files referring to the class file name in the one or more target class files if the first class file fails to be searched in the one or more target class files.
7. A computer readable storage medium, characterized in that the computer readable storage medium stores a computer program which, when executed by a processor, implements the method according to any of claims 1-5.
CN202010107627.2A 2020-02-21 2020-02-21 Interface compatibility detection method and device Active CN111352631B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010107627.2A CN111352631B (en) 2020-02-21 2020-02-21 Interface compatibility detection method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010107627.2A CN111352631B (en) 2020-02-21 2020-02-21 Interface compatibility detection method and device

Publications (2)

Publication Number Publication Date
CN111352631A CN111352631A (en) 2020-06-30
CN111352631B true CN111352631B (en) 2024-03-19

Family

ID=71195763

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010107627.2A Active CN111352631B (en) 2020-02-21 2020-02-21 Interface compatibility detection method and device

Country Status (1)

Country Link
CN (1) CN111352631B (en)

Families Citing this family (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112540788A (en) * 2020-12-03 2021-03-23 南方电网数字电网研究院有限公司 Method compatible with multi-manufacturer unmanned aerial vehicle flight control application apps
CN113360183A (en) * 2021-06-03 2021-09-07 湖南快乐阳光互动娱乐传媒有限公司 Method and related device for ensuring downward compatibility of JAVA external service

Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7765194B1 (en) * 2006-03-17 2010-07-27 Cisco Technology, Inc. Detection and enforcement of version compatibility in network devices
CN103440337A (en) * 2013-09-09 2013-12-11 百度在线网络技术(北京)有限公司 API (application program interface) compatibility scanning method and API compatibility scanning device
CN105630463A (en) * 2014-10-28 2016-06-01 阿里巴巴集团控股有限公司 Method and device for detecting JAR packet collision
CN106775842A (en) * 2016-11-30 2017-05-31 北京酷我科技有限公司 A kind of method of the dex subpackages that can customize
CN107577483A (en) * 2017-08-31 2018-01-12 武汉斗鱼网络科技有限公司 Component engineering calls method, storage medium, equipment and the system of main works code
CN108897588A (en) * 2018-07-09 2018-11-27 广州市千钧网络科技有限公司 A kind of method for routing and route device for intermodule communication
CN109725905A (en) * 2018-12-26 2019-05-07 苏州思必驰信息科技有限公司 Software architecture and implementation method based on source code collection, plug-in unit and application method for compiling pins component

Patent Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7765194B1 (en) * 2006-03-17 2010-07-27 Cisco Technology, Inc. Detection and enforcement of version compatibility in network devices
CN103440337A (en) * 2013-09-09 2013-12-11 百度在线网络技术(北京)有限公司 API (application program interface) compatibility scanning method and API compatibility scanning device
CN105630463A (en) * 2014-10-28 2016-06-01 阿里巴巴集团控股有限公司 Method and device for detecting JAR packet collision
CN106775842A (en) * 2016-11-30 2017-05-31 北京酷我科技有限公司 A kind of method of the dex subpackages that can customize
CN107577483A (en) * 2017-08-31 2018-01-12 武汉斗鱼网络科技有限公司 Component engineering calls method, storage medium, equipment and the system of main works code
CN108897588A (en) * 2018-07-09 2018-11-27 广州市千钧网络科技有限公司 A kind of method for routing and route device for intermodule communication
CN109725905A (en) * 2018-12-26 2019-05-07 苏州思必驰信息科技有限公司 Software architecture and implementation method based on source code collection, plug-in unit and application method for compiling pins component

Also Published As

Publication number Publication date
CN111352631A (en) 2020-06-30

Similar Documents

Publication Publication Date Title
WO2021098148A1 (en) Software compiling method and apparatus, and electronic device and storage medium
US8914780B2 (en) Incremental generation of managed assemblies
Dietrich et al. Broken promises: An empirical study into evolution problems in java programs caused by library upgrades
US9104797B1 (en) Efficient cloud-based annotation of crash reports
CN109032631B (en) Application program patch package obtaining method and device, computer equipment and storage medium
US8516505B2 (en) Cross-platform compatibility framework for computer applications
US20150363195A1 (en) Software package management
CN112099880B (en) Method and system for reducing application program driven by scene
CN111352631B (en) Interface compatibility detection method and device
US9658845B2 (en) Generating a where-used objects list for updating data
CN112769706A (en) Componentized routing method and system
CN113835713B (en) Source code packet downloading method, device, computer equipment and storage medium
CN113515303B (en) Project transformation method, device and equipment
CN112445706A (en) Program abnormal code acquisition method and device, electronic equipment and storage medium
CN110334031B (en) Memory allocation code detection method and device, computer equipment and storage medium
CN116414774A (en) File migration method, device, equipment and storage medium
CN111258802A (en) Method for capturing application program crash information and related equipment
CN116578282A (en) Code generation method, device, electronic equipment and medium
CN113536316B (en) Method and device for detecting component dependency information
CN112328241B (en) Method and device for creating Android library module dependency relationship in application program development
CN114706586A (en) Code compiling method, code running method, code compiling device, code running device, computer equipment and storage medium
CN111273940B (en) Method and device for uploading program file to code warehouse
CN111399842A (en) Code compiling method and device
CN115543486B (en) Server-free computing oriented cold start delay optimization method, device and equipment
CN111273944B (en) Method and device for generating program file and uploading program file to code warehouse

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant