JAVA reflection mechanism-based interface scanning method, apparatus and device, and storage medium

Through the interface scanning method based on the JAVA reflection mechanism, the classes in the JAR file package are automatically loaded and intercepted, and the interface information is obtained, which solves the problem of inefficient API integration, realizes the hot loading of API parameter information and automatic document generation, and improves the interface Access efficiency.

CN119271311APending Publication Date: 2025-01-07ZHONGDIAN DATA IND CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411302195.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-18
Publication Date
2025-01-07

AI Technical Summary

Technical Problem

The existing technology is inefficient when facing a large number of API integration, and requires manual sorting out business processes and API integration, resulting in inefficiency.

Method used

Using the interface scanning method based on the JAVA reflection mechanism, the classes in the JAR file package in the specified directory are traversed and loaded, and the class method calls are intercepted using the predefined class file converter and bytecode operation library, and the interface is obtained through the Java reflection mechanism. Information, realizes the hot loading of API parameter information and the automatic generation of API documents.

Benefits of technology

Without changing the source code, the Java language automatically scans all interfaces in the project project directory, improves interface access efficiency and API integration efficiency, and solves the problem of inefficient API integration.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119271311A_ABST
    Figure CN119271311A_ABST
Patent Text Reader

Abstract

The invention discloses an interface scanning method, device and equipment based on a JAVA reflection mechanism and a storage medium, and relates to the technical field of computers, and the method comprises the following steps: traversing and loading classes in a JAR file package under a specified directory; when a preset class in the loaded classes is loaded, intercepting method calling of the preset class based on a predefined class file converter and a byte code operation library; and after the interception succeeds, acquiring the interface information in the preset class based on a Java reflection mechanism. According to the method and the device, hot loading of the API parameter information can be carried out under the condition that source codes are not changed, all interfaces under a project catalog are automatically scanned by a Java language, and the API document is automatically generated, so that the interface access efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to an interface scanning method, device, equipment and storage medium based on a JAVA reflection mechanism. Background Art

[0002] In online application platforms, we usually face the problem of integrating a large number of multi-party service APIs (Application Programming Interface). Most of the current methods are based on actual business scenarios, and the APIs required by the business process are manually sorted out and integrated according to the needs. The integration efficiency is low when facing a large number of APIs. In addition, there are currently a large number of business systems based on Java language and built through the Spring Boot framework. Therefore, a method of automatically importing APIs in batches based on Java language is needed to optimize the API integration method.

[0003] The above contents are only used to assist in understanding the technical solution of the present application and do not constitute an admission that the above contents are prior art. Summary of the invention

[0004] The main purpose of this application is to provide an interface scanning method, device, equipment and storage medium based on the JAVA reflection mechanism, aiming to solve the technical problem of low integration efficiency of existing methods when facing a large number of APIs.

[0005] To achieve the above purpose, the present application proposes an interface scanning method based on JAVA reflection mechanism, the method comprising:

[0006] Traverse and load the classes in the JAR file package in the specified directory;

[0007] When loading a preset class in the loaded classes, intercepting a method call of the preset class based on a predefined class file converter and a bytecode operation library;

[0008] After successful interception, the interface information in the preset class is obtained based on the Java reflection mechanism.

[0009] In one embodiment, the step of traversing and loading classes in the JAR file package in the specified directory includes:

[0010] Traverse and obtain the files in the specified directory, and filter to obtain the JAR file package;

[0011] Obtain the directory path of the JAR file package and generate a uniform resource locator URL of the JAR file package;

[0012] Create an instance of a custom class loader based on the URL of the JAR file package;

[0013] The classes in the JAR file package are loaded through the instance of the custom class loader.

[0014] In one embodiment, before the step of intercepting the method call of the preset class based on the predefined class file converter and the bytecode operation library when loading the preset class in the loaded class, it also includes:

[0015] Based on the Java instrumentation mechanism and the preset class, define several class file converters;

[0016] Based on the Java agent mechanism, a Java agent is created, and the several class file converters are added to the Java agent.

[0017] In one embodiment, when loading a preset class in the loaded classes, the step of intercepting a method call of the preset class based on a predefined class file converter and a bytecode operation library comprises:

[0018] When loading a preset class in the loaded classes, based on a bytecode operation library, the preset class is used as a class to be enhanced;

[0019] Based on the predefined class file converter and the bytecode operation library, define interception logic for the class to be enhanced through annotations;

[0020] Based on the interception logic, the method call of the preset class is intercepted.

[0021] In one embodiment, after the interception is successful, the step of obtaining the interface information in the preset class based on the Java reflection mechanism includes:

[0022] After successful interception, the method of the preset class is scanned and obtained based on the Java reflection mechanism;

[0023] Based on the methods of the preset class, filter out the interface methods in the preset class;

[0024] Based on the interface method, obtain the parameter type of the corresponding method;

[0025] The interface method is called to obtain interface information.

[0026] In one embodiment, the method further comprises:

[0027] When the interface method is successfully called, the interface information is added to the interface registry, and an application programming interface API document is generated;

[0028] When the interface method call fails, the interface method is called again based on the preset call exception handling logic.

[0029] In one embodiment, the method further comprises:

[0030] If any JAR file package in the specified directory is detected to be updated, the updated content is obtained;

[0031] Based on the updated content, the interface information under the specified directory is re-acquired, and the API document is updated.

[0032] In addition, to achieve the above purpose, the present application also proposes an interface scanning device, the interface scanning device comprising:

[0033] The class loading module is used to traverse and load the classes in the JAR file package in the specified directory;

[0034] An interception module, used for intercepting method calls of a preset class in the loaded classes based on a predefined class file converter and a bytecode operation library when loading the preset class;

[0035] The acquisition module is used to obtain the interface information in the preset class based on the Java reflection mechanism after the interception is successful.

[0036] In addition, to achieve the above-mentioned purpose, the present application also proposes an interface scanning device, which includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the interface scanning method described above.

[0037] In addition, to achieve the above-mentioned purpose, the present application also proposes a storage medium, which is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, the steps of the interface scanning method described above are implemented.

[0038] One or more technical solutions proposed in this application have at least the following technical effects:

[0039] The interface scanning method, apparatus, device and storage medium based on the JAVA reflection mechanism proposed in the embodiments of the present application specifically traverse and load the classes in the JAR file package under the specified directory; when loading the preset class in the loaded class, based on the predefined class file converter and bytecode operation library, the method call of the preset class is intercepted; after the interception is successful, the interface information in the preset class is obtained based on the Java reflection mechanism.

[0040] This application traverses the JAR file package in the specified directory, loads all classes in the loaded JAR file package, and then uses the predefined class file converter and bytecode operation library to intercept the method call of the preset class when loading the preset class in all classes. Then, through the Java reflection mechanism, the interface method in the preset class is scanned and further interface information is obtained. It can hot load API parameter information without changing the source code, realize Java language automatic scanning of all interfaces under the project directory, and automatically generate API documents to improve interface access efficiency and API integration efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0041] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0042] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.

[0043] Figure 1 A flowchart of the first embodiment of the interface scanning method of the present application is provided;

[0044] Figure 2 A flowchart of the second embodiment of the interface scanning method of the present application is provided;

[0045] Figure 3 A flowchart of the third embodiment of the interface scanning method of the present application is provided;

[0046] Figure 4 A flowchart of the fourth embodiment of the interface scanning method of the present application is provided;

[0047] Figure 5 This is a schematic diagram of the module structure of the interface scanning device according to an embodiment of the present application;

[0048] Figure 6 This is a schematic diagram of the device structure of the hardware operating environment involved in the interface scanning method in the embodiment of the present application.

[0049] The purpose, features and advantages of this application will be further described in conjunction with the embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION

[0050] It should be understood that the specific embodiments described herein are only used to explain the technical solutions of the present application and are not used to limit the present application.

[0051] In order to better understand the technical solution of the present application, a detailed description will be given below in conjunction with the accompanying drawings and specific implementation methods.

[0052] The main solution of the embodiment of the present application is: traverse and load the classes in the JAR file package under the specified directory; when loading the preset class in the loaded class, intercept the method call of the preset class based on the predefined class file converter and bytecode operation library; after the interception is successful, obtain the interface information in the preset class based on the Java reflection mechanism.

[0053] Technical terms involved in the embodiments of this application:

[0054] Java Agent: Java Agent is a feature of the Java platform that allows a Java application to be dynamically attached to the JVM when it is started or running. Using Java Agent, developers can monitor, analyze, or modify the behavior of an application without modifying the original application code.

[0055] Byte Buddy: A Java bytecode manipulation library that allows developers to create and modify Java classes at runtime. Using Byte Buddy, you can enhance the functionality of a class by dynamically generating or changing the class bytecode without changing the original source code.

[0056] Java Instrumentation: Java instrumentation is a technique for modifying or enhancing the behavior of existing code. In Java, bytecode instrumentation can be implemented through the Java Instrument API. The Java Instrument API is an API provided by the Java platform for Java application performance analysis and monitoring. It allows agents to dynamically modify the bytecode of an application at JVM startup or runtime. Developers can intercept the class loading process and use a custom ClassFileTransformer interface to modify the class at the bytecode level.

