Java annotation conversion method and device, electronic equipment and storage medium

By using a Java annotation conversion system and a Large Language Model (LLM) to automatically convert Java annotations, the problems of low efficiency and error-proneness of manual conversion are solved, achieving efficient and accurate annotation conversion.

CN119271266APending Publication Date: 2025-01-07NEUSOFT CORP
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411194984.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-28
Publication Date
2025-01-07

AI Technical Summary

Technical Problem

In existing technologies, Java annotation conversion requires manual operation, which leads to low efficiency and is prone to errors.

Method used

A Java annotation conversion system is adopted, which includes a source code extractor, an annotation converter collection, and a source code loader. It automatically converts annotation data and uses the Large Language Model (LLM) for annotation conversion to ensure accuracy.

Benefits of technology

It achieves efficient and automated conversion of Java annotations without disrupting the original code structure, improving conversion efficiency and ensuring accuracy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119271266A_ABST
    Figure CN119271266A_ABST
Patent Text Reader

Abstract

The invention provides a Java annotation conversion method and device, electronic equipment and a storage medium, and relates to the technical field of data processing, the method is applied to a Java annotation conversion system, the system is internally configured with a source code extractor, an annotation converter set and a source code loader, the annotation converter set comprises a plurality of converters, and the converters are connected with the source code loader. The Java annotation conversion method comprises the following steps: extracting first annotation data of a first Java source code in a first annotation system and first code structure information corresponding to the first annotation data by utilizing a source code extractor; in the annotation converter set, converting the first annotation data into second annotation data under a second annotation system by utilizing a plurality of converters; and based on the first code structure information, combining the second annotation data and fixed code data in the first Java source code by using a source code loader to obtain a second Java source code under the second annotation system. According to the application, the annotation conversion accuracy can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of data processing technology, and in particular to a Java annotation conversion method, device, electronic device and storage medium. Background Art

[0002] In Java, annotations are a type of code-level metadata that provides a way to add information to a program at compile time or runtime. Annotations can be used to enhance the readability and maintainability of code, as well as to provide metadata for frameworks and tools. When the annotation system of a code changes, annotation conversion between the two annotation systems is required.

[0003] Currently, manual conversion is often required when converting Java annotations. However, manual conversion is a tedious and error-prone process. Developers need to be familiar with the syntax and semantic differences between the two annotation systems and manually modify each annotation, which is not only time-consuming, but also easy to introduce errors due to human factors. Summary of the invention

[0004] The present invention provides a Java annotation conversion method, device, electronic device and storage medium, which can improve the efficiency of Java annotation conversion while ensuring the accuracy of annotation conversion.

[0005] In a first aspect, a Java annotation conversion method is provided, which is applied to a Java annotation conversion system. The Java annotation conversion system is internally configured with a source code extractor, an annotation converter set, and a source code loader, wherein the annotation converter set includes multiple converters. The Java annotation conversion method includes: Extracting first annotation data of a first Java source code under a first annotation system and first code structure information corresponding to the first annotation data using a source code extractor; In the annotation converter set, a plurality of converters are used to convert the first annotation data into second annotation data under a second annotation system; Based on the first code structure information, the second annotation data and the fixed code data in the first Java source code are combined by using a source code loader to obtain a second Java source code under a second annotation system.

[0006] In a second aspect, a Java annotation conversion device is provided, which is applied to a Java annotation conversion system. The Java annotation conversion system is internally configured with a source code extractor, an annotation converter set, and a source code loader, wherein the annotation converter set includes multiple converters. The Java annotation conversion device includes: An extraction module, configured to extract first annotation data of a first Java source code under a first annotation system and first code structure information corresponding to the first annotation data by using a source code extractor; A conversion module, used for converting the first annotation data into second annotation data under a second annotation system by using a plurality of converters in the annotation converter set; The combining module is used to combine the second annotation data and the fixed code data in the first Java source code based on the first code structure information by using the source code loader to obtain the second Java source code under the second annotation system.

[0007] According to a third aspect, an electronic device is provided, comprising: a processor and a memory, wherein the memory is used to store a computer program, and the processor is used to call and run the computer program stored in the memory to execute the method in the first aspect or its various implementations.

[0008] According to a fourth aspect, a computer-readable storage medium is provided for storing a computer program, wherein the computer program enables a computer to execute the method according to the first aspect or its various implementations.

[0009] Through the technical solution provided by the present invention, the source code extractor can be used to extract the first annotation data to be converted and the first code structure information corresponding to the first annotation data in the first Java source code; then the first annotation data can be converted into the second annotation data under the second annotation system using multiple converters configured in the annotation converter set; finally, based on the first code structure information, the source code loader is used to combine the second annotation data and the fixed code data in the first Java source code to obtain the second Java source code under the second annotation system. The technical solution in this application can realize the automatic conversion of annotation data under two annotation systems without destroying the original code structure, without manual participation in the annotation conversion process, and can improve the efficiency of Java annotation conversion and ensure the accuracy of annotation conversion.

[0010] It should be understood that the above general description and the following detailed description are only exemplary and explanatory and cannot limit the present invention. Other features and advantages of the present invention will be described in detail in the following specific implementation section. BRIEF DESCRIPTION OF THE DRAWINGS

[0011] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0012] Figure 1A system architecture diagram of a Java annotation conversion system provided in an embodiment of the present application; Figure 2 A flowchart of a Java annotation conversion method provided in an embodiment of the present application; Figure 3 A flowchart of a Java annotation conversion method provided in another embodiment of the present application; Figure 4 A schematic diagram of a first annotation data extraction process provided in an embodiment of the present application; Figure 5 A schematic diagram of the structure of a Java annotation conversion device provided in an embodiment of the present application; Figure 6 A schematic diagram of the structure of a Java annotation conversion device provided by another embodiment of the present application; Figure 7 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0013] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.

