CN111352631A - Interface compatibility detection method and device - Google Patents

Interface compatibility detection method and device Download PDF

Info

Publication number
CN111352631A
CN111352631A CN202010107627.2A CN202010107627A CN111352631A CN 111352631 A CN111352631 A CN 111352631A CN 202010107627 A CN202010107627 A CN 202010107627A CN 111352631 A CN111352631 A CN 111352631A
Authority
CN
China
Prior art keywords
class
file
class file
files
name
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.)
Granted
Application number
CN202010107627.2A
Other languages
Chinese (zh)
Other versions
CN111352631B (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

Images

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

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Computational Linguistics (AREA)
  • Stored Programmes (AREA)

Abstract

The embodiment of the application discloses a method for detecting interface compatibility, 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 a class file name referenced by each target class file in the one or more target class files; if the first class file is found to be failed in the one or more target class files, recording and feeding back the class file name and a second class file which refers 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 the compiling period in advance for a developer to solve, the problem of passively waiting for the occurrence of online abnormity is avoided, and the stability of the application program is improved.

Description

Interface compatibility detection method and device
Technical Field
The invention relates to the technical field of computers, in particular to a method and a device for detecting interface compatibility.
Background
In the Android development process, the main project calls other modules or third-party class libraries by referring to binary Archive (AAR) files of Android Library items so as to expand functions of the main project. When the number of the AAR files referenced in the main project is increased, a problem that a plurality of AAR files are assigned to the same version number may occur. For example, assume that there are two AARs (A and B files, respectively), where the 1.0.0 version of the B file depends on the 1.0.0 version of the A file; the B file calls a method from class M in the A file. The code writer states in the project that the 1.0.0 version of the B file and the 2.0.0 version of the a file are referenced. When the project compilation is triggered, the plug-in (such as Gradle) is responsible for analyzing all direct dependencies and transfer dependencies of the AAR, and the version number of the A file in the project after the analysis comprises an A file 1.0.0 derived from the transfer dependency of the B file and an A file 2.0.0 specified in the project.
The problem of multiple calls for the same AAR file version number is typically automatically used, for example, the 2.0.0 version of the a file and the 1.0.0 version of the B file are used to participate in engineering packaging. However, in this scenario, if there is no foo () method of class M in version 2.0.0 of the a file, the code of version 1.0.0 of the B file corresponding to the method is incompatible with the implementation inside the a file, which may cause the application to flash back. Since the code in the AAR is already a class file, the interface incompatibility problem cannot be discovered in the native compiling process because the compiling error problem cannot occur. If the interface call between the AARs has the problem of interface incompatibility, and no problem is found in the Android native compiling process and the compiling process, once the application program runs to the code at the interface incompatibility position, a running error finally occurs to cause application flash quit, the stability of the application program is reduced, and the on-line user experience is influenced.
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 urgently.
Disclosure of Invention
The embodiment of the application provides a method and a device for detecting interface compatibility, and aims 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;
and if the first class file is searched in the one or more target class files unsuccessfully, recording and feeding back the class file name and a second class file which refers to the class file name.
In the embodiment of the application, the main project and the library file referred by the main project are compiled to obtain a corresponding class file (i.e., class file). And determining the name of the class file quoted in each class file, and searching whether the corresponding class file exists in the obtained multiple target class files according to the determined class file name. And traversing all the class files and repeating the above searching operation. If the searching in one or more class files fails according to the class file name referenced by a certain class file, the certain class file and the class file name referenced by the certain class file are recorded and fed back. By implementing the embodiment of the application, the compatibility problem of the AAR interface call is automatically detected and reported during engineering compilation. The compatibility problem can be exposed in the compiling period in advance for a developer to solve, the problem of passively waiting for the occurrence of online abnormity is avoided, and the stability of the 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 a class file corresponding to the main project and a class file corresponding to the referenced file; the determining one or more target class files according to the main project and the files referenced by the main project comprises: compiling the main project to obtain a class file corresponding to the main project; downloading the files quoted by the main project, and obtaining the class files corresponding to the quoted files by decompressing the quoted files.
In one possible implementation, the determining one or more target class files according to the main project and the file referenced by the main project includes: compiling the main project to obtain a class file corresponding to the main project; downloading the files quoted by the main project, wherein the files quoted by the main project are compressed files; decompressing the cited file to obtain a class file corresponding to 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 a possible implementation manner, the determining the class file name referred 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 the one or more class file names referred by the target class files; and determining one or more class file names referred by each class file according to the class file name in the target object corresponding to each class file.
In one possible implementation, the method further includes: if the first type of file is successfully searched in the one or more target type of files, one or more methods quoted in the first type of file are obtained; and determining related information of each method, wherein the related information comprises one or more of the file name of the belonging class, the contained method information and the contained field information.
In a possible implementation manner, if the related information of the method includes the file name of the class, the method for detecting interface compatibility 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; and if the third type of file is found to be failed, recording and feeding back the name of the class of file to which the certain method belongs and the third type of file to which the certain method belongs.
In a possible implementation manner, if the related information of the method includes: the method information includes a method name and a method signature, and the interface compatibility detection method further includes: searching a method corresponding to a method name and a method signature in a certain method in the one or more target class files; and if the corresponding method is found to be failed, recording and feeding back the first class file to which the certain method belongs, the line where the expression of the certain method is referred, the method name of the certain method and the method signature of the certain method.
In a possible implementation manner, if the related information of the method includes: and the field information includes a field name and a field descriptor, the method further includes: in the one or more target class files, searching fields corresponding to field names and field descriptors in a certain method; if the corresponding field is found out unsuccessfully, recording and feeding back the first class file to which the certain method belongs, the line where the expression for referring to the field information is located, the field name of the certain method and the field descriptor of the certain method.
In a second aspect, an embodiment of the present application provides an interface compatibility detection apparatus, where the apparatus includes:
the first determining unit is used for determining one or more target class files according to a main project and files referenced by the main project;
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 which refers to the class file name if the first class file is searched in the one or more target class files unsuccessfully.
In one possible implementation, the file referenced by the main project is a compressed file; the one or more target class files comprise a class file corresponding to the main project and a class file corresponding to the referenced file;
the first determining unit is specifically configured to: compiling the main project to obtain a class file corresponding to the main project; downloading the files quoted by the main project, and obtaining class files corresponding to the quoted files by decompressing the quoted files; 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 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 referred by the target class file; and determining one or more class file names referred by each class file according to the class file name 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 first type of file is successfully searched in the one or more target type of files, one or more methods quoted in the first type of file are obtained; and determining related information of each method, wherein the related information comprises one or more of the file name of the belonging class, the contained method information and the contained field information.
In a possible implementation manner, the apparatus further includes a second lookup unit, configured to:
if the related information of the method comprises the file name of the class to which the method belongs, searching a third class file corresponding to the file name of the class to which the method belongs from the one or more target class files; and if the third type of file is found to be failed, recording and feeding back the name of the class of file to which the certain method belongs and the third type of file to which the certain method belongs.
In a possible implementation manner, the apparatus further includes a third searching unit, configured to:
after the one or more methods referred in the first type of file are obtained, determining method information contained in each method 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 a method corresponding to a method name and a method signature in a certain method in the one or more target class files; and if the corresponding method is found to be failed, recording and feeding back the first class file to which the certain method belongs, the line where the expression of the certain method is referred, the method name of the certain method and the 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 referred in the first type of file are obtained, determining field information referred by each method, if the relevant information of the method comprises: and the field information includes a field name and a field descriptor, the method further includes: in the one or more target class files, searching fields corresponding to field names and field descriptors in a certain method; if the corresponding field is found out unsuccessfully, recording and feeding back the first class file to which the certain method belongs, the line where the expression for referring to the field information is located, the field name of the certain method and the 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 apparatus, and an output apparatus, where the processor, the communication device, the memory, the input apparatus, and the output apparatus are connected to each other, where the memory is used to store an application program code, and the processor is configured to call the application program code to perform the method according to any one of the first aspect.
In a fourth aspect, the present application provides a computer-readable storage medium storing a computer program, the computer program comprising program instructions that, when executed by a processor, cause the processor to perform the method of any one of the above first aspects.
In a fifth aspect, the present application provides a computer program comprising program instructions, which when executed by a processor, cause the processor to perform the method of any one of the above first aspects.
Drawings
The drawings to be used in the embodiments of the present application will be described below.
Fig. 1 is a schematic diagram of an interface compatibility detection architecture provided in an embodiment of the present application;
fig. 2 is a schematic flowchart of an interface compatibility detection method according to an embodiment of the present application;
fig. 3 is a schematic flowchart of another interface compatibility detection method provided in an embodiment of the present application;
fig. 4 is a schematic view of an application scenario of interface compatibility detection according to an embodiment of the present application;
fig. 5 is a schematic structural diagram of an interface compatibility detection apparatus 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 to make the technical solutions of the present invention better understood by those skilled in the art, 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 invention, 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 invention.
The following are detailed below.
First, some terms in the present application are explained so as to be easily understood 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 mainstream mobile phone operating systems at present; the Android development referred to in the embodiments of the present application refers to application development based on an Android system.
(2) An Android Archive (AAR) refers to a packing form of source codes, resource files and the like in Android, and is a zip-format compressed file. The AAR is a binary archive file of an Android library project, and comprises all resources such as class and res resource files. A form of presence of third party libraries in Android. Mainly containing the code (class file) and resources of the third party library.
(3) Maven, refers to a dependency management system, which is responsible for version management of AARs. It can also be called a Maven server as it is usually built in a remote server.
(4) The plug-in technology is a dynamic technology based on an Android system, and refers to a technology for dynamically loading and executing certain codes (namely plug-ins) without independently installing other application packages or upgrading the current APK on the basis of an Application Package (APK) installed by a user.
(5) Class (class), a construct of an object-oriented computer programming language in object-oriented programming, is a blueprint for creating objects that describes properties and methods common to created objects. A more rigid definition of a class is a cohesive package consisting of some specific metadata. It describes the behavior rules of some objects, which are called instances of the class. Classes have interfaces and structures. The interface describes how to interoperate with classes and their instances through methods, while the structure describes how data is divided into attributes in an instance. And a class file (class file) is a set of binary streams in units of 8-bit bytes.
(6) Interface calling means that different functional modules have a mutual calling relationship, each class can externally disclose some elements such as methods and fields in Java, and other classes can call the functions of other modules by calling the disclosed elements.
(7) The Java file, which is a series of human-readable computer language instructions written using the Java language by developers, is typically in the form of a text file with a Java suffix.
The following describes a system architecture for interface compatibility detection provided in an embodiment of the present application. Referring to fig. 1, fig. 1 is a schematic diagram of an interface compatibility detection architecture provided in the embodiment of the present application, including a main project 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 method comprises the following steps that a plug-in is applied to a main project 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 incorrect or incompatible code.
It is to be understood that the illustration in fig. 1 is merely an exemplary implementation of the embodiments 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 is an embodiment of the interface compatibility detection method provided in the present application, which specifically analyzes and solves the technical problem provided 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, where the slave computer is a single-side terminal (i.e., a terminal storing a master project and running a plug-in to perform compatibility detection on an interface). The method may comprise the following steps S201-S205, with 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 project citation 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). And determining paths of all AAR files, and decompressing the class files in the AAR files. And compiling the java file of the main project to obtain a class file and decompressing the AAR file to obtain a class file which are both 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 a class file corresponding to the main project and a class file corresponding to the referenced file; the determining one or more target class files according to the main project and the files referenced by the main project comprises: compiling the main project to obtain a class file corresponding to the main project; downloading the files quoted by the main project, wherein the files quoted by the main project are compressed files; decompressing the cited file to obtain a class file corresponding to 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, firstly compiling a main project and a file quoted by the main project to obtain a class file corresponding to the main project; optionally, the referenced file is a compressed file. And taking a plurality of class files obtained by compiling as target class files.
Step S202: determining a class file name referenced by each of the one or more target class files.
Specifically, for each target class file, the class file names of the references in the target class file are all determined, that is, the reference relationship between each target class file and other class files is clarified. 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 bytecode information of each class file is parsed using the javasist tool 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 present application does not limit the manner of parsing the bytecode 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 name contained in each target class file (i.e. the reference relationship between the target class file and other class files) is determined; searching a corresponding first class file in one or more class files according to the determined class file name; for example, one of the target class files contains a class file name of class a; it is found that the first class file is a class file with the name a.
Step S204: and if the first class file is searched in the one or more target class files unsuccessfully, recording and feeding back the class file name and a second class file which refers to the class file name.
Specifically, if the search for the first class file corresponding to the class file name in step S203 fails, the class file name that cannot be found and the second class file that refers to 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 means that the corresponding class file cannot be found according to the class file name. For example, one of the target class files contains a class file name of class a; the first type of file with the class file name a fails to be searched; then the class file name a is recorded; and, all the target class files are retrieved, and all the second class files referencing the first class file (i.e., class file name a) are screened and recorded.
Step S205: and if the corresponding first class file is successfully searched in the one or more target class files, acquiring one or more methods quoted in the first class file.
Specifically, on the premise that the corresponding first-class file is searched successfully, one or more methods included in the first-class file are determined in the successfully searched first-class file. For example, according to the name of the class file, the class file A is found successfully, and the method included in the class file A is determined; facilitating subsequent detection of the presence of a method.
Optionally, information related to each of the methods is determined. Specifically, under the condition that the class file exists, determining a method contained in the class file; and then acquiring one or more information in the method name, method signature, field name, field descriptor and other information of the class to which the method belongs. And further verifying whether corresponding methods and fields exist or not according to different types and quantities of the acquired information, wherein the related information comprises one or more of the file name of the belonging class, the contained method information and the contained field information.
In a possible implementation manner, after the obtaining one or more methods referenced in the first type of file, the method further includes: determining a 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; and if the third type of file is found to be failed, recording and feeding back the name of the class of file to which the certain method belongs and the third type of file to which the certain method belongs.
For example, after the first type file 1 is determined, the method foo () contained in the first type file 1 is determined. Determining the class name to which the foo () belongs (the class name to which the foo () belongs is not limited, that is, the first class file 1 contains the method foo, but the class file to which the method foo belongs may be a class file 2 or a class file 3, etc.), according to the class name to which the method belongs, trying to obtain the class to which the method expression refers by a specific search method (for example, classpool. get), and if the class does not exist, recording the class file containing the foo (), the class name of the called method in a specific list (for example, compatible set). And after all records are finished, the list information is fed back to the developer, so that the developer can conveniently locate and solve the problems and find an interface with incompatible codes.
In a possible implementation manner, after the obtaining one or more methods referenced in the first type of file, the method further includes: searching a method corresponding to a method name and a method signature in a certain method in the one or more target class files; and if the corresponding method is found to be failed, recording and feeding back the first class file to which the certain method belongs, the line where the expression of the certain method is referred, the method name of the certain method and the method signature of the certain method. If the corresponding method is found to be failed, recording and feeding back the first class file, the line of the expression containing the method information, the class 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 of 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, where the name and associated reference code of method B exists; and in the process of executing the first type of file A, calling the corresponding method B through the name of the method B. Then, searching whether the method B exists in the target class file through the name of the method B; if method B is not found, corresponding recording and feedback are performed.
Further optionally, after determining a class name to which the method referenced by the first class file belongs, determining a method name and a method signature included in the method; an attempt is made to acquire a corresponding method based on 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 of the calling method) in the method is located, the name of the method and the signature of the method which fails to be found are recorded.
In a possible implementation manner, after the obtaining one or more methods referenced in the first type of file, the method further includes: in the one or more target class files, searching fields corresponding to field names and field descriptors in a certain method; if the corresponding field is found out unsuccessfully, recording and feeding back the first class file to which the certain method belongs, the line where the expression for referring to the field information is located, the field name of the certain method and the field descriptor of the certain method. For example, method B calls field C by its name, and method B has associated code or expression that contains the name of field C. Then, the related content of the field C is searched in the target class file through the analyzed name of the field C, and if the related content of the field C is not found, 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 the like are recorded.
Further optionally, on the premise that the called method class exists, checking whether a field in which the recipe relates exists. And trying to acquire the corresponding field according to the field name and the field descriptor. If the corresponding field cannot be searched, the field name, the field descriptor, the class of the method call, the line of the expression of the call field and the class file (i.e. the first class file) of the line of the expression are recorded.
Referring to fig. 3, fig. 3 is a schematic diagram of another interface compatibility detection method flow provided in the embodiment of the present application, and the following description will be made from a computer side with reference to fig. 3. The method may comprise the following steps S301-S306, optional steps comprising step S302 and 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 foregoing step S201.
Step S302, analyzing the file structure of each target class file, and saving 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, a classlool object (i.e., a target object pool) is created, all class file directories under the all-classes directory are traversed, a javasist tool is used for analyzing the internal structure of each class file (i.e., a target class file) under the directory, and the analyzed internal structure of each class file is stored into a CtClass object (i.e., a target object); all CtClass objects are then saved to ClassPool.
Step S303: determining a class file name referenced by each of the one or more target class files.
Specifically, one or more class file names referred by each class file are determined according to the class file name in the target object corresponding to each class file. For example, all the CtClass objects are obtained from the above ClassPool and saved to allclasslst. Traversing the allClassList, and taking out one CtClass object at a time, and naming the object as currentClass. The class names of other class files referenced in all the CtClass objects are obtained. For a detailed description, refer to step S202.
Step S304: and 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.
Specifically, please refer to step S203.
Step S305: and if the first class file is searched in the one or more target class files unsuccessfully, recording and feeding back the class file name and a second class file which refers to the class file name.
Specifically, please refer to step S204.
Step S306: and if the corresponding first class file is successfully searched in the one or more target class files, acquiring one or more methods quoted in the first class file.
Specifically, please refer to step S205.
Optionally, information related to each of the methods is determined. Specifically, the related information of each of the methods may include one or more of a file name of a class to which each of the methods belongs, included 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 of the method; if the calling of 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; and if the field name and the field descriptor are called in the code expression, 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 view of an application scenario of 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. Fig. 4 shows the overall implementation principle of the embodiment of the present application. The embodiment of the application can be realized as a Gradle plug-in "check-invoke". The developer can achieve the purpose of automatically detecting and reporting the interface compatibility problem in the compiling period only by applying the plug-in the engineering. The specific implementation process is as follows:
1. when the Gradle starts to be compiled, all AARs are analyzed and downloaded, after the downloading is completed, the local paths of all AAR files are collected, class. The obtained class files are stored in an all-classes/< AAR-name > directory, wherein AAR-name is the name of the AAR file. It is understood that each aar file is stored under the corresponding category file directory.
2. And compiling all java files of the project to obtain a class file output directory. All class files under the directory are copied to the all-classes/project-build directory.
3. Creating a ClassPool object, traversing all class file directories under the all-classes directory, analyzing the internal structure of each class file under the directory by using a javasist tool, and storing the analyzed internal structure of each class file into a CtClass object; all CtClass objects are then saved to ClassPool.
4. All CtClass objects are obtained from ClassPool in 3 and stored in an allClassList.
5. Traversing the allClassList, taking out a CtClass object each time, named currentClass, and executing the following steps:
5.1 check if the current class currentClass is correct for calls of other classes:
5.1.1 calling a CtClass.getRefClasse () method to obtain other classes referenced by the current class, and storing the other classes into a refClassList, wherein each item in the list is a class name in a character string form;
5.1.2 if the refClassList list is empty, it indicates that the current class does not refer to other classes, no check is needed, 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, calling a classPool. get (className) method according to the className, trying to obtain the CtClass object analyzed in 3, if the obtained CtClass object is empty, indicating that the cited class with the name of className does not exist, and adding currentClass and className into an incompatible classes list;
5.2 check if the method call inside the current currentClass is correct:
5.2.1 obtaining all methods realized by the current class through a CtClass. getDecleardmethods () method, and storing the methods into a list methodList;
5.2.2 if the methodList list is empty, it indicates that the current class does not implement the method, there is no call for methods of other classes, no check is needed, and the following step 5.2.2.1 is skipped; if the methodList is not empty, traverse the methodList, scan each method using javasissist, execute step 5.2.2.1;
5.2.2.1, using javasissist to scan the whole method body of the current method, when an expression (such as method call, field access, object creation) is scanned, the following steps are executed:
5.2.2.1.1, acquiring three items of information, namely class name, method name and method signature of the method called by the expression of the current line, and field name and field descriptor;
5.2.2.1.2, according to the class name obtained in the previous step, attempting to obtain the Ctclass analyzed in the step 3 by using a ClassPool. get (className) method, if the obtained Ctclass is empty, indicating that the class where the method cited by the current expression is located does not exist, and adding currentClass and the class name in the step 5.2.2.1.1 into an incompatible class list;
5.2.2.1.3, if the CtClass object obtained in the previous step is not empty, it indicates that the called method is in the class, and it is necessary to continuously check whether the method exists. A getMethod (methodName, methodSign) method is called for the CtClass object, with the method name and method signature in parameter 5.2.2.1.1, and an attempt is made to obtain the corresponding method. If the return is null, the method quoted by the current expression does not exist, and the currentClass, the line number of the current expression, the calling class, the calling method name, the method signature and other five items of information are stored in an incompatible method list;
5.2.2.1.4, if the CtClass object obtained in step 5.2.2.1.2 is not empty, it indicates that the called method class exists, and it is necessary to continuously check whether the called field exists. For the CtClass object, a getField (fieldName, fieldDesc) method is called, and the field name and the field descriptor in 5.2.2.1.1 are used as parameters to attempt to obtain the corresponding field. If the return is null, the field quoted by the current expression does not exist, and the currentClass, the line number of the current expression, the calling class, the calling field name, the field descriptor and other five items of information are stored in an incompatible field list;
6. after the engineering compilation is finished, if any one of the three lists including the incompatible class, the incompatible method and the incompatible field is not empty, which indicates that the current engineering has the condition of interface incompatibility, the abnormal prompt developer is thrown out, information in the three lists is output, the developer is informed of the specific incompatible interface information, and the problem is conveniently positioned and solved.
It should be understood that fig. 3 is only an exemplary implementation manner in the embodiment of the present invention, and the application scenarios in the embodiment of the present invention include, but are not limited to, the above implementation manners.
In order to better implement the above-mentioned scheme of the present application, an embodiment of the present application further provides an interface compatibility detection device, please refer to fig. 5, where fig. 5 is a schematic structural diagram of an interface compatibility detection device provided in the embodiment of the present application; the following detailed description is made with reference to fig. 5:
as shown in fig. 5, the interface compatibility detection apparatus 50 may include a first determination unit 501, a second determination unit 502, a first search unit 503, a feedback unit 504, an analysis unit 505, a third determination unit 506, a second search unit 507, a third search unit 508, and a fourth search unit 509; wherein the content of the first and second substances,
a first determining unit 501, configured to determine one or more target class files according to a main project and a file 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;
a feedback unit 504, configured to record and feed back the class file name and the second class file that refers to the class file name if the first class file is not found in the one or more target class files.
Optionally, the first class of files 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 a class file corresponding to the main project and a class file corresponding to the referenced file; 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 files quoted by the main project, and obtaining class files corresponding to the quoted files by decompressing the quoted files; 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 a main project and files referenced by the main project, analyzing a file structure of each target class file, and storing the file structure to a target object corresponding to the target class files, wherein the file structure comprises one or more class file names referenced by the target class files; and determining one or more class file names referred by each class file according to the class file name 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 corresponding first type file is successfully searched in the one or more target type files, one or more methods quoted in the first type file are obtained; and determining related information of each method, wherein the related information comprises one or more of the file name of the belonging class, the contained method information and the contained field information.
In a possible implementation manner, the apparatus further includes a second lookup unit 507, configured to:
after the one or more methods quoted in the first class file are obtained, if the related information of the methods comprises the file names of the belonged classes, searching a third class file corresponding to the file name of the belonged class of a certain method in the one or more target class files; and if the third type of file is found to be failed, recording and feeding back the name of the class of file to which the certain method belongs and the third type of file to which the certain method belongs.
In a possible implementation manner, the apparatus further includes a third searching unit 508, configured to:
after the one or more methods referred in the first type of file are obtained, determining method information contained in each method 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 a method corresponding to a method name and a method signature in a certain method in the one or more target class files; and if the corresponding method is found to be failed, recording and feeding back the first class file to which the certain method belongs, the line where the expression of the certain method is referred, the method name of the certain method and the method signature of the certain method.
In a possible implementation manner, the apparatus further includes a fourth searching unit 509, configured to:
after the one or more methods referred in the first type of file are obtained, determining field information referred by each method, if the relevant information of the method comprises: and the field information includes a field name and a field descriptor, the method further includes: in the one or more target class files, searching fields corresponding to field names and field descriptors in a certain method; if the corresponding field is found out unsuccessfully, recording and feeding back the first class file to which the certain method belongs, the line where the expression for referring to the field information is located, the field name of the certain method and the field descriptor of the certain method.
It should be noted that specific implementation and beneficial effects of each unit in the interface compatibility detection apparatus 50 shown in fig. 5 may correspond to the corresponding descriptions in the method embodiments shown in fig. 2 to fig. 3, and are not described again here.
Referring to fig. 6, fig. 6 is a schematic structural diagram of a terminal device according to an embodiment of the present disclosure; 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 via a bus 604. The terminal device 60 may be a tablet computer, a personal computer, or other terminal device, the input device 606 may be a keyboard, a mouse, a voice input device, a touch panel, or other device, and the output device may be a display, or other device.
The memory 602 includes, but is not limited to, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM), or a portable read-only memory (CD-ROM), and the memory 602 is used for storing related instructions and data. The communication interface 603 is used for receiving and transmitting data.
The processor 601 may be one or more Central Processing Units (CPUs), and in the case that 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 a file 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 the first class file is found to be failed in the one or more target class files, recording and feeding back the class file name and a second class file which refers 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 a class file corresponding to the main project and a class file corresponding to the referenced file; the processor 601 is configured to: compiling the main project to obtain a class file corresponding to the main project; downloading the files quoted by the main project, and obtaining the class files corresponding to the quoted files by decompressing the quoted files.
In one possible implementation, the processor 601 is configured to: analyzing the file structure of each target class file, and saving the file structure to a target object corresponding to each target class file, wherein the file structure comprises one or more class file names referred by the target class files; and determining one or more class file names referred by each class file according to the class file name in the target object corresponding to each class file.
In one possible implementation, the processor 601 is configured to: if the corresponding first type file is successfully searched in the one or more target type files, one or more methods quoted in the first type file are obtained; and determining one or more of the file name of the class, the contained method information and the field information of each method in the one or more methods.
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; and if the second type of file is found to be failed, recording and feeding back the name of the file belonging to the second type of file and the first type of 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 a method corresponding to the method name and the method signature in each method in the one or more target class files; if the corresponding method is found to be failed, recording and feeding back the first class file, the line of the expression containing the method information, the class 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; according to the field name and the field descriptor of each method, searching corresponding fields in the one or more target class files; if the corresponding field is found to be failed, recording and feeding back the first class file, the line where the expression containing the field information is located, 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 project and the library file referred by the main project are compiled to obtain a corresponding class file (i.e., class file). And determining the name of the class file quoted in each class file, and searching whether the corresponding class file exists in the obtained multiple target class files according to the determined class file name. And traversing all the class files and repeating the above searching operation. If the searching in one or more class files fails according to the class file name referenced by a certain class file, the certain class file and the class file name referenced by the certain class file are recorded and fed back. By implementing the embodiment of the application, the compatibility problem of the AAR interface call is automatically detected and reported during engineering compilation. The compatibility problem can be exposed in the compiling period in advance for a developer to solve, the problem of passively waiting for the occurrence of online abnormity is avoided, and the stability of the 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 to fig. 3.
Embodiments of the present application also provide a computer-readable storage medium, which stores a computer program, where the computer program includes program instructions, and when the program instructions are executed by a processor, the method flows shown in fig. 2 to fig. 4 are implemented.
Embodiments of the present application also provide a computer program, which includes program instructions, and when the program instructions are executed by a processor, the method flows shown in fig. 2 to fig. 3 are implemented.
In the embodiments provided in the present invention, it should be understood that the disclosed apparatus, device and method can be implemented in other ways. For example, the above-described apparatus embodiments are merely illustrative, and for example, the division of the units is only one logical division, and other divisions may be realized in practice, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed.
Finally, it should be noted that: the above embodiments are only used to illustrate the technical solution of the present invention, and not to limit the same; while the invention has been described in detail and with reference to the foregoing embodiments, it will be understood by those skilled in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some or all of the technical features may be equivalently replaced; and the modifications or the substitutions do not make the essence of the corresponding technical solutions depart from the scope of the technical solutions of the embodiments of the present invention.