[0057] Java reflection mechanism: A feature of the Java language that allows the program to obtain class information at runtime, including class methods, properties, constructors, annotations, etc., without knowing the specific implementation of the class in advance. Through the reflection mechanism, class information can be obtained while the program is running, and class methods can be called, objects can be created, etc. at runtime. The Method class is a reflection mechanism class used to represent the methods of a class or interface. It contains metadata about method name, parameter types, return type, access modifiers, and other information.

[0058] Due to the existing interface scanning method, when faced with the need to integrate a large number of third-party service APIs, it is necessary to manually sort out the APIs required for the business process and integrate them, resulting in low API integration efficiency.

[0059] The present application provides a solution, which loads all classes in the loaded JAR file package by traversing the JAR file package in the specified directory, and then uses the predefined class file converter and bytecode operation library to intercept the method call of the preset class when loading the preset class in all classes, and then uses the Java reflection mechanism to scan and obtain the interface method in the preset class, and further obtain the interface information. It can perform hot loading of API parameter information without changing the source code, realize Java language automatic scanning of all interfaces under the project engineering directory, and automatically generate API documents, so as to improve the interface access efficiency and API integration efficiency.

[0060] It should be noted that the execution subject of this embodiment can be a computing service device with data processing, network communication and program running functions, such as a tablet computer, a personal computer, a mobile phone, etc., or an interface scanning device capable of realizing the above functions, etc. The following takes the interface scanning device as an example to illustrate this embodiment and the following embodiments.

[0061] Based on this, the embodiment of the present application provides an interface scanning method based on the JAVA reflection mechanism, referring to Figure 1 , Figure 1 This is a flowchart of the first embodiment of the interface scanning method of the present application.

[0062] In this embodiment, the interface scanning method includes steps S110 to S130:

[0063] Step S110, traverse and load the classes in the JAR file package in the specified directory;

[0064] It should be noted that the designated directory may be a directory of the software engineering file accessed by the interface scanning device in the file system, and the software engineering file directory may contain multiple JAR file packages corresponding to third-party API interfaces and resource files in other formats. A JAR file package refers to a file formed by packaging multiple Java class files, related metadata and resource (such as text, pictures, etc.) files, wherein a class file refers to a file containing class definition information.

[0065] It should also be noted that a class refers to a collection of methods or interfaces with the same data members and function members. A class is a template that defines object properties and methods. Objects (i.e., instances of a class) can be created through a class. The class loader is part of the Java runtime environment, and it is responsible for dynamically loading the bytecode of a class from the file system, network, or other sources into the memory of the Java virtual machine (JVM).

[0066] Specifically, the interface scanning device first traverses the files in the specified directory through recursive logic scanning, and filters to obtain the JAR file package, and then loads the JAR file package through the class loader, so as to load all classes in the JAR file package and use these loaded classes as classes to be reflected, so that the Java reflection mechanism can be used later to obtain the interface information in the class to be reflected.

[0067] In a feasible implementation manner, step 110 may include steps A01 to A04:

[0068] Step A01, traverse and obtain the files in the specified directory, and filter to obtain the JAR file package;

[0069] Step A02, obtaining the directory path of the JAR file package and generating a uniform resource locator URL of the JAR file package;

[0070] First, access the files in the specified directory through the java.io.File class, and filter them according to the file extension .jar to obtain all JAR file packages in the specified file directory.

[0071] Then obtain the directory path of the JAR file package. For example, you can use the getPath or getAbsolutePath method to obtain the complete file path of each JAR file.

[0072] Then, according to the directory path and file package name of each JAR file package, a uniform resource locator URL of each JAR file package is generated.

[0073] Step A03, creating an instance of a custom class loader based on the URL of the JAR file package;

[0074] Step A04: Load the classes in the JAR file package through the instance of the custom class loader.

[0075] In this embodiment, it is preferred to use a custom class loader, create an instance of the custom class loader, and add the URL of the JAR file package obtained above to the search path in the instance of the custom class loader, so that the instance of the custom class loader can load the JAR file package of the corresponding URL, and then load the class of the JAR file package, and use these loaded classes as the class to be reflected. Among them, the custom class loader is a class loader implemented by the developer, which is usually created by integrating the ClassLoader class and overriding its findClass method. The custom class loader can control the class search and loading mechanism, which allows developers to change the class loading method according to specific class loading requirements. The role of the custom class loader is to dynamically load external JAR packages or class files, thereby realizing the function of dynamically expanding the application.