[0014] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or server that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0015] In Java, annotations are a type of code-level metadata that provides a way to add information to a program at compile time or runtime. Annotations can be used to enhance the readability and maintainability of code, as well as to provide metadata for frameworks and tools. When the annotation system of a code changes, annotation conversion between the two annotation systems is required.

[0016] Currently, manual conversion is often required when converting Java annotations. However, manual conversion is a tedious and error-prone process. Developers need to be familiar with the syntax and semantic differences between the two annotation systems and manually modify each annotation, which is not only time-consuming, but also easy to introduce errors due to human factors.

[0017] In order to solve the above technical problems, the present invention is to propose a method as follows Figure 1 The Java annotation conversion system shown in the figure is internally configured with a source code extractor 11, an annotation converter set 12 and a source code loader 13, wherein the annotation converter set 12 includes multiple converters. When performing Java annotation conversion, the source code extractor 11 can be used to first extract the first annotation data of the first Java source code under the first annotation system, and extract the first code structure information corresponding to the first annotation data; then, the multiple converters in the annotation converter set 12 can be used to convert the first annotation data into the second annotation data under the second annotation system; finally, based on the first code structure information, the source code loader 13 is used to combine the second annotation data and the fixed code data in the first Java source code to obtain the second Java source code under the second annotation system.

[0018] In a specific application scenario, the multiple converters included in the annotation converter set 12 can be adaptively set according to the two annotation systems for annotation conversion. In the following embodiment steps of the present disclosure, the first annotation system is the Swagger2.0 annotation system, and the second annotation system is the SpringDoc annotation system. For example, the technical solution in the present application is described, but it does not constitute a specific limitation. Accordingly, when converting the Swagger2.0 annotation to the SpringDoc annotation, the multiple converters included in the annotation converter set 12 may include a class dependency converter, a configuration annotation converter, a class annotation converter, a method annotation converter, and a parameter annotation converter.

[0019] It should be noted that the technical solution in this application can be applied to Java annotation conversion between any two annotation systems. Accordingly, the multiple converters included in the annotation converter set 12 can be personalized according to the two annotation systems for annotation conversion. In the following embodiment steps of this application, the technical solution in this application is explained by taking the conversion of Swagger2.0 annotations under the Swagger2.0 annotation system to SpringDoc annotations under the SpringDoc annotation system as an example, but it does not constitute a specific limitation.

[0020] After introducing the application scenarios of the embodiments of the present invention, the technical solutions of the present invention will be described in detail below: Figure 2A flowchart of a Java annotation conversion method provided by an embodiment of the present invention, the method can be as follows Figure 1 The Java annotation conversion system is shown in Figure 1. Figure 2 As shown, the method may include the following steps: Step 210: Use a source code extractor to extract first annotation data of a first Java source code under a first annotation system and first code structure information corresponding to the first annotation data.

[0021] The first annotation system is the original annotation system to be converted into annotations; the first Java source code is the Java source code to be converted into annotations under the first annotation system; the first annotation data is the annotation data contained in the first Java source code; the first code structure information is the code structure information that can reflect the first annotation data in the first Java source code, and is used to splice the code after the annotation conversion, and replace the spliced ​​code into the first Java source code. The first code structure information may include code definition (function), code name (name), code implementation (statement), code start line number (start), and code end line number (end).

[0022] For the disclosed embodiment, the source code extractor constructed in the Java annotation conversion system can be used to call the JavaParser library through Python to traverse all Java source code files in the specified directory corresponding to the first annotation system, that is, the first Java source code. Then, the first annotation data is extracted and defined as a corresponding data structure, and the first code structure information corresponding to the first annotation data is further obtained.

[0023] Step 220: In the annotation converter set, use multiple converters to convert the first annotation data into second annotation data under the second annotation system.

[0024] The second annotation system is a target annotation system corresponding to the annotation conversion; the second annotation data is annotation data under the second annotation system obtained after the first annotation data is annotated and converted.

[0025] For the disclosed embodiment, the annotation converter set constructed in the Java annotation conversion system can be used to perform annotation conversion on the first annotation data. This stage aims to automatically convert the extracted first annotation data into the second annotation data to achieve seamless migration and upgrade of the API documentation tool. When the first annotation data contains multiple annotation types, multiple converters can be configured in the annotation converter set. Different converters are used to perform annotation conversion on different types of first annotation data.

[0026] When using different converters to perform annotation conversion on different types of first annotation data respectively, as a possible implementation method, for each converter, a corresponding conversion rule can be configured in it, so that the converter converts the first annotation data into the second annotation data based on the conversion rule; in view of the outbreak of big models, a new revolution has been launched in the field of software engineering, and some good results have been achieved in tasks such as code generation, error detection, automated testing, and document conversion. Therefore, as another possible implementation method, a big model can be used to realize the automatic conversion of code annotations. Specifically, a corresponding big model can be configured in each converter. The big model can accurately understand the differences between different annotation systems and automatically generate corresponding second annotation data, thereby greatly improving the efficiency and accuracy of the conversion and saving valuable time for developers.

[0027] Step 230: Based on the first code structure information, a source code loader is used to combine the second annotation data with the fixed code data in the first Java source code to obtain a second Java source code under a second annotation system.

[0028] For the embodiments of the present disclosure, after completing the conversion of all first annotation data to obtain second annotation data, different second annotation data can be spliced ​​based on the first code structure information, and the spliced ​​annotation data can be combined with the fixed code data in the first Java source code to obtain the second Java source code under the second annotation system.

