Method, device, computer device and storage medium for generating API documentation

CN115981723BActive Publication Date: 2026-10-27ANHUI HANGTIAN INFORMATION CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202211643056.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-12-20
Publication Date
2026-10-27
Estimated Expiration
2042-12-20

AI Technical Summary

Technical Problem

其他人在获取API文档后,可以通过API文档查看相关接口信息,利用简单的反编译获取代码中的相关注释,使得系统源代码泄露的风险增大

Benefits of technology

[0037]本发明所述的生成API文档的装置与所述生成API文档的方法相对于现有技术的优势相同,在此不再赘述。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115981723B_ABST
    Figure CN115981723B_ABST
Patent Text Reader

Abstract

The application provides a method and device for generating API document, computer equipment and storage medium, and relates to the technical field of software development, which comprises the following steps: obtaining original code annotation; adding API annotation to the original code annotation to obtain target code annotation; judging whether the target code annotation enters an annotation processor according to the annotation level of the target code annotation; if the target code annotation enters the annotation processor, processing the target code annotation entering the annotation processor to obtain API data; and summarizing the API data to generate API document. The application realizes that the API document is not released with the project, thereby reducing the risk of system information leakage.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of software development technology, and more specifically, to a method, apparatus, computer device, and storage medium for generating API documentation. Background Technology

[0002] Java is a portable high-level programming language where systems and applications communicate through APIs. An API is a set of predefined functions, and the API documentation is a technical content delivery document about the API, containing instructions on how to effectively use and integrate it. It contains all the information needed to use the API, detailing functions, classes, return types, parameters, etc. Understanding the API is crucial.

[0003] Currently, API documentation is mainly written in two ways: manually and generated using tools. Manual writing involves using Markdown syntax or other HTML styles to create API documentation, but this increases workload and can lead to issues like code and documentation not being updated in a timely manner. Tool-generated API documentation typically involves writing relevant information along with the code, then extracting and parsing it using specific tools. This technique primarily utilizes Java's built-in Swagger technology. Swagger automatically scans and parses code and annotations when the project starts, generating API documentation data, which is then made available to users through the API. However, using Swagger requires referencing its related libraries, and these libraries are also packaged when the production package is generated. This means the API documentation data is coupled into the program and released with it. After obtaining the API documentation, others can view related interface information and use simple decompilation to extract relevant comments from the code, increasing the risk of source code leakage. Summary of the Invention

[0004] The problem this invention addresses is the risk of system source code leakage that arises when API documentation is released with a project.

[0005] To address the above problems, this invention provides a method for generating API documentation, comprising:

[0006] Obtain the original code annotation; add API annotations to the original code annotation to obtain the target code annotation; determine whether the target code annotation enters the annotation processor based on the annotation level of the target code annotation; if the target code annotation enters the annotation processor, process the target code annotation that has entered the annotation processor to obtain API data; summarize the API data to generate API documentation.

[0007] Optionally, the annotation types of the source code annotations include control type annotations, service type annotations, and method parameter type annotations. The API annotations include a first category, a second category, and a third category. The first category of the API annotations is used to mark the control type annotations of the source code annotations. The second category of the API annotations is used to mark the service type annotations of the source code annotations. The third category of the API annotations is used to mark the method parameter type annotations of the source code annotations.

[0008] The step of adding API annotations to the original code annotations to obtain target code annotations includes:

[0009] Determine the annotation type of the original code annotation;

[0010] Based on the annotation type of the original code annotation, the corresponding category of the API annotation is added to the original code annotation to obtain the target code annotation.

[0011] Optionally, if the target code annotation enters the annotation processor, processing the target code annotation entering the annotation processor to obtain API data includes:

[0012] Extract multiple first elements marked by the first category from the target code annotations that enter the annotation processor, and sequentially determine whether the first element contains the control type annotation, wherein the first element includes multiple sub-elements;

[0013] If the control type annotation is not present, then retrieve the next first element.

[0014] If the control type annotation is included, then extract the service data of the first category and the control type annotation;

[0015] Extract the sub-elements of the method class from the first element, and obtain the second element based on the sub-elements of the method class;

[0016] The method attribute data and parameter attribute data are obtained based on the second element;

[0017] API data is obtained based on the service data, the method attribute data, and the parameter attribute data.

[0018] Optionally, obtaining method attribute data and parameter attribute data based on the second element includes:

[0019] Determine whether the second element contains the service type annotation.

[0020] If the service type annotation is included, then extract the self-attribute data of the second category and the second element, and the method attribute data of the service type annotation;

[0021] The second element parameter is converted to obtain the third element;

[0022] The parameter attribute data is obtained based on the self-parameter data of the third element and the method parameter type annotation.

[0023] Optionally, the annotation levels include source code level, class level, and runtime level;

[0024] The step of determining whether the target code annotation enters the annotation processor based on the annotation level of the target code annotation includes:

[0025] When the annotation level is the source code level, the target code annotation enters the annotation processor;

[0026] When the annotation level is the class level or the runtime level, the target code annotation does not enter the annotation processor.

[0027] Optionally, the step of not allowing the target code annotation to enter the annotation processor when the annotation level is the class level or the runtime level includes:

[0028] When the annotation level is the class level, the target code annotation does not enter the annotation processor, but enters the source file and is not loaded or run.

[0029] When the annotation level is the runtime level, the target code annotation does not enter the annotation processor, but enters the source file and is loaded and executed, wherein the source file is a compiled bytecode file.

[0030] Optionally, the annotation level of the API annotations is the source code level.

[0031] The method for generating API documentation described in this invention adds API annotations to original code annotations to obtain target code annotations. Annotation levels ensure that related annotations only appear in the annotation processor, guaranteeing that the runtime program is not interfered with by related annotations. If a target code annotation enters the annotation processor, it is processed according to the API annotation, obtaining API data. The annotation processor then generates API data, which is finally aggregated and packaged to generate API documentation. In this invention, the API data is generated by the target code annotations, which are obtained by adding API annotations to the original code annotations. These target code annotations are not coupled into the program composed of code; therefore, the generated API data is also not coupled into the program. This separation of API documentation and program effectively reduces the risk of API data leakage, thereby reducing the risk of system source code leakage.

[0032] The present invention also provides an apparatus for generating API documentation, comprising: a first acquisition unit, the first acquisition unit being used to acquire original code annotations;

[0033] An annotation unit, which is used to add API annotations to the original code annotations to obtain target code annotations;

[0034] The judgment unit is used to determine whether the target code annotation enters the annotation processor based on the annotation level of the target code annotation;

[0035] The second acquisition unit is used to process the target code annotation that has entered the annotation processor and acquire API data if the target code annotation enters the annotation processor.

[0036] A generation unit is used to aggregate the API data to generate API documentation.

[0037] The apparatus for generating API documentation and the method for generating API documentation described in this invention have the same advantages over the prior art, and will not be repeated here.

[0038] The present invention also provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the steps of the method for generating API documentation.

[0039] The computer device and the method for generating API documentation described in this invention have the same advantages over the prior art, and will not be repeated here.

[0040] The present invention also provides a computer-readable storage medium storing a computer program, which, when read and executed by a processor, implements the steps of the method for generating API documentation.

[0041] The computer-readable storage medium described in this invention and the method for generating API documentation have the same advantages over the prior art, and will not be repeated here. Attached Figure Description

[0042] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with the invention and, together with the description, serve to explain the principles of the invention.

[0043] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0044] Figure 1 The diagram shown illustrates the application environment of the method for generating API documentation in this embodiment of the invention.

[0045] Figure 2 The diagram shown is a schematic representation of a method for generating API documentation in an embodiment of the present invention.

[0046] Figure 3 The diagram shown is a first schematic diagram of obtaining API data in an embodiment of the present invention;

[0047] Figure 4 The diagram shown is a second schematic diagram of obtaining API data in an embodiment of the present invention;

[0048] Figure 5 The diagram shown is a schematic diagram of a preferred embodiment of the present invention;

[0049] Figure 6 The diagram shown is a schematic of an apparatus for generating API documentation in an embodiment of the present invention;

[0050] Figure 7 The diagram shown is an internal structural diagram of a computer device in an embodiment of the present invention. Detailed Implementation

[0051] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0052] Figure 1 This is an application environment diagram of the method for generating API documentation in an embodiment of the present invention. (Refer to...) Figure 1 This method for generating API documentation is applied to a system for generating API documentation. The system includes a terminal 110 and a server 120. The terminal 110 and server 120 are connected via a network. The terminal 110 can be a desktop terminal or a mobile terminal; a mobile terminal can be at least one of a mobile phone, tablet, or laptop. The server 120 can be a standalone server or a server cluster consisting of multiple servers.