[0076] Among them, ClassLoader is the core component of the Java class loading mechanism. Almost all Java class loading activities are completed through ClassLoader or its subclasses.

[0077] In this implementation, the JAR file packages in the specified directory are found through filtering, the URL of each JAR file package is obtained, and the classes in the JAR file package are dynamically loaded into the Java virtual machine through a custom class loader.

[0078] Step S120, when loading a preset class in the loaded classes, intercepting a method call of the preset class based on a predefined class file converter and a bytecode operation library;

[0079] Step S130, after the interception is successful, the interface information in the preset class is obtained based on the Java reflection mechanism.

[0080] It should be noted that the preset class refers to a specific class that the relevant developers pre-select based on the actual interface scanning requirements. The preset class can be loaded by inputting the specific class name as a parameter in the defineClass method in the custom class loader; the interface method is used to indicate the operations that the members of the class can perform. The interface method may include but is not limited to the method name of the interface class, interface basic information, and interface parameter information.

[0081] In this embodiment, the predefined class file transformer refers to a class file transformer predefined by relevant developers, wherein Transformer refers to a class that implements the ClassFileTransformer interface, and the class includes a Transform method that implements the ClassFileTransformer interface. The Transform method is used to be called before the class file is loaded into the JVM, allowing the developer to modify the bytecode of the class. The ClassFileTransformer interface is a key part of the JavaInstrument API, which is used to define how to transform the class file before loading it into the JVM.

[0082] It is understandable that after the JAR file in the specified directory is updated, in order to improve the efficiency of the interface scan, the interface scan may be performed only on the updated JAR file to confirm whether there is any newly added, deleted or modified interface information.

[0083] In this embodiment, the preset classes may be all classes or some specific classes in the aforementioned classes to be reflected.

[0084] First, according to the needs of developers, when the interface scanning device uses a custom class loader to load the preset class, it is necessary to first intercept the methods of each class in the preset class.

[0085] Specifically, in the predefined Transformer class, the bytecode operation library Byte Buddy is used to enhance the preset class to be loaded in the Java virtual machine, modify the bytecode of the preset class, and intercept the method call in the preset class. Class enhancement generally refers to the act of adding new methods, fields, or modifying existing methods to a class without modifying the original class source code.

[0086] Finally, after successfully intercepting the method call in the preset class, use the custom class loader to load the preset class, and then use the Java reflection mechanism to perform a reflection check on the preset class to obtain the information of the preset class, including the class methods, properties, constructors, annotations, etc., and filter out the methods declared or defined as interfaces. Perform a method call on the filtered interface method to obtain the interface information. Among them, the interface information may include basic interface information and interface parameter information. The basic interface information may include but is not limited to interface encoding method, interface ID, interface description, interface name, interface protocol type, interface request type, whether the interface is available, interface address and service ID. The interface parameter information may include but is not limited to parameter ID, interface call parameter, parameter description, parameter default value, parameter attribution type, parameter name, parameter type, response representation and response preset value.

[0087] Furthermore, in order to obtain a predefined class file transformer Transformer, before the above step S120, the method further includes step S01: defining a plurality of class file transformers based on the Java proxy mechanism and the Java plug-in mechanism.

[0088] Specifically, based on the Java proxy mechanism and the Java instrumentation mechanism, a corresponding Transformer method is constructed for each preset class, and a specific class can be intercepted by specifying a matching class name in the Transformer method.

[0089] At the same time, in order to make the constructed Transformer methods effective, the interface scanning device needs to create a Java agent and register these Transformer methods in the Java agent.

[0090] In a feasible implementation manner, the step S01 may include steps B01 to B02:

[0091] Step B01, defining a plurality of class file converters based on the Java plug-in mechanism and the preset class;

[0092] Step B02: Based on the Java agent mechanism, create a Java agent, and add the plurality of class file converters to the Java agent.

[0093] Specifically, firstly, according to the Java instrumentation mechanism, a corresponding Transformer method is constructed for each preset class, and several Transformer classes are defined.

[0094] Then, in order to intercept the class before the Java virtual machine calls the JAR file package in the specified directory, it is necessary to create a Java agent containing the premain method according to the Java agent mechanism, and then add the several Transformer classes to the Java agent, so as to register the Transformer class to the Instrumentation instance. In this way, whenever the Java virtual machine tries to load a class in the JAR file package, it is necessary to first call the Transformer method in the Transformer class and pass in the bytecode of the class. Then, the relevant developer can modify the bytecode in the Transformer method, and then return the modified bytecode to implement bytecode instrumentation. Among them, the premain method is used to be called before the Java virtual machine initializes the main class.

