CN115981723A - Method and device for generating API (application program interface) document, computer equipment and storage medium - Google Patents

Method and device for generating API (application program interface) document, computer equipment and storage medium Download PDF

Info

Publication number
CN115981723A
CN115981723A CN202211643056.XA CN202211643056A CN115981723A CN 115981723 A CN115981723 A CN 115981723A CN 202211643056 A CN202211643056 A CN 202211643056A CN 115981723 A CN115981723 A CN 115981723A
Authority
CN
China
Prior art keywords
annotation
api
processor
level
code
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202211643056.XA
Other languages
Chinese (zh)
Inventor
杨军
苏彭
张研
邵慧
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Anhui Aisino Corp
Original Assignee
Anhui Aisino Corp
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Anhui Aisino Corp filed Critical Anhui Aisino Corp
Priority to CN202211643056.XA priority Critical patent/CN115981723A/en
Publication of CN115981723A publication Critical patent/CN115981723A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

  • Document Processing Apparatus (AREA)

Abstract

The invention provides a method, a device, computer equipment and a storage medium for generating an API document, which relate to the technical field of software development and comprise the following steps: acquiring an original code annotation; adding API annotation to the original code annotation to obtain a target code annotation; judging whether the target code annotation enters an annotation processor or not 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 acquire API data; and summarizing the API data to generate an API document. The invention realizes that the API document is not released along with the project, thereby reducing the risk of system information leakage.

Description