[0053] Combination Figure 2 As shown, this embodiment provides a method for generating API documentation, including:

[0054] Step 210: Obtain the original code annotations;

[0055] Step 220: Add API annotations to the original code annotations to obtain target code annotations;

[0056] Step 230: Determine whether the target code annotation enters the annotation processor based on the annotation level of the target code annotation;

[0057] Step 240: If the target code annotation enters the annotation processor, then process the target code annotation that has entered the annotation processor to obtain API data;

[0058] Step 250: Summarize the API data to generate API documentation.

[0059] Specifically, the process involves obtaining raw code annotations, which serve as auxiliary descriptions of the program code. API annotations are then added to these raw code annotations, resulting in target code annotations. The target code annotations are compiled, and their annotation level is used to determine whether they should enter the annotation processor. If they do, the API annotations are matched against and relevant elements are obtained sequentially. API parsing and data structure assembly are then performed to acquire API data. Finally, the API data is aggregated to generate API documentation.

[0060] In some embodiments, the original code annotations are dynamically parsed using Java's built-in Swagger technology. Swagger is a specification and complete framework for generating, describing, invoking, and visualizing web services. The overall goal is to ensure that the client and the file system update at the same speed as the server. The methods, parameters, and models of the file are tightly integrated into the server-side code, allowing the API to always stay in sync. Swagger technology generates corresponding API documentation based on Spring Web MVC, coupling the API documentation data into the project. When the program changes, its parsed content also changes accordingly, existing simultaneously with the running system. The annotation processor is a custom annotation parser. The annotation processor processes the target code annotations entering the annotation processor, matching the target code annotations, using a traversal method to obtain code elements and annotation elements for parsing and extracting relevant data, and assembling the data structure.

[0061] The method for generating API documentation described in this embodiment adds API annotations to the original code annotations to obtain target code annotations. Annotation levels ensure that related annotations only appear in the annotation processor, guaranteeing that the runtime program is not interfered with by related annotations. If a target code annotation enters the annotation processor, it is processed according to the API annotation, obtaining API data. The annotation processor then generates API data, which is finally aggregated and packaged to generate API documentation. In this invention, the API data is generated by the target code annotations, which are obtained by adding API annotations to the original code annotations. These target code annotations are not coupled into the program composed of code; therefore, the generated API data is also not coupled into the program. This separation of API documentation and program effectively reduces the risk of API data leakage, thereby reducing the risk of system information leakage.

[0062] In this embodiment of the invention, the annotation types of the original code annotations include control type annotations, service type annotations, and method parameter type annotations. The API annotations include a first category, a second category, and a third category. The first category of the API annotations is used to mark the control type annotations of the original code annotations, the second category of the API annotations is used to mark the service type annotations of the original code annotations, and the third category of the API annotations is used to mark the method parameter type annotations of the original code annotations.

[0063] The step of adding API annotations to the original code annotations to obtain target code annotations includes:

[0064] Determine the annotation type of the original code annotation;

[0065] Based on the annotation type of the original code annotation, the corresponding category of the API annotation is added to the original code annotation to obtain the target code annotation.

[0066] Specifically, the first category of API annotations, `@ApiDoc`, is at the class level, corresponding to the control type annotations in the original code annotations, namely `@Controller` and `@RestController` annotations in Spring WebMvc. The second category of API annotations, `@ApiService`, is at the method level, applied to REST services, corresponding to the service type annotations in the original code annotations, namely the `@RequestMapping` annotation added to methods in Spring WebMvc. The third category of API annotations, `@ApiParam`, is at the parameter level, applied to parameters in the method body. It corresponds to the method parameter type annotations in the original code annotations, namely `@RequestParam`, `@RequestBody`, etc., in Spring WebMvc.

[0067] In some embodiments, the API annotations are stored as simple Java objects in the form of JSON strings, forming structured interface information. Organizing and storing them as JSON strings makes deserialization into simple Java objects very convenient. An example of the API annotation is shown below:

[0068] @Target({ElementType.METHOD,ElementType.TYPE,ElementType.ANNOTATION_TYPE})

[0069] @Documented

[0070] @Retention(RetentionPolicy.SOURCE)

[0071] @Inherited

[0072] public@interface ApiDoc{

[0073] int groupId() defaults to 0;

[0074] String desc() defaule "";

[0075] }

[0076] @Target({ElementType.METHQD})