[0095] This embodiment provides an interface scanning method, which traverses and loads the classes in the JAR file package under a specified directory; when loading a preset class in the loaded classes, intercepting the method call of the preset class based on a predefined class file converter and a bytecode operation library; after the interception is successful, obtaining the interface information in the preset class based on the Java reflection mechanism.

[0096] This application traverses the JAR file package in the specified directory, loads all classes in the loaded JAR file package, and then uses the predefined class file converter and bytecode operation library to intercept the method call of the preset class when loading the preset class in all classes. Then, through the Java reflection mechanism, the interface method in the preset class is scanned and further interface information is obtained. It can hot load API parameter information without changing the source code, realize Java language automatic scanning of all interfaces under the project directory, and automatically generate API documents to improve interface access efficiency and API integration efficiency.

[0097] Based on the first embodiment of the present application, in the second embodiment of the present application, the same or similar contents as those in the above-mentioned embodiment 1 can refer to the above introduction, and will not be repeated later. Figure 2 , the step S120 includes steps S210 to S230:

[0098] Step S210, when loading a preset class in the loaded classes, based on a bytecode operation library, taking the preset class as a class to be enhanced;

[0099] In this embodiment, for the preset class to be loaded into the Java virtual machine, a ByteBuddy object is created according to Byte Buddy, and the redefine method is used in the Byte Buddy object to perform class enhancement on the preset class to be loaded into the Java virtual machine. First, the TypeDescription description of the Byte Buddy object is used as a parameter, and each class in the preset class is designated as a class to be enhanced.

[0100] Step S220, based on the predefined class file converter and the bytecode operation library, defining interception logic for the class to be enhanced through annotations;

[0101] Then, the interface scanning device needs to define the enhanced logic. In this embodiment, the enhanced logic is the interception logic. Specifically, in the Byte Buddy object, the visit method and the Advice.to method are used to intercept the method call in the class, and the InterceptorAdvice class is used to define the interception logic for the preset class. By returning the interception logic of the preset class to the corresponding predefined Transformer, the interception of the preset class is realized.

[0102] Specifically, in the InterceptorAdvice class, the @Advice.OnMethodEnter annotation is used to intercept the entry point of the method in the preset class, and the @Advice.OnMethodExit annotation is used to intercept the exit point of the method in the preset class, thereby defining the interception logic of the preset class.

[0103] It can be understood that in order to ensure the reliability of intercepting methods in the preset class, the interface scanning device sets the parameter "onThrowable = Throwable.class" of the @Advice.OnMethodExit annotation to allow receiving a Throwable object when the method in the preset class exits, and then checks whether the method in the preset class throws an exception, generates a corresponding warning notification, and displays it visually on the interface scanning device so that relevant personnel can handle it accordingly.

[0104] Step S230: intercepting the method call of the preset class based on the interception logic.

[0105] Finally, the defined interception logic is directly applied to the preset class to be loaded, and then the preset class is loaded using the custom class loader to intercept the method calls of the preset class and realize hot replacement of the preset class information without changing the source code.

[0106] This embodiment provides an interface scanning method, which traverses and loads the classes in the JAR file package under a specified directory; defines several class file converters based on the Java instrumentation mechanism and the preset class; creates a Java agent based on the Java agent mechanism, and adds the several class file converters to the Java agent; when loading the preset class in the loaded class, based on the bytecode operation library, the preset class is used as a class to be enhanced; based on the predefined class file converter and the bytecode operation library, the interception logic is defined for the class to be enhanced through annotations; based on the interception logic, the method call of the preset class is intercepted; after the interception is successful, the interface information in the preset class is obtained based on the Java reflection mechanism.

[0107] This embodiment uses the above solution to build a class file converter Transformer based on the Java proxy mechanism and the Java plug-in mechanism, and uses a custom class loader to load the preset class to achieve class isolation loading. The functional module corresponding to the class can be loaded or unloaded as needed to increase flexibility. At the same time, when loading the preset class, class enhancement is achieved through Byte Buddy, and API parameter information can be hot loaded without changing the source code, realizing Java language automatic scanning of all interfaces under the project directory, and automatically generating API documents, so as to improve interface access efficiency and API integration efficiency.

[0108] Based on the first embodiment and / or the second embodiment of the present application, in the third embodiment of the present application, the same or similar contents as those in the first embodiment and / or the second embodiment can refer to the above description, and will not be described in detail later. Figure 3 , the step S130 includes steps S310 to S340:

[0109] Step S310, after successful interception, scan and obtain the method of the preset class based on the Java reflection mechanism;