[0029] In summary, according to the Java annotation conversion method provided by the present invention, the source code extractor can be used to extract the first annotation data to be converted and the first code structure information corresponding to the first annotation data in the first Java source code; then the first annotation data is converted into the second annotation data under the second annotation system by using multiple converters configured in the annotation converter set; finally, based on the first code structure information, the source code loader is used to combine the second annotation data and the fixed code data in the first Java source code to obtain the second Java source code under the second annotation system. The technical solution in this application can realize the automatic conversion of annotation data under two annotation systems without destroying the original code structure, without manual participation in the annotation conversion process, and can improve the efficiency of Java annotation conversion and ensure the accuracy of annotation conversion.

[0030] based on Figure 2 The embodiment shown is a refinement and extension of the above embodiment. In order to fully illustrate the specific implementation process of the method of this embodiment, this embodiment provides the following Figure 3 The specific method shown. Figure 3 based on Figure 2 As shown in 3, the method comprises the following steps: Step 310: Use a source code extractor to extract first annotation data of a first Java source code under a first annotation system and first code structure information corresponding to the first annotation data, wherein the first annotation data includes at least one of a first type of dependency and a first target annotation.

[0031] In specific application scenarios, such as Figure 4 As shown, when using the source code extractor to extract the first annotation data of the first Java source code under the first annotation system and the first code structure information corresponding to the first annotation data, a file traversal process can be first performed to traverse all files under the specified project path, find all files ending with ".java", that is, the first Java source code, and store the paths of these files in the list java_files, otherwise, ignore these codes. After finding the .java file, the dependency information reading process can be further performed, that is, for each Java file in the list java_files, read the file content, and check whether each line contains the first annotation import statement corresponding to the first annotation system. If the first annotation import statement is detected, the index of the line is recorded in the list v2_annotations_list. Otherwise, it is considered that the current first Java source code does not contain the first annotation data, no conversion is required, and the source code is directly retained. After detecting the first annotation data, the java code file is parsed by using javaparser to obtain the first code structure information, which may include: whether to handle exceptions (isThrowException), which is used for splicing code after conversion; code definition (function), which is used for splicing code after conversion; code name (name), a unique identifier; code implementation (statement), which does not include code definition, which is used for splicing code after conversion; code start line number (start), which is used to replace the spliced ​​code into the source file; code end line number (end), which is used to replace the spliced ​​code into the source file; parameter array (parameters), which is used to convert the annotation of the parameter separately; method annotation part (Method), which is used for conversion. The parsing process includes executing Java related commands to identify and extract the above definition content. In addition, method information extraction can be further performed, the obtained parsing output is converted into JSON format, Method in the method information is extracted from the parsing output in JSON format, Method objects are created, and these objects are stored in the list method_list, so that annotation conversion can be performed based on Method objects later.

[0032] As another possible implementation, if an error occurs during the extraction of the first annotation data, such as failure to execute a Java command, the source code extractor can capture the error information and print it out to facilitate user troubleshooting.

[0033] Correspondingly, for the embodiments of the present disclosure, the embodiment steps may include: using a source code extractor to perform a source code extraction process on a first Java source code under a first annotation system, obtaining first annotation data of the first Java source code under the first annotation system and first code structure information corresponding to the first annotation data; wherein the source code extraction process includes: extracting a Java file in the first Java source code; reading the first annotation data containing the first annotation import statement corresponding to the first annotation system in the Java file; and obtaining the first code structure information corresponding to the first annotation data by parsing the first annotation data, wherein the first code structure information includes at least the code start line number and the code end line number corresponding to the first annotation data.

[0034] Exemplarily, when the first annotation system is the Swagger2.0 annotation system, the first annotation data is the Swagger2.0 annotation, and the first annotation import statement corresponding to the Swagger2.0 annotation system may be "importio.swagger.annotations" or "import springfox.documentation.annotations".

[0035] Step 320a: Use a class dependency converter to convert the first class dependency into a second class dependency based on a preset conversion rule.

[0036] In a specific application scenario, class dependency conversion is the first step of annotation conversion, and its purpose is to update the import statements in the Java file to reflect the new dependencies of the second annotation data.

[0037] For the disclosed embodiment, the class dependency converter automatically converts the class dependency according to the preset conversion rules. The preset conversion rules may include: determining at least one code line position containing a first annotation import statement under a first annotation system in a first Java source code; clearing the first annotation import statement, and adding a second annotation import statement under a second annotation system at the first code line position in the at least one code line position.

[0038] Exemplarily, when the first annotation system is the Swagger2.0 annotation system and the second annotation system is the SpringDoc annotation system, when the first type of dependency is converted into the second type of dependency by using the class dependency converter based on the preset conversion rules, the following steps may be performed: Record the import line number of the Swagger2 annotation (i.e. the first annotation data): During the code extraction phase, the system has identified and recorded the line numbers of all Swagger2 annotation import statements (i.e. the first annotation import statements), which are stored in the v2_annotations_list list.

[0039] Clear the original Swagger2 annotation import statements: The system traverses each line number in v2_annotations_list and replaces the original Swagger2 annotation import statements in the corresponding line with empty ones, effectively removing all old Swagger2 annotation dependencies.