Claims (10)

1. A method for interface compatibility detection, comprising:
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 contained in each target class file in the one or more target class files;
and if the first class file is searched in the one or more target class files unsuccessfully, recording and feeding back the class file name and a second class file which refers to the class file name.
2. The interface compatibility detection method of claim 1, wherein said determining one or more target class files from a master project and files referenced by the master project comprises:
compiling the main project to obtain a class file corresponding to the main project;
downloading the files quoted by the main project, wherein the files quoted by the main project are compressed files; decompressing the cited file to obtain a class file corresponding to 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.
3. The interface compatibility detection method according to claim 1, wherein said determining a class file name referenced by each of the one or more target class files comprises:
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 referred by the target class file;
and determining one or more class file names referred by each class file according to the class file name in the target object corresponding to each class file.
4. The interface compatibility detection method of claim 1, further comprising:
if the first type of file is successfully searched in the one or more target type of files, one or more methods quoted in the first type of file are obtained;
and determining related information of each method, wherein the related information comprises one or more of the file name of the belonging class, the contained method information and the contained field information.
5. The interface compatibility detection method according to claim 4, wherein if the related information of the method includes the file name of the class, the interface compatibility detection method further comprises:
searching a third class file corresponding to the class file name of a certain method in the one or more target class files;
and if the third type of file is found to be failed, recording and feeding back the name of the class of file to which the certain method belongs and the third type of file to which the certain method belongs.
6. The interface compatibility detection method of claim 4, wherein if the method-related information includes: the method information includes a method name and a method signature, and the interface compatibility detection method further includes:
searching a method corresponding to a method name and a method signature in a certain method in the one or more target class files;
and if the corresponding method is found to be failed, recording and feeding back the first class file to which the certain method belongs, the line where the expression of the certain method is referred, the method name of the certain method and the method signature of the certain method.
7. The interface compatibility detection method of claim 4, wherein if the method-related information includes: and the field information includes a field name and a field descriptor, the method further includes:
in the one or more target class files, searching fields corresponding to field names and field descriptors in a certain method;
if the corresponding field is found out unsuccessfully, recording and feeding back the first class file to which the certain method belongs, the line where the expression for referring to the field information is located, the field name of the certain method and the field descriptor of the certain method.
8. An interface compatibility detection apparatus, comprising:
the first determining unit is used for determining one or more target class files according to a main project and files referenced by the main project;
a second determining unit, configured to determine a class file name referenced by each of the one or more target class files;
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 the second class file which refers to the class file name if the first class file is failed to be searched in the one or more target class files.
9. 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 one of claims 1-7.
10. A computer program, characterized in that the computer program comprises instructions which, when executed by a computer, cause the computer to carry out the method according to any one of claims 1-7.
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 true CN111352631A (en) 2020-06-30
CN111352631B 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)