[0110] Specifically, after intercepting the method of the preset class, the interface scanning device does not detect through the Throwable object that the method in the preset class throws an exception, such as class non-existence, class loading failure, and other exception information, which means that the interface scanning device successfully intercepts the method of the preset class.

[0111] After the interface scanning device successfully intercepts the method of the preset class, it obtains the interface information of the class to be reflected through the Java reflection mechanism.

[0112] First, use the getClass method of the class to obtain the Class object of the class to be reflected loaded by URLClassLoader. Then use the getMethods or getDeclaredMethods method of the Class object to obtain all public methods or all declared methods of the class to be reflected and obtain a method array.

[0113] Step S320, based on the methods of the preset class, filter out the interface methods in the preset class;

[0114] Step S330, based on the interface method, obtaining the parameter type of the corresponding method;

[0115] Step S340: Call the interface method to obtain interface information.

[0116] Then, traverse the method array again to filter out the methods marked as interfaces. In this embodiment, the interface list of the class to be reflected can be checked through Java reflection, and then the corresponding interface method can be traversed in the method array to confirm whether there is a corresponding method signature in the class to be reflected. In Java 8 and above, the default method of the interface can also be identified by checking the isDefault method of the method. For each interface method obtained by screening, the getParameterTypes method of the Method object is used to obtain the parameter type information of the interface method.

[0117] Finally, the interface method is called by calling the invoke method of the Method object to obtain the interface information.

[0118] It is understandable that in order to ensure the smooth acquisition of interface information, it is necessary to handle the exceptions that occur during the method call. Therefore, after the above step 130, steps C01 to C02 are also included:

[0119] Step C01, when the interface method is successfully called, the interface information is added to the interface registration table, and an application programming interface API document is generated;

[0120] Step C02: when the interface method call fails, the interface method is called again based on a preset call exception handling logic.

[0121] Specifically, when the interface method is called successfully, the interface scanning device adds the relevant information of the successful interface call, including but not limited to interface ID, interface description, interface name, interface protocol type, interface request type, whether the interface is available, interface address, interface call parameters, parameter description, parameter name, parameter type, and response representation, to the interface registry related to the specified directory, and after performing an interface scan on all JAR file packages under the specified directory, generates an application programming interface API document based on the interface registry to achieve API integration under the specified directory.

[0122] When an interface method fails to be called, the interface scanning device will re-call the failed interface method according to the call exception handling logic pre-set by the developer. The call exception handling logic can randomly select parameters to call the interface method within the parameter range that satisfies the interface method, and set the number of retries to avoid repeated calls that cause the device to freeze. At the same time, you can also set the information based on the interface method call failure to generate an API call failure warning, and display it visually on the interface scanning device.

[0123] This embodiment provides an interface scanning method, which traverses and loads the classes in the JAR file package under a specified directory; when loading a preset class in the loaded classes, intercepts the method call of the preset class based on a predefined class file converter and a bytecode operation library; after the interception is successful, scans and obtains the method of the preset class based on the Java reflection mechanism; based on the method of the preset class, filters out the interface method in the preset class; based on the interface method, obtains the parameter type of the corresponding method; calls the interface method to obtain interface information.

[0124] Through the above scheme, this embodiment uses the Java reflection mechanism to scan and obtain all methods in the preset class, then screens and obtains the interface method, and finally obtains the relevant information of the interface by calling the interface method, so as to realize the automatic scanning of all interfaces under the project engineering directory in Java language and automatically generate API documents, so as to improve the interface access efficiency and API integration efficiency.

[0125] Based on the first embodiment and / or the second embodiment and / or the third embodiment of the present application, in the fourth embodiment of the present application, the same or similar contents as those in the first embodiment, the second embodiment and / or the third embodiment can refer to the above description and will not be described in detail later. Figure 4 The method further includes steps S410 to S420:

[0126] Step S410, if it is detected that any JAR file package in the specified directory is updated, the updated content is obtained;

[0127] Step S420: Based on the updated content, reacquire the interface information under the specified directory and update the API document.

[0128] First, the interface scanning device can periodically poll and scan the JAR files in the specified directory, and determine whether the files corresponding to the third-party API interfaces in the specified directory are updated by methods such as the size of the JAR file package and the modification date.

[0129] Then, when the size and modification date of the JAR file package are updated, the updated JAR file package in the specified directory is obtained, and the corresponding interface information of the updated JAR file package is re-obtained, synchronized to the interface registry associated with the specified directory, and the API document is updated.