Method and device for generating API (application program interface) document, computer equipment and storage medium
Technical Field
The invention relates to the technical field of software development, in particular to a method and a device for generating an API (application program interface) document, computer equipment and a storage medium.
Background
Java is an existing high-level programming language for writing programs with portability, in which systems and applications communicate through APIs. Where the API is a predefined function and the API document is a technical content delivery file for the API, containing instructions on how to efficiently use and integrate the API. It contains all the information needed to use the API, detailing functions, classes, return types, parameters, etc., which is important for interpretation of the API.
The current writing of API documents is largely divided into manual writing and generation of API documents by tools. The manual writing is an API document which is manually written by using a MarkDown grammar or other Html styles, but the problems of increased workload, asynchronous and untimely code and document updating and the like can be caused. The API document generated by the tool mostly writes the related information together with the code, and then the related information is extracted and analyzed by the specific tool. The technology is mainly realized through a Java self-contained swagger technology, and the swagger technology can automatically scan and analyze codes and annotations when a project is started to generate API document data, and then the API document data is viewed by a user through an interface. But introducing swagger at the same time needs to refer to the related class library, and the related class library is also packaged when the production package is generated, and the API document data is coupled in the program and is published along with the program. After other people acquire the API document, the relevant interface information can be checked through the API document, and relevant comments in the code are acquired by simple decompilation, so that the risk of system source code leakage is increased.
Disclosure of Invention
The invention solves the problem that the API document increases the leakage risk of the system source code along with the release of the project.
In order to solve the above problem, the present invention provides a method for generating an API document, comprising:
acquiring an original code annotation; adding API annotation to the original code annotation to obtain a target code annotation; judging whether the target code annotation enters an annotation processor or not according to the annotation level of the target code annotation; if the object code annotation enters the annotation processor, processing the object code annotation entering the annotation processor to obtain API data; and summarizing the API data to generate an API document.
Optionally, the annotation types of the original code annotation include a control type annotation, a service type annotation and a method parameter type annotation, the API annotation includes a first category, a second category and a third category, the first category of the API annotation is used for marking the control type annotation of the original code annotation, the second category of the API annotation is used for marking the service type annotation of the original code annotation, and the third category of the API annotation is used for marking the method parameter type annotation of the original code annotation;
adding an API annotation to the original code annotation to obtain a target code annotation, wherein the API annotation comprises:
judging the annotation type of the original code annotation;
and adding the API annotation of the corresponding category to the original code annotation according to the annotation type of the original code annotation to obtain the target code annotation.
Optionally, if the target code annotation enters the annotation processor, processing the target code annotation entering the annotation processor to obtain API data, including:
extracting a plurality of first elements marked by the first category in the target code annotation entering the annotation processor, and sequentially judging whether the first elements contain the control type annotation, wherein the first elements comprise a plurality of sub-elements;
if the control type annotation is not included, retrieving a next one of the first elements,
if the control type annotation is contained, extracting service data of the first category and the control type annotation;
extracting sub-elements which are method classes in the first elements, and acquiring second elements according to the sub-elements of the method classes;
acquiring method attribute data and parameter attribute data according to the second element;
and acquiring API data according to the service data, the method attribute data and the parameter attribute data.
Optionally, the obtaining of the method attribute data and the parameter attribute data according to the second element includes:
determining whether the second element contains the service type annotation,
if the service type annotation is contained, extracting the second category, the self attribute data of the second element and the method attribute data of the service type annotation;
converting the second element parameter to obtain a third element;
and acquiring the parameter attribute data according to the self parameter data of the third element and the method parameter type annotation.
Optionally, the annotation levels include a source code level, a class level, and a run level;
the judging whether the target code annotation enters an annotation processor according to the annotation level of the target code annotation comprises the following steps:
when the annotation level is the source code level, the object code annotation enters the annotation processor;
when the annotation level is the class level or run level, then the object code annotation does not enter the annotation processor.
Optionally, when the annotation level is the class level or the run level, then the object code annotation does not enter the annotation processor, including;
when the annotation level is the class level, the object code annotation does not enter the annotation processor, enters a source file and is not loaded and run;
and when the annotation level is the operation level, the object code annotation does not enter the annotation processor, enters a source file, and is loaded and operated, wherein the source file is a compiled byte code file.
Optionally, the annotation levels of the API annotation are all the source code levels.
The method for generating the API document obtains the target code annotation by adding the API annotation to the original code annotation, and ensures that the program in operation is not interfered by the relevant annotation by making the relevant annotation only appear in the annotation processor through the annotation level. And if the target code annotation enters the annotation processor, processing the target code annotation entering the annotation processor according to the API annotation to obtain the API data, generating the API data through the annotation processor, and finally summarizing and packaging the API data to generate the API document. In the invention, the target code annotation is generated by adding the API annotation to the original code annotation, and the target code annotation is not coupled into the program formed by the codes, so that the generated API data is not coupled into the program, namely, the API document is separated from the program, thereby effectively reducing the risk of API data leakage and further reducing the risk of system source code leakage.
The invention also provides a device for generating the API document, which comprises: a first obtaining unit for obtaining an original code annotation;
the annotation unit is used for adding API annotation to the original code annotation to obtain a target code annotation;
the judging unit is used for judging whether the target code annotation enters an annotation processor or not according to the annotation level of the target code annotation;
the second acquisition unit is used for processing the target code annotation entering the annotation processor and acquiring API data if the target code annotation enters the annotation processor;
and the generating unit is used for summarizing the API data to generate an API document.
Compared with the prior art, the device for generating the API document and the method for generating the API document have the same advantages, and are not described again here.
The invention also provides a computer device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, the processor implementing the steps of the method of generating an API document when executing the computer program.
The computer device and the method for generating the API document have the same advantages compared with the prior art, and are not described herein again.
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 of generating an API document.
The advantages of the computer-readable storage medium and the method for generating the API document of the present invention over the prior art are the same, and are not described herein again.
Drawings
The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the invention and together with the description, serve to explain the principles of the invention.
In order to more clearly illustrate the embodiments or technical solutions in the prior art of the present invention, the drawings used in the description of the embodiments or the prior art will be briefly described below, and it is obvious for those skilled in the art to obtain other drawings without inventive labor.
FIG. 1 is a diagram illustrating an application environment of a method for generating an API document in an embodiment of the present invention;
FIG. 2 is a diagram illustrating a method of generating an API document in an embodiment of the present invention;
FIG. 3 is a first diagram illustrating obtaining API data according to an embodiment of the present invention;
FIG. 4 is a second diagram illustrating obtaining API data according to an embodiment of the present invention;
FIG. 5 is a schematic diagram of a preferred embodiment of the present invention;
FIG. 6 is a diagram illustrating an apparatus for generating an API document in an embodiment of the present invention;
fig. 7 is a diagram showing an internal structure of a computer apparatus in the embodiment of the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, but not all, embodiments of the present invention. All other embodiments, which can be obtained by a person skilled in the art without any inventive step based on the embodiments of the present invention, are within the scope of the present invention.
FIG. 1 is a diagram of an application environment of a method for generating an API document in an embodiment of the present invention. Referring to fig. 1, the method of generating an API document is applied to a system for generating an API document. The system for generating the API document includes a terminal 110 and a server 120. The terminal 110 and the server 120 are connected through a network. The terminal 110 may specifically be a desktop terminal or a mobile terminal, and the mobile terminal may specifically be at least one of a mobile phone, a tablet computer, a notebook computer, and the like. The server 120 may be implemented as a stand-alone server or a server cluster composed of a plurality of servers.
As shown in fig. 2, the present embodiment provides a method for generating an API document, including:
step 210, obtaining an original code annotation;
step 220, adding API annotation to the original code annotation to obtain a target code annotation;
step 230, judging whether the target code annotation enters an annotation processor according to the annotation level of the target code annotation;
step 240, if the target code annotation enters the annotation processor, processing the target code annotation entering the annotation processor to obtain API data;
and step 250, summarizing the API data to generate an API document.
Specifically, an original code annotation is obtained, which is an auxiliary representation for the program code. And adding an API note to the original code note according to the original code note, and obtaining a target code note in a self-defined API note mode. Compiling the object code annotation, and judging whether the object code annotation enters an annotation processor or not according to the annotation level of the object code annotation. And if the target code annotation enters the annotation processor, sequentially matching and obtaining related elements according to the API annotation, performing API analysis and data structure assembly, and obtaining API data. And summarizing the API data to generate an API document.
In some embodiments, the original code annotation is obtained by dynamic parsing through java-owned swagger technology, which is a canonical and complete framework for generating, describing, invoking and visualizing Web services. The overall goal is to have the client and file system update as a server at the same rate. The methods, parameters, and models of the files are tightly integrated into the server-side code, allowing the API to keep the synchronization at all times. The swagger technology generates a corresponding API document based on spring-webmvc, couples the API document data into a project, and when a program changes, the analyzed content also changes in response to the change, and exists with a running system. The annotation processor is a self-defined annotation parser, processes the target code annotation entering the annotation processor, obtains code elements and annotation elements in a traversal mode by matching the target code annotation, parses the code elements and the annotation elements, extracts relevant data and assembles a data structure.
In the method for generating the API document according to this embodiment, the target code annotation is obtained by adding the API annotation to the original code annotation, and the annotation level is used to make the relevant annotation appear only in the annotation processor, so as to ensure that the program in operation is not interfered by the relevant annotation. And if the target code annotation enters the annotation processor, processing the target code annotation entering the annotation processor according to the API annotation to obtain the API data, generating the API data through the annotation processor, and finally summarizing and packaging the API data to generate the API document. In the invention, the target code annotation is generated by adding the API annotation to the original code annotation, and the target code annotation is not coupled into the program formed by the codes, so that the generated API data is not coupled into the program, namely, the API document is separated from the program, thereby effectively reducing the risk of API data leakage and further reducing the risk of system information leakage.
In an embodiment of the present invention, the annotation types of the original code annotation include a control type annotation, a service type annotation and a method parameter type annotation, the API annotation includes a first category, a second category and a third category, the first category of the API annotation is used for marking the control type annotation of the original code annotation, the second category of the API annotation is used for marking the service type annotation of the original code annotation, and the third category of the API annotation is used for marking the method parameter type annotation of the original code annotation;
adding an API annotation to the original code annotation to obtain a target code annotation, wherein the API annotation comprises:
judging the annotation type of the original code annotation;
and adding the API annotation of the corresponding category to the original code annotation according to the annotation type of the original code annotation to obtain the target code annotation.
Specifically, the first category @ API is Class (Class) level, and the @ RestController annotation for the control type annotated by the original code is @ Controller in SpringWebMvc. The second category @ API service of the API annotation is a method (method) level, which is applied to the Rest service, and is an annotation of the service type annotated with the original code annotation, i.e., an annotation of the @ RequestMapping type added to the method in SpringWebMvc. The third category of the API annotation, apiParam, is the parameter level, applied to parameters in the recipe body. The annotation of the parameter type of the method for marking the original code annotation is @ RequestParam, @ RequestBody and the like in SpringWebMvc.
In some embodiments, the API annotations are stored in the form of json strings in the form of simple Java objects, forming structured interface information. After being organized and stored in the json string mode, the Java objects are deserialized into simple Java objects, which is very simple and convenient. Examples of the API annotations are as follows:
@Target({ElementType.METHOD,ElementType.TYPE,ElementType.ANNOTATION_TYPE})
@Documented
@Retention(RetentionPolicy.SOURCE)
@Inherited
public@interface ApiDoc{
int groupId()default 0;
String desc()defaule“”;
}
@Target({ElementType.METHQD})
@Documented
@Retention(RetentionPolicy.SOURCE)
@Inherited
public@interface ApiService{
int groupId()default 0;
String desc()defaule“”;
}
@Target({ElementType.PARAMETER})
@Documented
@Retention(RetentionPolicy.SOURCE)
@Inherited
public@interface ApiParam{
String desc()defaule“”;
String daraType()default“string”;
intlength()default-1;
}
in some embodiments, the target code annotation example is as follows:
@ ApiDoc (groupId =1, desc = "a certain series of services")
@RestController
publicclassDemoController{
@ Apiservice (groupId =1, desc = "a certain service")
@GetMapping(“/hello”)
@ResponseBody
PublicStringhello(
@ ApiParam (desc = "certain parameter", dataType = "string", length = 10)
@RequestParamStringa,
@ ApiParam (desc = "some message body", dateType = "obj")
@RequestBodyBodybody)
{
System.out.println(“a=”+a);
System.out.println(“body=”+body);
Return“helloworld!”;
}
The method for generating the API document of this embodiment obtains the target code annotation by defining the API annotation and adding the API annotation to the original code annotation. And associating the API note with the original code note, and acquiring required data from the original code note through the customized API note.
In this embodiment of the present invention, if the target code annotation enters the annotation processor, processing the target code annotation entering the annotation processor to obtain API data includes:
extracting a plurality of first elements marked by the first category in the target code annotation entering the annotation processor, and sequentially judging whether the first elements contain the control type annotation, wherein the first elements comprise a plurality of sub-elements;
if the control type annotation is not included, retrieving a next one of the first elements,
if the control type annotation is contained, extracting service data of the first category and the control type annotation;
extracting sub-elements of a method class in the first elements, and acquiring second elements according to the sub-elements of the method class;
acquiring method attribute data and parameter attribute data according to the second element;
and acquiring API data according to the service data, the method attribute data and the parameter attribute data.
Specifically, as shown in fig. 3, the embodiment of the present invention further includes:
step 310, starting to compile and enter a Processor interface, obtaining a first element marked by the first class @ API of the API annotation through the Processor interface, wherein the first element is not marked by other codes, is irrelevant to interface service and can be ignored;
step 320, judging whether the first element contains the control type annotation @ Controller or @ RestController, if not, indicating that the first element is not a SpringRest service, and if so, entering the next step to extract data;
step 330, extracting the control type annotation @ Controller and @ RestController annotation data and the data of the first type @ api, and summarizing the data into service data;
step 340, traverse all sub-elements of the element, where the sub-elements contain all structures, methods, and variables of the class. Filtering out other types, and only selecting the elements of which the types are the Method types as second elements;
step 350, obtaining method attribute data and parameter attribute data according to the second element, and obtaining API data according to the service data, the method attribute data and the parameter attribute data.
The method for generating the API document of this embodiment extracts the service data by extracting the first element marked by the first category in the target code annotation entered into the annotation processor, determining whether the first element includes the control type annotation, and determining whether the first element is related to the interface data. And judging the sub-elements of the first element, selecting method sub-elements as second elements, and extracting method attribute data and parameter attribute data by using the second elements. And summarizing the service data, the method attribute data and the parameter attribute data to obtain API data. And analyzing the API annotation through an annotation processor, and obtaining data in a classified manner to obtain complete API data.
In an embodiment of the present invention, the obtaining of the method attribute data and the parameter attribute data according to the second element includes:
determining whether the second element contains the service type annotation,
if the service type annotation is contained, extracting the second category, the self attribute data of the second element and the method attribute data of the service type annotation;
converting the second element parameter to obtain a third element;
and acquiring the parameter attribute data according to the self parameter data of the third element and the method parameter type annotation.
Specifically, as shown in fig. 4, the embodiment of the present invention further includes:
step 410, judging whether the second element contains the service type annotation @ RequestMapping, @ GetMapping, @ PostMapping and other series annotations, if not, the second element may be other internal methods or static methods, but not mapped services, and can be ignored, if yes, the next step is carried out to extract data;
step 420, extracting annotation data of the series of annotations such as the service type annotation @ RequestMapping, @ GetMapping, @ PostMapping and the like and the second type @ ApiService data, and extracting attribute data (method name, element type, return value type, abnormal definition and the like) of a second element per se to collect and acquire method attribute data;
step 430, traversing all the parameter-entering and parameter-exiting elements of the second element, obtaining a parameter list of the second element through type conversion, and converting the parameters of the second element to obtain a third element;
step 440, extracting the third element self attribute data (parameter name, parameter type and the like), extracting the data (parameter name, check Length, whether empty or not and the like) annotated in the runtime of the method parameter type annotation @ RequestParam, @ Length, @ NotNull and the like, and if the third type @ ApiParam exists, extracting the annotation data (parameter description, parameter type and the like) in the annotation data, summarizing and obtaining the parameter attribute data.
The method for generating the API document of this embodiment processes the attribute data of the second element obtaining method by determining whether the second element contains the service type annotation, and converts the second element into the third element to obtain the parameter attribute data by parameter conversion, thereby obtaining the relevant data more comprehensively.
In the embodiment of the invention, the annotation level comprises a source code level, a class level and a running level;
the judging whether the object code annotation enters an annotation processor according to the annotation level of the object code annotation comprises:
when the annotation level is the source code level, the object code annotation enters the annotation processor;
when the annotation level is the class level or run level, then the object code annotation does not enter the annotation processor.
In particular, the Java annotation, also called metadata, is a code level specification. It is a feature introduced by JDK1.5 and later versions, at the same level as classes, interfaces, enumerations. It may be declared in front of a packet, class, field, method, local variable, method parameter, etc. to illustrate these elements. The Java annotation has an important attribute of Retentionpolicy. When the annotation level of the reservation strategy is the SOURCE code level, annotating the object code annotation marked as SOURCE level and entering the annotation processor; when the annotation level is the CLASS level, the annotation is marked as a CLASS level annotation, and when the annotation level is the operation level, the annotation is marked as a RUNTIME level annotation, the object code annotation does not enter the annotation processor.
The method for generating the API document of the embodiment inserts a tag for certain tool processing into the source code by defining annotation levels of a source code level, a class level and a running level, thereby separating the code for generating the API document and the source code for running the program during compiling and avoiding packaging and publishing of the API document and the source code.
In this embodiment of the present invention, when the annotation level is the class level or the run level, the target code annotation does not enter the annotation processor, including;
when the annotation level is the class level, the object code annotation does not enter the annotation processor, enters a source file and is not loaded and run;
and when the annotation level is the operation level, the object code annotation does not enter the annotation processor, enters a source file, and is loaded and operated, wherein the source file is a compiled byte code file.
Specifically, a compiler of an annotation at a compile-to-run stage and a policy of a virtual machine for the annotation are specified according to a retention policy, and when the annotation level is the class level, the object code annotation does not enter the annotation processor, and is compiled into a class file, but is not discarded when the object code annotation is loaded by the virtual machine and is class-loaded. Useful in the processing of byte code files, such as byte code instrumentation. Note that this approach is used by default. When the annotation level is the run level, the object code annotation does not enter the annotation processor, is compiled into a class file, is loaded by a virtual machine and is not discarded all the time, and the annotation is also reserved in the run time, so that the information of the annotation can be read by using a reflection mechanism. The virtual machine is a Java virtual machine, which has a perfect hardware structure, such as a processor, a stack, a register, and the like, and also has a corresponding instruction system. The Java virtual machine shields information related to a specific operating system platform, so that Java programs can be run on a variety of platforms without modification by only generating bytecode that runs on the Java virtual machine.
According to the method for generating the API document, the annotation entering the virtual machine is discarded in class recording according to different uses, namely the annotation is discarded by the virtual machine, meanwhile, the code annotation with the annotation level as the operation level is reserved and cannot be discarded by the virtual machine all the time, program operation is realized through reasonable discarding, and meanwhile, the operation memory is reduced.
In the embodiment of the present invention, the annotation levels of the API annotation are all the source code levels.
In the method for generating the API document of the present embodiment, the annotation levels of all API annotations are defined as SOURCE code levels, i.e. SOURCE levels, which ensures that the API annotations only appear before code compilation, and the class file after code compilation does not contain the relevant annotations and comments, thereby ensuring that the runtime program is not interfered by the API annotations at all.
In some preferred embodiments, as shown in fig. 5, the embodiments of the present invention further include:
step 510, the developer writes comments, and writes comments on interface classes, methods, parameters and the like by using the API comments of the relevant SOURCE levels defined by the method;
step 520, compiling the code and the annotation according to the annotation level in the retention strategy, namely the source code level, the class level and the operation level, and judging whether the object code annotation enters the annotation processor or not;
and 531, triggering and analyzing the API annotation by the annotation processor, combining the current API interface code, dynamically generating related API data, summarizing the API data into an API file and storing the API file in a specified position.
Step 532, sending the API document to a client, and providing a visual API document for a user by using a user frame body, wherein the user frame body is constructed by HTML codes;
step 541, generating a class file of the code, packaging the class file, sending the packaged file to the virtual machine, storing the file in a class loader, and loading the file into a memory.
In some preferred embodiments, apache Maven (project management and Automation build tool) is used as an example to build the annotation processor such that annotation processor-related content is not packaged into Jar packages. Scope needs to be declared as provider at the time of reference so maven does not introduce it and associated dependencies at the time of packaging. Or packaging the parser and the dependent package into a fatjar (special software package), then declaring as provider in scope, and adding in spring-boot-Maven-plug-in (Maven package plug-in) to exclude the parser and the dependent package. Or the annotation processor is packaged into a maven-plugin (maven plugin), and the method of the maven plugin is used for triggering in the compiling stage. Meanwhile, in order to trigger the annotation processor, a correct parser path needs to be configured in the maven-compiler-plugin.
The method for generating the API document obtains the target code annotation by adding the API annotation to the original code annotation, and ensures that the program in operation is not interfered by the relevant annotation by making the relevant annotation only appear in the annotation processor through the annotation level. And if the object code annotation enters the annotation processor, processing the object code annotation entering the annotation processor according to the API annotation to obtain the API data, generating the API data through the annotation processor, and finally summarizing and packaging the API data to generate the API document. In the invention, the target code annotation is generated by adding the API annotation to the original code annotation, and the target code annotation is not coupled into the program formed by the codes, so that the generated API data is not coupled into the program, namely, the API document is separated from the program, thereby effectively reducing the risk of API data leakage and further reducing the risk of system information leakage.
Corresponding to the method for generating the API document, the embodiment of the invention also provides a device for generating the API document. Fig. 6 is a schematic diagram of an apparatus for generating an API document according to an embodiment of the present invention, and as shown in fig. 6, the apparatus for generating an API document includes:
a first obtaining unit 610, configured to obtain an original code annotation;
an annotation unit 620, configured to add an API annotation to the original code annotation to obtain a target code annotation;
a judging unit 630, configured to judge whether the target code annotation enters an annotation processor according to an annotation level of the target code annotation;
a second obtaining unit 640, configured to, if the target code annotation enters the annotation processor, process the target code annotation entering the annotation processor, and obtain API data;
a generating unit 650, configured to aggregate the API data to generate an API document.
In an embodiment of the present invention, the determining unit 630 is further configured to use an annotation type of the original code annotation to include a control type annotation, a service type annotation, and a method parameter type annotation, where the API annotation includes a first category, a second category, and a third category, the first category of the API annotation is used for marking the control type annotation of the original code annotation, the second category of the API annotation is used for marking the service type annotation of the original code annotation, and the third category of the API annotation is used for marking the method parameter type annotation of the original code annotation; judging the annotation type of the original code annotation; and adding the API annotation of the corresponding category to the original code annotation according to the annotation type of the original code annotation to obtain the target code annotation.
The second obtaining unit 640 is further configured to extract a plurality of first elements marked by the first category in the target code annotation entering the annotation processor, and sequentially determine whether the first elements include the control type annotation, where the first elements include a plurality of sub-elements;
if the control type annotation is not included, retrieving a next one of the first elements,
if the control type annotation is contained, extracting service data of the first category and the control type annotation;
extracting sub-elements of a method class in the first elements, and acquiring second elements according to the sub-elements of the method class;
acquiring method attribute data and parameter attribute data according to the second element;
and acquiring API data according to the service data, the method attribute data and the parameter attribute data.
The second obtaining unit 640 is further configured to determine whether the second element contains the service type annotation,
if the service type annotation is contained, extracting the second category, the self attribute data of the second element and the method attribute data of the service type annotation;
converting the second element parameters to obtain a third element;
and acquiring the parameter attribute data according to the self parameter data of the third element and the method parameter type annotation.
The judging unit 630 is further configured to determine the annotation level includes a source code level, a class level, and a run level; when the annotation level is the source code level, the object code annotation enters the annotation processor;
when the annotation level is the class level or run level, then the object code annotation does not enter the annotation processor.
The judging 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 the object code annotation;
and when the annotation level is the operation level, the object code annotation does not enter the annotation processor, enters a source file, and is loaded and operated, wherein the source file is a compiled byte code file.
According to the device for generating the API document, the target code annotation is obtained by adding the API annotation to the original code annotation, and the related annotation only appears in the annotation processor through the annotation level, so that the program in operation is not interfered by the related annotation. And if the target code annotation enters the annotation processor, processing the target code annotation entering the annotation processor according to the API annotation to obtain the API data, generating the API data through the annotation processor, and finally summarizing and packaging the API data to generate the API document. In the invention, the target code annotation is generated by adding the API annotation to the original code annotation, and the target code annotation is not coupled into the program formed by the codes, so that the generated API data is not coupled into the program, namely, the API document is separated from the program, thereby effectively reducing the risk of API data leakage and further reducing the risk of system information leakage.
FIG. 7 is a diagram that illustrates an internal structure of the computer device in one embodiment. The computer device may specifically be the terminal 110 (or the server 120) in fig. 1. As shown in fig. 7, the computer apparatus includes a processor, a memory, a network interface, an input device, and a display screen connected through a system bus. Wherein the memory includes a non-volatile storage medium and an internal memory. The non-volatile storage medium of the computer device stores an operating system and may further store a computer program that, when executed by the processor, causes the processor to implement the dynamic graphics generating method. The internal memory may also have a computer program stored therein, which when executed by the processor, causes the processor to perform the dynamic graphics generation method. The display screen of the computer equipment can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer equipment can be a touch layer covered on the display screen, a key, a track ball or a touch pad arranged on the shell of the computer equipment, an external keyboard, a touch pad or a mouse and the like.
Those skilled in the art will appreciate that the architecture shown in FIG. 7 is merely a block diagram of some of the structures associated with the present invention and is not intended to limit the computing devices in which the present invention may be used, and that a particular computing device may include more or less components than those shown, or may have some components combined, or may have a different arrangement of components.
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, wherein the processor implements the following steps when executing the computer program: acquiring an original code annotation; adding API annotation to the original code annotation to obtain a target code annotation; judging whether the target code annotation enters an annotation processor or not 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 acquire API data; and summarizing the API data to generate an API document.
In another embodiment of the present invention, the processor, when executing the computer program, further performs the steps of the method for generating an API document.
The computer device and the method for generating the API document have the same advantages compared with the prior art, and are not described herein again.
Another embodiment of the present invention provides a computer-readable storage medium having a computer program stored thereon, the computer program, when executed by a processor, implementing the steps of: acquiring an original code annotation; adding API annotation to the original code annotation to obtain a target code annotation; judging whether the target code annotation enters an annotation processor or not 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 acquire API data; and summarizing the API data to generate an API document.
In another embodiment of the invention, the computer program, when executed by the processor, further performs the steps of the method of generating an API document.
It will be understood by those skilled in the art that all or part of the processes of the methods of the embodiments described above may be implemented by a computer program, which may be stored in a non-volatile computer readable storage medium, and when executed, may include the processes of the embodiments of the methods described above. Any reference to memory, storage, databases, or other media used in embodiments provided herein may 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 a variety of forms such as Static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double Data Rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous Link DRAM (SLDRAM), rambus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
The advantages of the computer-readable storage medium and the method for generating the API document of the present invention over the prior art are the same, and are not described herein again.
It is noted that, in this document, relational terms such as "first" and "second," and the like, are used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrases "comprising a," "...," or "comprising" does not exclude the presence of additional like elements in a process, method, article, or apparatus that comprises the element.
The foregoing are merely exemplary embodiments of the present invention, which enable those skilled in the art to understand or practice the present invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the generic principles defined herein may be applied to other embodiments without departing from the spirit or scope of the invention. Thus, the present invention is not intended to be limited to the embodiments shown herein but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.

Claims (10)

1. A method of generating an API document, comprising:
acquiring an original code annotation;
adding API annotation to the original code annotation to obtain a target code annotation;
judging whether the target code annotation enters an annotation processor or not 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 acquire API data;
and summarizing the API data to generate an API document.
2. The method of generating an API document of claim 1, wherein annotation types of said original code annotation include a control type annotation, a service type annotation, and a method parameter type annotation, said API annotation including a first category, a second category, and a third category, said first category of said API annotation being used to tag said control type annotation of said original code annotation, said second category of said API annotation being used to tag said service type annotation of said original code annotation, said third category of said API annotation being used to tag said method parameter type annotation of said original code annotation;
adding an API annotation to the original code annotation to obtain a target code annotation, wherein the API annotation comprises:
judging the annotation type of the original code annotation;
and adding the API annotation of the corresponding category to the original code annotation according to the annotation type of the original code annotation to obtain the target code annotation.
3. The method of generating an API document of claim 2, wherein if the target code annotation enters the annotation processor, processing the target code annotation entering the annotation processor to obtain API data, comprises:
extracting a plurality of first elements marked by the first category in the object code annotation entering the annotation processor, and sequentially judging whether the first elements contain the control type annotation, wherein the first elements comprise a plurality of sub-elements;
if the control type annotation is contained, extracting service data of the first category and the control type annotation;
extracting sub-elements which are method classes in the first elements, and acquiring second elements according to the sub-elements of the method classes;
acquiring method attribute data and parameter attribute data according to the second element;
and acquiring API data according to the service data, the method attribute data and the parameter attribute data.
4. The method of generating an API document of claim 3, wherein said obtaining method attribute data and parameter attribute data from said second element comprises:
determining whether the second element contains the service type annotation,
if the service type annotation is contained, extracting the second category, the self attribute data of the second element and the method attribute data of the service type annotation;
converting the second element parameter to obtain a third element;
and acquiring the parameter attribute data according to the self parameter data of the third element and the method parameter type annotation.
5. The method of generating an API document of claim 1, wherein said annotation levels comprise a source code level, a class level, and a run level;
the judging whether the target code annotation enters an annotation processor according to the annotation level of the target code annotation comprises the following steps:
when the annotation level is the source code level, the object code annotation enters the annotation processor;
when the annotation level is the class level or the run level, then the object code annotation does not enter the annotation processor.
6. The method of generating an API document according to claim 5, wherein when the annotation level is the class level or run level, then the target code annotation does not enter the annotation processor, including;
when the annotation level is the class level, the object code annotation does not enter the annotation processor, enters a source file and is not loaded and run;
and when the annotation level is the operation level, the object code annotation does not enter the annotation processor, enters a source file, and is loaded and operated, wherein the source file is a compiled byte code file.
7. The method of generating an API document of claim 6 wherein said annotation levels of said API annotation are all said source code levels.
8. An apparatus for generating an API document, comprising:
a first obtaining unit for obtaining an original code annotation;
the annotation unit is used for adding API annotation to the original code annotation to obtain a target code annotation;
the judging unit is used for judging whether the target code annotation enters an annotation processor or not according to the annotation level of the target code annotation;
the second acquisition unit is used for processing the target code annotation entering the annotation processor and acquiring API data if the target code annotation enters the annotation processor;
and the generating unit is used for summarizing the API data to generate an API document.
9. A computer device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor implements the method of generating an API document as claimed in any one of claims 1 to 7 when executing the computer program.
10. A computer-readable storage medium, characterized in that it stores a computer program which, when read and executed by a processor, implements the method of generating an API document according to any one of claims 1-7.
CN202211643056.XA 2022-12-20 2022-12-20 Method and device for generating API (application program interface) document, computer equipment and storage medium Pending CN115981723A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202211643056.XA CN115981723A (en) 2022-12-20 2022-12-20 Method and device for generating API (application program interface) document, computer equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202211643056.XA CN115981723A (en) 2022-12-20 2022-12-20 Method and device for generating API (application program interface) document, computer equipment and storage medium

Publications (1)

Publication Number Publication Date
CN115981723A true CN115981723A (en) 2023-04-18

Family

ID=85965979

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202211643056.XA Pending CN115981723A (en) 2022-12-20 2022-12-20 Method and device for generating API (application program interface) document, computer equipment and storage medium

Country Status (1)

Country Link
CN (1) CN115981723A (en)

Similar Documents

Publication Publication Date Title
Maffeis et al. An operational semantics for JavaScript
US7478366B2 (en) Debugger and method for debugging computer programs across multiple programming languages
JP4619698B2 (en) Code segment creation method and system
Kirby Reflection and hyper-programming in persistent programming systems
Fuhrer et al. Efficiently refactoring Java applications to use generic libraries
Wang et al. Precise constraint-based type inference for Java
US20080222616A1 (en) Software translation
US8533692B2 (en) Dynamic software enhancement parameters
US20060026559A1 (en) Automatic content completion of valid values for method argument variables
US8621429B2 (en) Software development support apparatus, function extension method and storage medium for storing function extension program
KR100750834B1 (en) A method of data call stack tracing in data monitoring of JAVA byte code and a device for storing the method in compter program type
Shatnawi et al. Analyzing program dependencies in java ee applications
US8171452B2 (en) Dynamic software enhancement
US20140372993A1 (en) Overloading on constants
US9038033B1 (en) Techniques and mechanisms for web application minification
US8606766B2 (en) Method and system to handle java class versioning
CN112612502A (en) Patch generation method, device, equipment and storage medium
CN114153459A (en) Interface document generation method and device
US20050055682A1 (en) Authoring and using generic classes in JAVA language code
Brada et al. Practical verification of component substitutability using subtype relation
CN110110299B (en) Text conversion method, device and server
CN112445706A (en) Program abnormal code acquisition method and device, electronic equipment and storage medium
CN112269602A (en) WebAssembly loading method and device and storage medium
CN115981723A (en) Method and device for generating API (application program interface) document, computer equipment and storage medium
Knodel et al. Improving fact extraction of framework-based software systems

Legal Events

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