Cited By (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

Cited By (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

Also Published As

Publication number Publication date
CN111352631B (en) 2024-03-19

Similar Documents

Publication Publication Date Title
WO2021098148A1 (en) Software compiling method and apparatus, and electronic device and storage medium
US7127707B1 (en) Intellisense in project upgrade
US7836440B2 (en) Dependency-based grouping to establish class identity
Dietrich et al. Broken promises: An empirical study into evolution problems in java programs caused by library upgrades
US8856752B2 (en) Monitoring asset state to enable partial build
US8516505B2 (en) Cross-platform compatibility framework for computer applications
US10083029B2 (en) Detect application defects by correlating contracts in application dependencies
US10514898B2 (en) Method and system to develop, deploy, test, and manage platform-independent software
CN111176722B (en) Method, device and storage medium for detecting file version of third party library
CN113835713B (en) Source code packet downloading method, device, computer equipment and storage medium
CN112099880B (en) Method and system for reducing application program driven by scene
US20080040656A1 (en) Instance annotation in object-oriented programming
CN111352631B (en) Interface compatibility detection method and device
CN111008017B (en) Oclin-based pre-review method for files to be submitted and related components
CN112769706A (en) Componentized routing method and system
CN114661423A (en) Cluster configuration detection method and device, computer equipment and storage medium
CN113515303A (en) Project transformation method, device and equipment
CN112445706A (en) Program abnormal code acquisition method and device, electronic equipment and storage medium
CN116578282A (en) Code generation method, device, electronic equipment and medium
CN111258802A (en) Method for capturing application program crash information and related equipment
US20210055919A1 (en) Detection of semantic equivalence of program source codes
CN111367796A (en) Application program debugging method and device
US8135943B1 (en) Method, apparatus, and computer-readable medium for generating a dispatching function
CN115543486B (en) Server-free computing oriented cold start delay optimization method, device and equipment
CN111273940A (en) Method and device for 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