[0130] This embodiment provides an interface scanning method, which traverses and loads the classes in the JAR file package under the specified directory; when loading the preset class in the loaded class, intercepts the method call of the preset class based on the predefined class file converter and the bytecode operation library; after the interception is successful, obtains the interface information in the preset class based on the Java reflection mechanism; when the interface method call is successful, adds the interface information to the interface registry, and generates an application programming interface API document; when the interface method call fails, re-calls the interface method based on the preset call exception handling logic; if it is detected that any of the JAR file packages under the specified directory is updated, the update content is obtained; based on the update content, the interface information under the specified directory is re-obtained, and the API document is updated.

[0131] Through the above scheme, this embodiment detects and updates the JAR file packages in the specified directory. When an updated JAR file package is detected, the interface is re-scanned to obtain the updated interface information, thereby realizing real-time updating of API documents, avoiding manual operations, and further improving API integration efficiency.

[0132] This application also provides an interface scanning device, please refer to Figure 5 , the interface scanning device comprises:

[0133] The class loading module 10 is used to traverse and load the classes in the JAR file package in the specified directory;

[0134] The interception module 20 is used to intercept the method call of the preset class based on the predefined class file converter and the bytecode operation library when loading the preset class in the loaded class;

[0135] The acquisition module 30 is used to acquire the interface information in the preset class based on the Java reflection mechanism after the interception is successful.

[0136] The interface scanning device provided in the present application adopts the interface scanning method in the above embodiment, which can solve the technical problem of low integration efficiency of the existing method when facing a large number of APIs.

[0137] Compared with the prior art, the beneficial effects of the interface scanning device provided in the present application are the same as the beneficial effects of the interface scanning method provided in the above embodiment, and other technical features in the interface scanning device are the same as the features disclosed in the above embodiment method, which will not be repeated here.

[0138] The present application provides an interface scanning device, which includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the interface scanning method in the above-mentioned embodiment one.

[0139] Reference below Figure 6 , which shows a schematic diagram of the structure of an interface scanning device suitable for implementing the embodiment of the present application. The interface scanning device in the embodiment of the present application may include but is not limited to mobile terminals such as mobile phones, laptop computers, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Descriptions), PMPs (Portable Media Players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 6 The interface scanning device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.

[0140] like Figure 6 As shown, the interface scanning device may include a processing device 1001 (e.g., a central processing unit, a graphics processor, etc.), which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM: Read Only Memory) 1002 or a program loaded from a storage device 1003 to a random access memory (RAM: Random Access Memory) 1004. In RAM1004, various programs and data required for the operation of the interface scanning device are also stored. The processing device 1001, ROM1002, and RAM1004 are connected to each other through a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Generally, the following systems can be connected to the I / O interface 1006: an input device 1007 including, for example, a touch screen, a touch pad, a keyboard, a mouse, an image sensor, a microphone, an accelerometer, a gyroscope, etc.; an output device 1008 including, for example, a liquid crystal display (LCD: Liquid Crystal Display), a speaker, a vibrator, etc.; a storage device 1003 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 1009. Communication device 1009 can allow interface scanning device to communicate with other devices wirelessly or by wire to exchange data. Although interface scanning device with various systems is shown in the figure, it should be understood that it is not required to implement or have all the systems shown. More or fewer systems can be implemented or have alternatively.

[0141] In particular, according to the embodiments disclosed in the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present application include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device, or installed from a storage device 1003, or installed from a ROM 1002. When the computer program is executed by the processing device 1001, the above-mentioned functions defined in the method of the embodiment disclosed in the present application are executed.

[0142] The interface scanning device provided in the present application adopts the interface scanning method in the above embodiment, which can solve the technical problem of low integration efficiency of the existing method when facing a large number of APIs.

[0143] Compared with the prior art, the beneficial effects of the interface scanning device provided in the present application are the same as the beneficial effects of the interface scanning method provided in the above embodiment, and other technical features in the interface scanning device are the same as the features disclosed in the method of the previous embodiment, which will not be repeated here.

[0144] It should be understood that the various parts disclosed in this application can be implemented by hardware, software, firmware or a combination thereof. In the description of the above embodiments, specific features, structures, materials or characteristics can be combined in any one or more embodiments or examples in a suitable manner.

[0145] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.

[0146] The present application provides a computer-readable storage medium having computer-readable program instructions (ie, computer programs) stored thereon, and the computer-readable program instructions are used to execute the interface scanning method in the above-mentioned embodiment.

[0147] The computer-readable storage medium provided in the present application may be, for example, a USB flash drive, but is not limited to electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, systems or devices, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this embodiment, the computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, system or device. The program code contained on the computer-readable storage medium may be transmitted using any appropriate medium, including but not limited to: wires, optical cables, RF (Radio Frequency), etc., or any suitable combination of the above.