[0077] @Documented

[0078] @Retention(RetentionPolicy.SOURCE)

[0079] @Inherited

[0080] public@interface ApiService{

[0081] int groupId() defaults to 0;

[0082] String desc() defaule "";

[0083] }

[0084] @Target({ElementType.PARAMETER})

[0085] @Documented

[0086] @Retention(RetentionPolicy.SOURCE)

[0087] @Inherited

[0088] public@interface ApiParam{

[0089] String desc() defaule "";

[0090] String daraType()default "string";

[0091] intlength()default-1;

[0092] }

[0093] In some embodiments, the target code annotation example is shown below:

[0094] @ApiDoc(groupId=1, desc="XXX series of services")

[0095] @RestController

[0096] public class DemoController {

[0097] @Apiservice(groupId=1, desc="service_name")

[0098] @GetMapping(“ / hello”)

[0099] @ResponseBody

[0100] PublicString hello(

[0101] @ApiParam(desc="parameter_name", dataType="string", length=10)

[0102] @RequestParamStringa,

[0103] @ApiParam(desc="newspaper type", dateType="obj")

[0104] @RequestBodyBodybody)

[0105] {

[0106] System.out.println(“a="+a);

[0107] System.out.println(“body="+body);

[0108] Return "helloworld!";

[0109] }

[0110] The method for generating API documentation in this embodiment involves defining API annotations and adding API annotations to the original code annotations to obtain target code annotations. The API annotations are then linked to the original code annotations, and the required data is retrieved from the original code annotations using the custom API annotations.

[0111] In this embodiment of the invention, if the target code annotation enters the annotation processor, processing the target code annotation entering the annotation processor to obtain API data includes:

[0112] Extract multiple first elements marked by the first category from the target code annotations that enter the annotation processor, and sequentially determine whether the first element contains the control type annotation, wherein the first element includes multiple sub-elements;

[0113] If the control type annotation is not present, then retrieve the next first element.

[0114] If the control type annotation is included, then extract the service data of the first category and the control type annotation;

[0115] Extract the sub-elements of the method class from the first element, and obtain the second element based on the sub-elements of the method class;

[0116] The method attribute data and parameter attribute data are obtained based on the second element;

[0117] API data is obtained based on the service data, the method attribute data, and the parameter attribute data.

[0118] Specifically, in combination Figure 3 As shown, embodiments of the present invention also include:

[0119] Step 310: Start compiling and enter the Processor interface. Obtain the first element marked with the first category @ApiDoc in the API annotation through the Processor interface. The unmarked elements are other class code, which are unrelated to the interface service and can be ignored.

[0120] Step 320: Determine whether the first element contains the control type annotation @Controller or @RestController. If not, it means that it is not a SpringRest service and can be left unparsed. If it exists, proceed to the next step to extract data.

[0121] Step 330: Extract the control type annotations @Controller and @RestController, as well as the data of the first category @ApiDoc, and summarize them into service data;

[0122] Step 340: Iterate through all child elements of this element. At this point, the child elements will contain all structs, methods, and variables of this class. Filter out other types and select only the elements of type Method as the second element.

[0123] Step 350: Obtain method attribute data and parameter attribute data based on the second element, and obtain API data based on the service data, the method attribute data, and the parameter attribute data.

[0124] The method for generating API documentation in this embodiment extracts a first element marked with a first category from the target code annotations entering the annotation processor, then determines whether the first element contains the control type annotation and whether the first element is related to interface data, thereby extracting service data. Next, it judges the sub-elements of the first element, selecting the method class sub-element as the second element, and uses the second element to extract method attribute data and parameter attribute data. The service data, method attribute data, and parameter attribute data are then aggregated to obtain API data. Finally, the annotation processor parses the API annotations, categorizes and obtains the data, resulting in complete API data.

[0125] In this embodiment of the invention, obtaining method attribute data and parameter attribute data based on the second element includes:

[0126] Determine whether the second element contains the service type annotation.

[0127] If the service type annotation is included, then extract the self-attribute data of the second category and the second element, and the method attribute data of the service type annotation;

[0128] The second element parameter is converted to obtain the third element;

[0129] The parameter attribute data is obtained based on the self-parameter data of the third element and the method parameter type annotation.

[0130] Specifically, in combination Figure 4 As shown, embodiments of the present invention also include:

[0131] Step 410: Determine whether the second element contains the service type annotations such as @RequestMapping, @GetMapping, @PostMapping, etc. If not, it may be other internal methods or static methods, and is not a mapped service, so it can be ignored. If it exists, proceed to the next step to extract data.

[0132] Step 420: Extract the annotation data of the service type annotations such as @RequestMapping, @GetMapping, @PostMapping and the second category @ApiService data, and extract the attribute data of the second element itself (method name, element type, return value type, exception definition, etc.) to obtain the method attribute data;

[0133] Step 430: Iterate through all input and output parameters of the second element, obtain its parameter list through type conversion, and convert the parameters of the second element to obtain the third element;

[0134] Step 440: Extract the attribute data of the third element itself (parameter name, parameter type, etc.), extract the data of the runtime annotations such as @RequestParam, @Length, and @NotNull of the method parameter type (parameter name, validation length, whether it is not null, etc.), and if it has the third category @ApiParam, extract the annotation data (parameter description, parameter type, etc.) therein, and summarize to obtain the parameter attribute data.

[0135] The method for generating API documentation in this embodiment determines whether the second element contains a service type annotation, processes the second element to obtain method attribute data, and then converts the second element into the third element to obtain parameter attribute data through parameter conversion, thereby obtaining relevant data more comprehensively.

[0136] In this embodiment of the invention, the annotation levels include source code level, class level, and runtime level;

[0137] The step of determining whether the target code annotation enters the annotation processor based on the annotation level of the target code annotation includes:

[0138] When the annotation level is the source code level, the target code annotation enters the annotation processor;

[0139] When the annotation level is the class level or the runtime level, the target code annotation does not enter the annotation processor.

[0140] Specifically, Java annotations, also known as metadata, are code-level descriptions. Introduced in JDK 1.5 and later, they exist at the same level as classes, interfaces, and enumerations. They can be declared before packages, classes, fields, methods, local variables, method parameters, etc., to describe these elements. A key attribute of Java annotations is the RetentionPolicy. This retention policy dictates that when the annotation level is source code level (marked as SOURCE), the target code annotation enters the annotation processor; when the annotation level is class level (marked as CLASS), or when the annotation level is runtime level (marked as RUNTIME), the target code annotation does not enter the annotation processor.

[0141] The method for generating API documentation in this embodiment defines source code level, class level, and runtime level annotation levels, inserting tags for processing by a certain tool into the source code. This separates the code used to generate API documentation from the source code used to run the program at compile time, avoiding the need to package and distribute API documentation and source code separately.

[0142] In this embodiment of the invention, the step of not allowing the target code annotation to enter the annotation processor when the annotation level is the class level or the runtime level includes:

[0143] When the annotation level is the class level, the target code annotation does not enter the annotation processor, but enters the source file and is not loaded or run.

[0144] When the annotation level is the runtime level, the target code annotation does not enter the annotation processor, but enters the source file and is loaded and executed, wherein the source file is a compiled bytecode file.

[0145] Specifically, the retention strategy specifies the compiler and virtual machine's strategy for handling annotations during the compilation and runtime phases. When the annotation level is at the class level, the target code annotation does not enter the annotation processor but is compiled into the class file, but is not discarded during class loading. This is useful in bytecode file processing, such as bytecode instrumentation. This method is used by default for annotations. When the annotation level is at the runtime level, the target code annotation does not enter the annotation processor but is compiled into the class file and loaded by the virtual machine. It is never discarded and is retained at runtime, allowing reflection to be used to read its information. The virtual machine is the Java Virtual Machine (JVM), which has a complete hardware architecture, such as a processor, stack, registers, and a corresponding instruction set. The JVM shields information related to specific operating system platforms, allowing Java programs to run on multiple platforms without modification simply by generating bytecode that runs on the JVM.

[0146] The method for generating API documentation in this embodiment discards class-level code annotations when the class is recorded, depending on their purpose, while retaining runtime-level code annotations that are never discarded by the virtual machine. This reasonable discarding allows the program to run while reducing runtime memory usage.

[0147] In this embodiment of the invention, the annotation level of the API annotation is the source code level.

[0148] In the method for generating API documentation in this embodiment, the annotation level of all API annotations is defined as source code level, i.e., SOURCE level. This ensures that API annotations only appear before code compilation, and the compiled class file does not contain this related annotation and comment, thus ensuring that the runtime program is completely unaffected by API annotations.

[0149] In some preferred embodiments, combined with Figure 5 As shown, embodiments of the present invention also include:

[0150] Step 510: Developers write annotations and use the relevant SOURCE-level API annotations defined in this method to annotate interface classes, methods, parameters, etc.

[0151] Step 520: Compile the code and annotations according to the annotation level in the retention strategy, namely source code level, class level and runtime level, and determine whether the target code annotation enters the annotation processor;

[0152] Step 531: Trigger parsing of the API annotations through the annotation processor, dynamically generate relevant API data in combination with the current API interface code, summarize them into an API file and store it in a specified location.

[0153] Step 532: Send the API documentation to the client and provide the user with a visual API documentation using a user box, wherein the user box is constructed from HTML code;

[0154] Step 541: Generate the class file of the code, package it, send the packaged file to the virtual machine, store it in the class loader, and load it into memory.

[0155] In some preferred embodiments, using Apache Maven (a project management and automated build tool) as an example, the annotation processor is built so that its related content is not packaged into a JAR file. When referencing it, the scope needs to be declared as a provider so that Maven will not include it and its associated dependencies during packaging. Alternatively, the resolver and its dependencies can be packaged together into a fat JAR, declared as a provider in the scope, and added to the spring-boot-maven-plugin (Maven packaging plugin) to exclude it. Alternatively, the annotation processor can be packaged as a maven-plugin and triggered during the compilation phase as a Maven plugin. Additionally, to trigger the annotation processor, the correct resolver path must be configured in the maven-compiler-plugin.

[0156] The method for generating API documentation described in this invention adds API annotations to the original code annotations to obtain target code annotations. Annotation levels ensure that related annotations only appear in the annotation processor, guaranteeing that the runtime program is not interfered with by related annotations. If a target code annotation enters the annotation processor, it is processed according to the API annotation, obtaining API data. The annotation processor then generates API data, which is finally aggregated and packaged to generate API documentation. In this invention, the API data is generated by the target code annotations, which are obtained by adding API annotations to the original code annotations. These target code annotations are not coupled into the program composed of code; therefore, the generated API data is also not coupled into the program. This separation of API documentation and program effectively reduces the risk of API data leakage, thereby reducing the risk of system information leakage.

[0157] Corresponding to the above-described method for generating API documentation, this embodiment of the invention also provides an apparatus for generating API documentation. Figure 6 The diagram shown is a schematic representation of an apparatus for generating API documentation according to an embodiment of the present invention. Figure 6 As shown, the apparatus for generating API documentation includes:

[0158] The first acquisition unit 610 is used to acquire the original code annotations;

[0159] Annotation unit 620, the annotation unit is used to add API annotations to the original code annotations to obtain target code annotations;

[0160] The judgment unit 630 is used to determine whether the target code annotation enters the annotation processor based on the annotation level of the target code annotation;

[0161] The second acquisition unit 640 is used to process the target code annotation that has entered the annotation processor and acquire API data if the target code annotation enters the annotation processor.

[0162] The generation unit 650 is used to summarize the API data and generate API documentation.

[0163] In this embodiment of the invention, the determining unit 630 is further configured to: determine the annotation type of the original code annotation, which includes control type annotation, service type annotation, and method parameter type annotation; and specify that the API annotation includes a first category, a second category, and a third category. The first category of the API annotation is used to mark the control type annotation of the original code annotation, the second category of the API annotation is used to mark the service type annotation of the original code annotation, and the third category of the API annotation is used to mark the method parameter type annotation of the original code annotation. The unit further defines the determination of the annotation type of the original code annotation and adds the corresponding category of the API annotation to the original code annotation based on the annotation type, thereby obtaining the target code annotation.

[0164] The second acquisition unit 640 is further configured to extract multiple first elements marked by the first category in the target code annotation entering the annotation processor, and sequentially determine whether the first element contains the control type annotation, wherein the first element includes multiple sub-elements;

[0165] If the control type annotation is not present, then retrieve the next first element.

[0166] If the control type annotation is included, then extract the service data of the first category and the control type annotation;

[0167] Extract the sub-elements of the method class from the first element, and obtain the second element based on the sub-elements of the method class;

[0168] The method attribute data and parameter attribute data are obtained based on the second element;

[0169] API data is obtained based on the service data, the method attribute data, and the parameter attribute data.

[0170] The second acquisition unit 640 is further configured to determine whether the second element contains the service type annotation.

[0171] If the service type annotation is included, then extract the self-attribute data of the second category and the second element, and the method attribute data of the service type annotation;

[0172] The second element parameter is converted to obtain the third element;

[0173] The parameter attribute data is obtained based on the self-parameter data of the third element and the method parameter type annotation.

[0174] The judgment unit 630 is further configured to include source code level, class level and runtime level in the annotation level; when the annotation level is the source code level, the target code annotation enters the annotation processor;

[0175] When the annotation level is the class level or the runtime level, the target code annotation does not enter the annotation processor.

[0176] The judgment unit 630 is further configured to, when the annotation level is the class level, not enter the annotation processor, enter the source file, and not load and run;

[0177] When the annotation level is the runtime level, the target code annotation does not enter the annotation processor, but enters the source file and is loaded and executed, wherein the source file is a compiled bytecode file.

[0178] The apparatus for generating API documentation according to embodiments of the present invention adds API annotations to original code annotations to obtain target code annotations. Annotation levels ensure that related annotations only appear in the annotation processor, guaranteeing that the runtime program is not interfered with by related annotations. If a target code annotation enters the annotation processor, the API annotation is processed according to the API annotation processing, API data is obtained, API data is generated through the annotation processor, and finally, the API data is aggregated and packaged to generate API documentation. In this invention, the API data is generated by the target code annotations, which are obtained by adding API annotations to the original code annotations. These target code annotations are not coupled into the program composed of code; therefore, the generated API data is also not coupled into the program. That is, the API documentation and the program are separated, effectively reducing the risk of API data leakage, thereby reducing the risk of system information leakage.

[0179] Figure 7 An internal structural diagram of a computer device in one embodiment is shown. Specifically, this computer device may be... Figure 1 Terminal 110 (or server 120) in the middle. For example... Figure 7As shown, the computer device includes a processor, memory, network interface, input device, and display screen connected via a system bus. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores an operating system and may also store computer programs. When executed by the processor, these programs enable the processor to implement dynamic graphics generation methods. The internal memory may also store computer programs, which, when executed by the processor, enable the processor to perform dynamic graphics generation methods. The display screen can be an LCD screen or an e-ink screen. The input device can be a touch layer covering the display screen, buttons, a trackball, or a touchpad mounted on the computer device's casing, or an external keyboard, touchpad, or mouse.

[0180] Those skilled in the art will understand that Figure 7 The structures shown are merely block diagrams of some structures related to the present invention and do not constitute a limitation on the computer devices to which the present invention is applied. Specific computer devices may include more or fewer components than those shown in the figures, or combine certain components, or have different component arrangements.

[0181] Another embodiment of the present invention provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it performs the following steps: obtaining original code annotations; adding API annotations to the original code annotations to obtain target code annotations; determining whether the target code annotations enter the annotation processor based on the annotation level of the target code annotations; if the target code annotations enter the annotation processor, processing the target code annotations that have entered the annotation processor to obtain API data; and summarizing the API data to generate API documentation.

[0182] In another embodiment of the present invention, the processor also implements the step of the method for generating API documentation when executing the computer program.

[0183] The computer device and the method for generating API documentation described in this invention have the same advantages over the prior art, and will not be repeated here.

[0184] Another embodiment of the present invention provides a computer-readable storage medium storing a computer program thereon. When the computer program is executed by a processor, it performs the following steps: obtaining original code annotations; adding API annotations to the original code annotations to obtain target code annotations; determining whether the target code annotations enter the annotation processor based on the annotation level of the target code annotations; if the target code annotations enter the annotation processor, processing the target code annotations that have entered the annotation processor to obtain API data; and summarizing the API data to generate API documentation.

[0185] In another embodiment of the present invention, when the computer program is executed by a processor, it also implements the steps of the method for generating API documentation.

[0186] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, storage, databases, or other media used in the embodiments provided by this invention can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), RAMbus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and RAMbus dynamic RAM (RDRAM), etc.

[0187] The computer-readable storage medium described in this invention and the method for generating API documentation have the same advantages over the prior art, and will not be repeated here.

[0188] It should be noted that, in this document, relational terms such as "first" and "second" are used merely to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0189] The above description is merely a specific embodiment of the present invention, enabling those skilled in the art to understand or implement the invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the invention. Therefore, the present invention is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features of the invention herein.

Claims

1. A method for generating API documentation, characterized in that, include: Get the original code annotations; API annotations are added to the original code annotations to obtain target code annotations. The annotation types of the original code annotations include control type annotations, service type annotations, and method parameter type annotations. The API annotations include a first category, a second category, and a third category. The first category of the API annotations is used to mark the control type annotations of the original code annotations. The second category of the API annotations is used to mark the service type annotations of the original code annotations. The third category of the API annotations is used to mark the method parameter type annotations of the original code annotations. The step of adding API annotations to the original code annotations to obtain target code annotations includes: Determine the annotation type of the original code annotation; Based on the annotation type of the original code annotation, the corresponding category of the API annotation is added to the original code annotation to obtain the target code annotation; Based on the annotation level of the target code annotation, determine whether the target code annotation enters the annotation processor; If the target code annotation enters the annotation processor, the target code annotation entering the annotation processor is processed to obtain API data, including: Extract multiple first elements marked by the first category from the target code annotations that enter the annotation processor, and sequentially determine whether the first element contains the control type annotation, wherein the first element includes multiple sub-elements; If the control type annotation is included, then extract the service data of the first category and the control type annotation; Extract the sub-elements of the method class from the first element, and obtain the second element based on the sub-elements of the method class; The method attribute data and parameter attribute data are obtained based on the second element; The API data is obtained based on the service data, the method attribute data, and the parameter attribute data; The API data is then aggregated to generate API documentation.

2. The method for generating API documentation according to claim 1, characterized in that, The step of obtaining method attribute data and parameter attribute data based on the second element includes: Determine whether the second element contains the service type annotation. If the service type annotation is included, then extract the self-attribute data of the second category and the second element, and the method attribute data of the service type annotation; The second element parameter is converted to obtain the third element; The parameter attribute data is obtained based on the self-parameter data of the third element and the method parameter type annotation.

3. The method for generating API documentation according to claim 1, characterized in that, The annotation levels include source code level, class level, and runtime level; The step of determining whether the target code annotation enters the annotation processor based on the annotation level of the target code annotation includes: When the annotation level is the source code level, the target code annotation enters the annotation processor; When the annotation level is the class level or the runtime level, the target code annotation does not enter the annotation processor.

4. The method for generating API documentation according to claim 3, characterized in that, When the annotation level is the class level or the runtime level, the target code annotation does not enter the annotation processor, including: When the annotation level is the class level, the target code annotation does not enter the annotation processor, but enters the source file and is not loaded or run. When the annotation level is the runtime level, the target code annotation does not enter the annotation processor, but enters the source file and is loaded and executed, wherein the source file is a compiled bytecode file.

5. The method for generating API documentation according to claim 4, characterized in that, The annotation level of the API annotations is the source code level.

6. An apparatus for generating API documentation, characterized in that, include: The first acquisition unit is used to acquire the original code annotations; An annotation unit is used to add API annotations to the original code annotations to obtain target code annotations. The annotation types of the original code annotations include control type annotations, service type annotations, and method parameter type annotations. The API annotations include a first category, a second category, and a third category. The first category of the API annotations is used to mark the control type annotations of the original code annotations. The second category of the API annotations is used to mark the service type annotations of the original code annotations. The third category of the API annotations is used to mark the method parameter type annotations of the original code annotations. The step of adding API annotations to the original code annotations to obtain target code annotations includes: Determine the annotation type of the original code annotation; Based on the annotation type of the original code annotation, the corresponding category of the API annotation is added to the original code annotation to obtain the target code annotation; The judgment unit is used to determine whether the target code annotation enters the annotation processor based on the annotation level of the target code annotation; The second acquisition unit is configured to process the target code annotation entering the annotation processor and acquire API data if the target code annotation enters the annotation processor, including: Extract multiple first elements marked by the first category from the target code annotations that enter the annotation processor, and sequentially determine whether the first element contains the control type annotation, wherein the first element includes multiple sub-elements; If the control type annotation is included, then extract the service data of the first category and the control type annotation; Extract the sub-elements of the method class from the first element, and obtain the second element based on the sub-elements of the method class; The method attribute data and parameter attribute data are obtained based on the second element; The API data is obtained based on the service data, the method attribute data, and the parameter attribute data; A generation unit is used to aggregate the API data to generate API documentation.

7. A computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the method for generating API documentation as described in any one of claims 1 to 5.

8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when read and executed by a processor, implements the method for generating API documentation as described in any one of claims 1-5.