[0040] Add SpringDoc annotation imports: At the first line number processed, the system will add new import statements required by SpringDoc annotations (i.e., second annotation import statements). Specifically, the following six second annotation import statements related to SpringDoc annotations can be added to the top of the Java file: ["import io.swagger.v3.oas.annotations.Hidden;\n" "import io.swagger.v3.oas.annotations.0peration;\n" "import io.swagger.v3.oas.annotations.Parameter;\n" "import io.swagger.v3.oas.annotations.Parameters;\n" "import io.swagger.v3.oas.annotations.media.Schema;\n" "import io.swagger.v3.oas.annotations.tags.Tag;\n" "import io.swagger.v3.oas.annotations.enums.ParameterIn;\n",] These second annotation import statements are required for the correct working of SpringDoc annotations, and they ensure that subsequent annotation conversion and code updates can proceed smoothly. Based on the above steps, class dependency conversion in Java code is implemented.

[0041] Step 330a: Determine a first code line difference between the first type of dependency and the second type of dependency regarding class dependency conversion.

[0042] In a specific application scenario, class dependency conversion may result in a change in the number of code lines. When the number of code lines changes, a parameter line_diff1 may be used to record the difference in the number of first code lines generated after the first type of dependency is converted to the second type of dependency, to facilitate loading the converted code back in step 350 of the embodiment.

[0043] In embodiment step 320b, which is parallel to embodiment step 320a, in a target annotation converter, the first target annotation is converted into a second target annotation using the large language model configured therein.

[0044] Among them, the target annotation converter includes at least one of a configuration annotation converter, a class annotation converter, a method annotation converter, and a parameter annotation converter, the first target annotation includes at least one of a first configuration annotation, a first class annotation, a first method annotation, and a first parameter annotation, and the second target annotation includes at least one of a second configuration annotation, a second class annotation, a second method annotation, and a second parameter annotation. When the first annotation system is the Swagger2.0 annotation system and the second annotation system is the SpringDoc annotation system, the target annotation converter may include a configuration annotation converter, a class annotation converter, a method annotation converter, and a parameter annotation converter, the first target annotation may include a first configuration annotation, a first class annotation, a first method annotation, and a first parameter annotation, and the second target annotation may include a second configuration annotation, a second class annotation, a second method annotation, and a second parameter annotation.

[0045] For the embodiment of the present disclosure, in step 320b, in the target annotation converter, using the large language model configured therein, converting the first target annotation into the second target annotation may include the following steps: Step 320b-1: extract a first target annotation beginning with a preset annotation character from the first annotation data.

[0046] In a specific application scenario, different first annotation data may correspond to different preset annotation characters. Exemplarily, when the first target annotation is the first configuration annotation, the preset annotation character may be "@Bean", and specifically, the first configuration annotation starting with "@Bean" may be extracted from the first annotation data; when the first target annotation is the first category annotation, the preset annotation character may be "@Api(", and specifically, the first category annotation starting with "@Api(" may be extracted from the first annotation data; when the first target annotation is the first method annotation, the preset annotation character may be "@Api", "@ApiOperation", "@ApiImplicitParams", "@ApiImplicitParam", "@ApiParam", "@ApiIgnore", "@ApiModel", "@ApiModelProperty", and specifically, the first method annotation starting with any one of the above preset annotation characters may be extracted from the first annotation data; when the first target annotation is the first parameter annotation, the preset annotation character may be "@ApiParam", "@ApiIgnore", and specifically, the first parameter annotation starting with "@ApiParam" or "@ApiIgnore" may be extracted from the first annotation data.

[0047] Step 320b-2: Using the large language model configured in the target annotation converter, based on preset model rules and sample data, convert the first target annotation into a second target annotation.

[0048] For the embodiments of the present disclosure, the embodiment steps may include: in a configuration annotation converter, using the first largest language model configured therein, based on the first preset model rule and the first sample data, converting the first configuration annotation into the second configuration annotation; and / or, in a class annotation converter, using the second largest language model configured therein, based on the second preset model rule and the second sample data, converting the first class annotation into the second class annotation; and / or, in a method annotation converter, using the third largest language model configured therein, based on the third preset model rule and the third sample data, converting the first method annotation into the second method annotation; and / or, in a parameter annotation converter, using the fourth largest language model configured therein, based on the fourth preset model rule and the fourth sample data, converting the first parameter annotation into the second parameter annotation. Among them, the first largest language model, the second largest language model, the third largest language model, and the fourth largest language model can be any large language model respectively. In the present application, the first largest language model, the second largest language model, the third largest language model, and the fourth largest language model are taken as large language models (LLM) as an example to illustrate the technical solution in the present application, but it does not constitute a specific limitation.