[0148] The computer-readable storage medium may be included in the interface scanning device; or may exist independently without being assembled into the interface scanning device.

[0149] The above-mentioned computer-readable storage medium carries one or more programs. When the above-mentioned one or more programs are executed by the interface scanning device, the interface scanning device: traverses and loads the classes in the JAR file package under the specified directory; when loading the preset class among the loaded classes, intercepts the method call of the preset class based on the predefined class file converter and bytecode operation library; after the interception is successful, obtains the interface information in the preset class based on the Java reflection mechanism.

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

[0151] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present application. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0152] The modules involved in the embodiments described in this application may be implemented by software or hardware, wherein the name of the module does not constitute a limitation on the unit itself in some cases.

[0153] The readable storage medium provided in the present application is a computer-readable storage medium, which stores computer-readable program instructions (i.e., computer programs) for executing the above-mentioned interface scanning method, and can solve the technical problem of low integration efficiency of existing methods when facing a large number of APIs.

[0154] Compared with the prior art, the beneficial effects of the computer-readable storage medium provided in the present application are the same as the beneficial effects of the interface scanning method provided in the above embodiment, and are not elaborated here.

[0155] The above are only preferred embodiments of the present application, and are not intended to limit the patent scope of the present application. Any equivalent structure or equivalent process transformation made using the contents of the present application specification and drawings, or directly or indirectly applied in other related technical fields, are also included in the patent processing scope of the present application.

Claims

1. An interface scanning method based on JAVA reflection mechanism, characterized in that: The method comprises: Traverse and load the classes in the JAR file package in the specified directory; When loading a preset class in the loaded classes, intercepting a method call of the preset class based on a predefined class file converter and a bytecode operation library; After successful interception, the interface information in the preset class is obtained based on the Java reflection mechanism.

2. The method according to claim 1, characterized in that The step of traversing and loading classes in the JAR file package under the specified directory includes: Traverse and obtain the files in the specified directory, and filter to obtain the JAR file package; Obtain the directory path of the JAR file package and generate a uniform resource locator URL of the JAR file package; Create an instance of a custom class loader based on the URL of the JAR file package; The classes in the JAR file package are loaded through the instance of the custom class loader.

3. The method according to claim 1, characterized in that Before the step of intercepting the method call of the preset class based on the predefined class file converter and the bytecode operation library when loading the preset class in the loaded class, the method further includes: Based on the Java instrumentation mechanism and the preset class, define several class file converters; Based on the Java agent mechanism, a Java agent is created, and the several class file converters are added to the Java agent.

4. The method according to claim 3, characterized in that The step of intercepting the method call of the preset class based on the predefined class file converter and the bytecode operation library when loading the preset class in the loaded class includes: When loading a preset class in the loaded classes, based on a bytecode operation library, the preset class is used as a class to be enhanced; Based on the predefined class file converter and the bytecode operation library, define interception logic for the class to be enhanced through annotations; Based on the interception logic, the method call of the preset class is intercepted.

5. The method according to claim 1, characterized in that After the interception is successful, the step of obtaining the interface information in the preset class based on the Java reflection mechanism includes: After successful interception, the method of the preset class is scanned and obtained based on the Java reflection mechanism; Based on the methods of the preset class, filter out the interface methods in the preset class; Based on the interface method, obtain the parameter type of the corresponding method; The interface method is called to obtain interface information.

6. The method as claimed in claim 5, characterized in that The method further comprises: When the interface method is successfully called, the interface information is added to the interface registry, and an application programming interface API document is generated; When the interface method call fails, the interface method is called again based on the preset call exception handling logic.

7. The method according to any one of claims 1 to 6, characterized in that The method further comprises: If any JAR file package in the specified directory is detected to be updated, the updated content is obtained; Based on the updated content, the interface information under the specified directory is re-acquired, and the API document is updated.

8. An interface scanning device, characterized in that: The interface scanning device comprises: The class loading module is used to traverse and load the classes in the JAR file package in the specified directory; An interception module, used for intercepting method calls of a preset class in the loaded classes based on a predefined class file converter and a bytecode operation library when loading the preset class; The acquisition module is used to obtain the interface information in the preset class based on the Java reflection mechanism after the interception is successful.

9. An interface scanning device, characterized in that: The device comprises: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the interface scanning method according to any one of claims 1 to 7.

10. A storage medium, characterized in that: The storage medium is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, the steps of the interface scanning method according to any one of claims 1 to 7 are implemented.

Citation Information

Cited By

  • Code hot loading method and equipment

    CN120762808A