[0049] Among them, as a possible implementation method, in the configuration annotation converter, using the first language model configured therein, based on the first preset model rules and the first sample data, when converting the first configuration annotation to the second configuration annotation, specifically, after completing the dependency conversion, LLM is used in this stage to convert the Docket configuration of Swagger2 to the OpenAPI configuration of SpringDoc. Since there are differences in API configuration between the two annotation systems of Swagger2 and SpringDoc, correctly handling these configurations is crucial to ensuring the consistency and integrity of API documents. When encountering an API configuration form written in Swagger2.0 that starts with a Docket configuration method with "@Bean", let the first language model LLM play the role of an experienced backend engineer to convert it to obtain the converted second configuration annotation corresponding to the first configuration annotation. And it is hoped that the first language model LLM will comply with the following first preset model rules: First preset model rule 1: Transform according to the parameters in the original annotation First Preset Model Rule 2: Do not add extra parameters based on your own understanding Several rules clearly tell the first language model LLM to only convert the API configuration part in the code according to the pre-agreed requirements, and convert the existing parameters in the configuration according to prior knowledge, but do not hallucinate and add additional parameters to the configuration. In order to let the first language model LLM further understand the requirements of this stage, the first sample data is also given to the first language model LLM. The first sample data is as follows: Swagger2 uses Docket for configuration …java @Bean public Docket api(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("My API") .description("API Description") .version("1.0") .build(); … SpringDoc configuration using OpenAPI …java @Bean public OpenAPI customOpenAPI(){ return new OpenAPI() .info(new Info() .title("My API") .version("2.0") .deseription("API Description") .termsofService("http: / / swagger.io / terms / ") .license(new License().name("Apache 2.0").url("http: / / springdoc.org"))); } As a possible implementation method, in the class annotation converter, the second largest language model configured therein is used to convert the first type of annotation to the second type of annotation based on the second preset model rules and the second sample data. In view of the large scale of the code being processed, involving millions of lines of code and a large number of classes, the rule-based annotation conversion method will no longer be applicable. Therefore, the second largest language model LLM is used in this stage to perform the conversion of class annotations. First, traverse each line of Java code in the first annotation data. If the code starts with "@Api(", it means that the annotation is a class annotation written in Swagger2.0 and needs to be converted. In this stage, the second largest language model LLM plays the role of a backend development engineer, whose task is to convert the swagger2.0 class annotation @API (i.e., the first type of annotation) into the class annotation @Tag (i.e., the second type of annotation) that complies with the SpringDoc OpenAPI coding specification. In order to enable the second largest language model LLM to better perform the class annotation conversion work, a second preset model rule is added in this stage: Second preset model rule 1: Transform according to the parameters in the original annotation The second preset model rule 1 explicitly tells the second largest language model LLM that only the parameters in the original annotation need to be converted. In order to make the second largest language model LLM better understand the defined rules, we also add a second sample data to further inform the second largest language model LLM of the purpose through the second sample data. The second largest language model LLM can automatically perform the conversion operation based on the second preset model rule and the second sample data, replacing the Swagger2 class annotation with the SpringDoc annotation, ensuring that the integrity and functionality of the code are not affected during the conversion process. The second sample data is as follows: Swagger2.0 code: @Api(value = "Base Medicine controller", tags = {"Base Medicine"}) springDoc code: @Tag(name="Base medicine", description="Base medicine controller") As a possible implementation method, in the method annotation converter, the third language model configured therein is used to convert the first method annotation into the second method annotation based on the third preset model rule and the third sample data. When processing a large-scale Java code base, the conversion of method annotations is particularly critical because they are directly related to the specific behavior and document description of the API. Considering the huge scale of the code base and the diversity of annotation types, the present invention still uses the third language model LLM to complete this conversion task. First, traverse the code, and when the code starts with the preset annotation characters "@Api", "@ApiOperation", "@ApiImplicitParams", "@ApiImplicitParam", "@ApiParam", "@ApiIgnore", "@ApiMode1", and "@ApiModelProperty", the conversion is triggered. At this stage, the third language model LLM still plays the role of a back-end development engineer, converting the Swagger2.0 method annotation into a method annotation that complies with the SpringDoc coding specification. In order to allow the third language model LLM to better perform the method annotation conversion work, a third preset model rule is added at this stage: The third preset model rule 1: only convert the current annotation; Third preset model rule 2: Do not have any annotations, methods, or classes that are not in the code before conversion; The third preset model rule 3: Do not add a class or method outside the method based on your own understanding.

[0050] Among them, the third preset model rule 1 clearly tells the third language model LLM that only the method annotations in the original code need to be converted, but after actual operation, it is found that the third language model LLM will generate code beyond the requirements, such as an extra class or method. Therefore, we supplemented the third preset model rule 2 and the third preset model rule 3 on the basis of the third preset model rule 1 to strictly limit the excessive use of the third language model LLM.

[0051] In addition, method annotations may have parameters. For this case, this stage provides a third sample data for the third language model LLM for method annotations without parameters and method annotations with parameters respectively.

[0052] The third sample data without parameters is: Swagger2.0 code: …java @ApiOperation(value = "Get the cardinal drug list", notes = "Detailed list of cardinal drugs") … springDoc code: …java @Operation(summary = "Get the basic drug list", description = "Basic drug detailed list") … The third sample data with parameters is: Swagger2.0 code: …java @ApiOperation(value = "Get the base drug list", notes = "Base drug list") @ApiImplicitParans(value = {@ApiImplicitParam(paramType = "query",dataType = "string", name = "usagecode", value = "usage", required = false)}) … springDoc code: …java @Operation(summary = "Get the basic drug list", description = "Basic drug detailed list", paraneters = {@Parameter(name = "usageCode", description = "usage",schema=@Schema(type = "string"))}) … As a possible implementation method, in the parameter annotation converter, the fourth language model configured therein is used to convert the first parameter annotation into the second parameter annotation based on the fourth preset model rules and the fourth sample data. Parameter annotations play an important role in API documents, and they provide necessary meta information for API parameters. Traverse each line of code extracted to the parameter array parameters to determine whether it meets the triggering conditions for parameter annotation conversion, namely: annotations starting with @ApiParam, which indicates that the annotation is a parameter annotation of Swagger2; annotations starting with @ApiIgnore, which indicates that the annotation instructs Swagger2 to ignore a parameter.

[0053] For the recognized @ApiParam annotation, use the fourth language model LLM to map it to the parameter annotation of SpringDoc. Let the fourth language model LLM act as a backend development engineer to complete the parameter annotation conversion and let the fourth language model LLM meet two fourth preset model rules: Fourth preset model rule 1: only convert the current annotation; Fourth Preset Model Rule 2: Don’t add an extra method or class based on your understanding And a fourth sample data for reference is provided for the fourth language model LLM. The fourth sample data is as follows: Swagger2.0 code: @ApiParam(name = "bookSns", value = "Order Serial Number", required = true) springDoc code: @Parameter(name = "bookSns", description = "Order serial number", required = true) For the recognized @ApiIgnore annotation, the system will apply predefined rules for transformation, that is, replace the line of code with @Parameter(hidden = true) to ensure that the parameter is properly hidden in SpringDoc.

[0054] Step 330b: Determine a second code line difference between the first target annotation and the second target annotation regarding annotation conversion.

[0055] Similar to class dependency conversion, annotation conversion can also cause changes in the number of code lines. For example, after using LLM to perform API configuration conversion, it may cause differences in the number of code lines. In order to load the converted code back in step 350 of the embodiment, at this stage, the parameter line_diff2 can be used to record the second code line difference after annotation conversion.

[0056] Step 340: Based on the first code structure information, the first code line difference and / or the second code line difference, respectively calculate the converted code positions corresponding to the second annotation data and the fixed code data, wherein the second annotation data includes at least one of a second type of dependency and a second target annotation.

[0057] It should be noted that, for the two branch steps of embodiment steps 320a, 330a, and embodiment steps 320b, 330b, in a specific application scenario, the two branch steps may be executed simultaneously, or only one of the branch steps may be executed. As a possible implementation, when only the branch steps of embodiment steps 320a, 330a are executed, the embodiment of the present disclosure may respectively calculate the converted code positions of the second annotation data and the fixed code data based on the first code structure information and the first code line number difference; as a possible implementation, when only the branch steps of embodiment steps 320b, 330b are executed, the embodiment of the present disclosure may respectively calculate the converted code positions of the second annotation data and the fixed code data based on the first code structure information and the second code line number difference; as a possible implementation, when the two branch steps of embodiment steps 320a, 330a, and embodiment steps 320b, 330b are executed simultaneously, the embodiment of the present disclosure may respectively calculate the converted code positions of the second annotation data and the fixed code data based on the first code structure information, the first code line number difference and the second code line number difference. In the following embodiments of the present disclosure, the technical solution in the present application is explained by taking the example of respectively calculating the converted code positions corresponding to the second annotation data and the fixed code data based on the first code structure information, the first code line number difference and the second code line number difference, but it does not constitute a specific limitation.

[0058] For the disclosed embodiment, after completing the conversion of the class dependency and the first target annotation, the converted second annotation data can be accurately loaded back into the original Java source code file. When the second annotation data includes the second class dependency and the second target annotation, the converted code can be loaded according to the start line number (start) and end line number (end) of the converted code in the first code structure information extracted in step 310 of the embodiment, the first code line number difference (line_diff1) of the class dependency conversion determined in step 330a of the embodiment, and the second code line number difference (line_diff2) of the annotation conversion determined in step 330b of the embodiment, and at the beginning of the converted code, the converted class dependency and target annotation are spliced ​​respectively, and the converted code position corresponding to the second annotation data and the fixed code data is further calculated. Among them, new_code is the code position after conversion, and code is the code position before conversion:

[0059]

[0060]

[0061] Step 350: reorganize the second annotation data and the fixed code data according to the converted code position to obtain a second Java source code under the second annotation system.

[0062] For the disclosed embodiment, after determining the converted code position corresponding to the second annotation data and the fixed code data, the accurate position of the second annotation data in the original code file can be located based on the converted code position, and the line number of the subsequent code can be adjusted according to the line number difference, and then the converted second annotation data can be inserted at this position. The converted annotation code can be automatically loaded back into the original code without destroying the structure of the original code. That is, the code reorganization of the second annotation data and the fixed code data is achieved, and the second Java source code under the second annotation system is obtained.

[0063] In summary, the technical solution in this application can first use a source code extractor in the first Java source code to extract the first annotation data to be annotated and the first code structure information corresponding to the first annotation data; then use multiple converters configured in the annotation converter set to convert the first annotation data into the second annotation data under the second annotation system; finally, based on the first code structure information, use the source code loader to combine the second annotation data and the fixed code data in the first Java source code to obtain the second Java source code under the second annotation system. The technical solution in this application can realize the automatic conversion of annotation data under two annotation systems without destroying the original code structure, without manual participation in the annotation conversion process, which can improve the efficiency of Java annotation conversion and ensure the accuracy of annotation conversion.

[0064] Based on the above Figure 2 , Figure 3 The specific description of the Java annotation conversion method provided is as follows Figure 5 As shown, Figure 5 The Java annotation conversion device is applied to a Java annotation conversion system, and the Java annotation conversion system is internally configured with a source code extractor, an annotation converter set, and a source code loader, wherein the annotation converter set includes multiple converters, such as Figure 5 As shown, the device comprises: An extraction module 51, which can be used to extract first annotation data of a first Java source code under a first annotation system and first code structure information corresponding to the first annotation data by using a source code extractor; A conversion module 52, which can be used to convert the first annotation data into second annotation data under a second annotation system using multiple converters in the annotation converter set; The combining module 53 can be used to combine the second annotation data and the fixed code data in the first Java source code based on the first code structure information using the source code loader to obtain the second Java source code under the second annotation system.

[0065] In some embodiments of the present application, the extraction module 51 can be used to use a source code extractor to perform a source code extraction process on a first Java source code under a first annotation system to obtain first annotation data of the first Java source code under the first annotation system and first code structure information corresponding to the first annotation data; wherein the source code extraction process includes: extracting a Java file in the first Java source code; reading the first annotation data containing the first annotation import statement corresponding to the first annotation system in the Java file; and obtaining the first code structure information corresponding to the first annotation data by parsing the first annotation data, wherein the first code structure information includes at least the code start line number and the code end line number corresponding to the first annotation data.

[0066] In some embodiments of the present application, multiple converters include at least one of a class dependency converter and a target annotation converter, the first annotation data includes at least one of a first class dependency and a first target annotation, and the second annotation data includes at least one of a second class dependency and a second target annotation; the conversion module 52 can be used to use the class dependency converter to convert the first class dependency into the second class dependency based on a preset conversion rule; and / or, in the target annotation converter, the first target annotation is converted into the second target annotation using the large language model configured therein, wherein the target annotation converter includes at least one of a configuration annotation converter, a class annotation converter, a method annotation converter, and a parameter annotation converter, the first target annotation includes at least one of a first configuration annotation, a first class annotation, a first method annotation, and a first parameter annotation, and the second target annotation includes at least one of a second configuration annotation, a second class annotation, a second method annotation, and a second parameter annotation.

[0067] In some embodiments of the present application, the preset conversion rules include: determining at least one code line position containing a first annotation import statement under a first annotation system in a first Java source code; clearing the first annotation import statement, and adding a second annotation import statement under a second annotation system at the first code line position in at least one code line position.

[0068] In some embodiments of the present application, the conversion module 52 can be specifically used to extract a first target annotation starting with a preset annotation character from the first annotation data; and use the large language model configured in the target annotation converter to convert the first target annotation into a second target annotation based on preset model rules and sample data.

[0069] In some embodiments of the present application, Figure 6 As shown, the device further includes: a determination module 54; The determination module 54 may be used to determine a first code line difference between the first class dependency and the second class dependency regarding class dependency conversion; and / or determine a second code line difference between the first target annotation and the second target annotation regarding annotation conversion.

[0070] In some embodiments of the present application, the combination module 53 can be specifically used to calculate the converted code positions corresponding to the second annotation data and the fixed code data based on the first code structure information, the first code line difference and / or the second code line difference; according to the converted code positions, the second annotation data and the fixed code data are reorganized to obtain the second Java source code under the second annotation system.

[0071] Regarding the device in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.

[0072] The embodiments of the present application and the technical solutions disclosed in the present invention propose a rule-based source code extractor, which traverses all files under the specified project path, judges and extracts the relevant code of the first annotation system in the Java source code file and the relevant structural information such as the starting line number and the structure line number according to the rules, so as to be used for subsequent source code loading; designs a set of annotation converters based on the combination of rules and LLM, converts the class dependencies, configuration annotations, class annotations, method annotations and parameter annotations in the code respectively, and uses LLM to automatically convert the first annotation data in the million-level code into the second annotation data, so as to realize the seamless migration and upgrade of API documentation tools using LLM; and constructs a source code loader, which locates the exact position in the original code file according to the code structure information, inserts the converted annotation code at this position, and automatically loads the converted annotation code back to the original code without destroying the structure of the original code.

[0073] The Java annotation conversion device of the embodiment of the present invention is described above from the perspective of the functional module in conjunction with the accompanying drawings. It should be understood that the functional module can be implemented in the form of hardware, can be implemented by instructions in the form of software, and can also be implemented by a combination of hardware and software modules. Specifically, the steps of the Java annotation conversion method embodiment in the embodiment of the present invention can be completed by the hardware integrated logic circuit and / or software instructions in the processor, and the steps of the Java annotation conversion method applied in conjunction with the embodiment of the present invention can be directly embodied as a hardware decoding processor to execute, or can be executed by a combination of hardware and software modules in the decoding processor. Optionally, the software module can be located in a mature storage medium in the field such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory, an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory, and the processor reads the information in the memory, and completes the steps in the above-mentioned Java annotation conversion method embodiment in conjunction with its hardware.

[0074] Figure 7 It is a schematic block diagram of an electronic device 700 according to an embodiment of the present invention.

[0075] like Figure 7 As shown, the electronic device 700 may include: The memory 710 and the processor 720, the memory 710 is used to store the computer program and transmit the program code to the processor 720. In other words, the processor 720 can call and run the computer program from the memory 710 to implement the method in the embodiment of the present invention.

[0076] For example, the processor 720 may be configured to execute the above method embodiments according to instructions in the computer program.

[0077] In some embodiments of the present invention, the processor 720 may include but is not limited to: General-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc.

[0078] In some embodiments of the present invention, the memory 710 includes but is not limited to: Volatile memory and / or non-volatile memory. Among them, the non-volatile memory can be read-only memory (ROM), programmable ROM (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM) or flash memory. Volatile memory can be random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of RAM are available, such as static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link DRAM (SLDRAM), and direct memory bus random access memory (Direct Rambus RAM, DR RAM).

[0079] In some embodiments of the present invention, the computer program may be divided into one or more modules, which are stored in the memory 710 and executed by the processor 720 to complete the method provided by the present invention. The one or more modules may be a series of computer program instruction segments capable of completing specific functions, and the instruction segments are used to describe the execution process of the computer program in the controller.

[0080] like Figure 7 As shown, the electronic device 700 may further include: The transceiver 730 may be connected to the processor 720 or the memory 710 .

[0081] The processor 720 may control the transceiver 730 to communicate with other devices, specifically, to send data to other devices or receive data sent by other devices. The transceiver 730 may include a transmitter and a receiver. The transceiver 730 may further include an antenna, and the number of antennas may be one or more.

[0082] It should be understood that the various components in the electronic device are connected via a bus system, wherein the bus system includes not only a data bus but also a power bus, a control bus and a status signal bus.

[0083] The present invention also provides a computer storage medium having a computer program stored thereon, which, when executed by a computer, enables the computer to perform the method of the above method embodiment. In other words, an embodiment of the present invention also provides a computer program product containing instructions, which, when executed by a computer, enables the computer to perform the method of the above method embodiment.

[0084] When software is used for implementation, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function according to the embodiment of the present invention is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable systems. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a website site, a computer, a server or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (Digital Subscriber Line, DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or a data center that includes one or more available media integration. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a digital video disc (Digital Video Disc, DVD)), or a semiconductor medium (e.g., a solid-state drive (SSD)), etc.

[0085] Those skilled in the art will appreciate that the modules and algorithm steps of each example described in conjunction with the embodiments applied herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.

[0086] In the several embodiments provided by the present invention, it should be understood that the disclosed systems, systems and methods can be implemented in other ways. For example, the system embodiments described above are only schematic. For example, the division of the modules is only a logical function division. There may be other division methods in actual implementation, such as multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of systems or modules, which can be electrical, mechanical or other forms.

[0087] The modules described as separate components may or may not be physically separated, and the components displayed as modules may or may not be physical modules, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. For example, each functional module in each embodiment of the present application may be integrated into a processing module, or each module may exist physically separately, or two or more modules may be integrated into one module.

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

Claims

1. A Java annotation conversion method, characterized in that: The method is applied to a Java annotation conversion system, wherein the Java annotation conversion system is internally configured with a source code extractor, an annotation converter set, and a source code loader, wherein the annotation converter set includes multiple converters, and the Java annotation conversion method includes: Extracting first annotation data of a first Java source code under a first annotation system and first code structure information corresponding to the first annotation data using the source code extractor; In the annotation converter set, the first annotation data is converted into second annotation data under a second annotation system using the multiple converters; Based on the first code structure information, the second annotation data and the fixed code data in the first Java source code are combined by using the source code loader to obtain a second Java source code under the second annotation system.

2. The method according to claim 1, characterized in that The extracting first annotation data of a first Java source code under a first annotation system and first code structure information corresponding to the first annotation data by using the source code extractor includes: Using the source code extractor to perform a source code extraction process on a first Java source code under a first annotation system, to obtain first annotation data of the first Java source code under the first annotation system and first code structure information corresponding to the first annotation data; The source code extraction process includes: Extracting a Java file from the first Java source code; Reading, in the Java file, first annotation data including a first annotation import statement corresponding to the first annotation system; By parsing the first annotation data, first code structure information corresponding to the first annotation data is obtained, and the first code structure information at least includes a code start line number and a code end line number corresponding to the first annotation data.

3. The method according to claim 1, characterized in that The plurality of converters include at least one of a class dependency converter and a target annotation converter, the first annotation data includes at least one of a first class dependency and a first target annotation, and the second annotation data includes at least one of a second class dependency and a second target annotation; In the annotation converter set, using the plurality of converters to convert the first annotation data into second annotation data under a second annotation system includes: Using the class dependency converter to convert the first class dependency into the second class dependency based on a preset conversion rule; and / or, In the target annotation converter, the first target annotation is converted into the second target annotation using the large language model configured therein, wherein the target annotation converter includes at least one of a configuration annotation converter, a class annotation converter, a method annotation converter, and a parameter annotation converter, the first target annotation includes at least one of a first configuration annotation, a first class annotation, a first method annotation, and a first parameter annotation, and the second target annotation includes at least one of a second configuration annotation, a second class annotation, a second method annotation, and a second parameter annotation.

4. The method according to claim 3, characterized in that The preset conversion rules include: Determining in the first Java source code at least one code line location containing a first annotation import statement under the first annotation system; The first annotation import statement is cleared, and a second annotation import statement under the second annotation system is added at the first code line position in the at least one code line position.

5. The method according to claim 3, characterized in that: In the target annotation converter, using the large language model configured therein, converting the first target annotation into the second target annotation includes: Extracting a first target annotation starting with a preset annotation character from the first annotation data; The first target annotation is converted into the second target annotation based on preset model rules and sample data by using the large language model configured in the target annotation converter.

6. The method according to claim 3, characterized in that The method further comprises: Determine a first code line difference between the first class dependency and the second class dependency regarding class dependency conversion; and / or, A second difference in the number of lines of code between the first target annotation and the second target annotation with respect to annotation transformation is determined.

7. The method according to claim 6, characterized in that The method of combining the second annotation data and the fixed code data in the first Java source code by using the source code loader based on the first code structure information to obtain the second Java source code under the second annotation system includes: Based on the first code structure information, the first code line number difference and / or the second code line number difference, respectively calculate the converted code positions corresponding to the second annotation data and the fixed code data; According to the converted code position, the second annotation data and the fixed code data are reorganized to obtain a second Java source code under the second annotation system.

8. A Java annotation conversion device, characterized in that: The device is applied to a Java annotation conversion system, wherein the Java annotation conversion system is internally configured with a source code extractor, an annotation converter set, and a source code loader, wherein the annotation converter set includes a plurality of converters, and the Java annotation conversion device comprises: An extraction module, configured to extract, by using the source code extractor, first annotation data of a first Java source code under a first annotation system and first code structure information corresponding to the first annotation data; A conversion module, configured to convert the first annotation data into second annotation data under a second annotation system by using the plurality of converters in the annotation converter set; A combining module is used to combine the second annotation data and the fixed code data in the first Java source code by using the source code loader based on the first code structure information to obtain a second Java source code under the second annotation system.

9. An electronic device, characterized in that: include: A processor and a memory, the memory being used to store a computer program, and the processor being used to call and run the computer program stored in the memory to execute the method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: Used to store a computer program, wherein the computer program enables a computer to execute the method according to any one of claims 1 to 7.