Code conversion method and device, storage medium and program product

By parsing the archive files and configuration files of Java services, extracting basic class information and dependencies, and performing field and method type conversion, it solves the problems of low conversion efficiency and difficult quality assurance in Java to TypeScript migration, and achieves efficient and accurate automatic code conversion.

CN120631371APending Publication Date: 2025-09-12BEIJING 58 INFORMATION TTECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202510725849.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-30
Publication Date
2025-09-12

AI Technical Summary

Technical Problem

When migrating services written in the Java programming language to services written in the TypeScript programming language, manually converting Java classes to TypeScript classes is time-consuming and tedious. Significant differences make it difficult to ensure conversion quality, and can introduce errors, reducing development efficiency.

Method used

By parsing the archive files and sub-project object configuration files of the service to be converted, extracting basic class information and dependencies, performing field and method type conversion, and generating TypeScript-compliant code.

Benefits of technology

It realizes automatic code conversion between programming types in different languages, reduces the difficulty and workload of cross-language conversion, and improves conversion efficiency and accuracy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120631371A_ABST
    Figure CN120631371A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a code conversion method and device, a storage medium and a program product. The method comprises the following steps: in response to a code conversion request triggered for a to-be-converted service, obtaining a first code corresponding to the to-be-converted service and an archived file on which the to-be-converted service depends; wherein the first code is a code written by adopting a first programming language. Analyzing an archived file on which the to-be-converted service depends and a sub-project object configuration file in the archived file, extracting class basic information and a dependency relationship, and further determining a first field and a first method contained in the first code, field type conversion processing is carried out on the first field, parameter type conversion processing is carried out on the parameters in the first method, and finally the second code conforming to the second programming language is generated, so that automatic code conversion between different language programming types is realized, the difficulty and workload of cross-language code conversion are reduced, and the efficiency of cross-language code conversion is improved. And the conversion efficiency and the accuracy of the conversion result are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of artificial intelligence technology, and in particular to a code conversion method, device, storage medium, and program product. Background Art

[0002] With the rapid development of information technology and the continuous evolution of software architecture, enterprises are gradually migrating services based on the Java programming language to services based on the TypeScript programming language to better adapt to the needs of modern web development. However, when migrating Java-based services to TypeScript-based services, developers need to manually convert the Java classes in the service to be converted into TypeScript classes. This process is not only time-consuming but also requires processing a large amount of code, significantly increasing the developer workload. Furthermore, the Java and TypeScript programming languages ​​have significant differences in type systems, syntax structures, and programming paradigms, making manual conversion difficult to ensure quality. Summary of the Invention

[0003] Various aspects of the present application provide a code conversion method, device, storage medium, and program product for automatically converting codes between programming types in different languages ​​to improve conversion efficiency and accuracy of conversion results.

[0004] The present invention provides a method for transcoding, which includes:

[0005] In response to a code conversion request triggered for a service to be converted, obtaining a first code corresponding to the service to be converted and an archive file on which the service to be converted depends, wherein the first code is written in a first programming language;

[0006] Parsing the class files in the archive file to extract basic class information corresponding to each class in the first code from the class files;

[0007] Parsing the sub-project object configuration file in the archive file to obtain dependency relationships corresponding to the first code, and determining a first field and a first method included in the first code based on basic class information corresponding to each class and the dependency relationships;

[0008] Performing field type conversion on the first field to obtain a converted second field, where the second field is a field in the second programming language;

[0009] Performing parameter type conversion on the parameters in the first method to obtain a converted second method, where the second method is a method in the second programming language;

[0010] Based on the second field and the second method, a second code corresponding to the service to be converted is generated.

[0011] The present invention provides a code conversion device, comprising:

[0012] an acquisition module, configured to, in response to a code conversion request triggered for a service to be converted, acquire a first code corresponding to the service to be converted and an archive file on which the service to be converted depends, wherein the first code is written in a first programming language;

[0013] an extraction module, configured to parse the class files in the archive file to extract basic class information corresponding to each class in the first code from the class files;

[0014] a parsing module, configured to parse the sub-project object configuration file in the archive file, obtain dependency relationships corresponding to the first code, and determine a first field and a first method included in the first code based on basic class information corresponding to each class and the dependency relationships;

[0015] a first conversion module, configured to perform field type conversion processing on the first field to obtain a converted second field, where the second field is a field in the second programming language;

[0016] a second conversion module, configured to perform parameter type conversion on the parameters in the first method to obtain a second method after conversion, where the second method is a method in the second programming language;

[0017] A generating module is used to generate a second code corresponding to the service to be converted based on the second field and the second method.

[0018] An embodiment of the present application further provides an electronic device, comprising: a memory and a processor; the memory is used to store a computer program; the processor is coupled to the memory and is used to execute the computer program to implement each step in the code conversion method provided in the embodiment of the present application.

[0019] The embodiment of the present application further provides a computer-readable storage medium storing a computer program. When the computer program is executed by a processor, the processor implements the steps of the code conversion method provided in the embodiment of the present application.

[0020] An embodiment of the present application further provides a computer program product, including a computer program / instruction. When the computer program / instruction is executed by a processor, the processor is caused to implement the steps of the code conversion method provided in the embodiment of the present application.

[0021] The code conversion solution provided in the embodiments of the present application can provide users with a code conversion service between different programming languages. In a specific implementation, first, in response to a code conversion request triggered for a service to be converted, the first code corresponding to the service to be converted and the archive file on which the service to be converted depends are obtained. The first code is written in a first programming language, and the code conversion request is used to request that the first code written in the first programming language be converted into code in a specified second programming language. Next, the class files in the archive file are parsed to extract basic class information corresponding to each class in the first code. The sub-project object configuration files in the archive file are parsed to obtain the dependencies corresponding to the first code. Then, based on the basic class information and dependencies corresponding to each class, the first field and first method contained in the first code are determined. Furthermore, field type conversion is performed on the first field to obtain a converted second field, which is a field in the second programming language. Parameter type conversion is performed on the parameters in the first method to obtain a converted second method, which is a method in the second programming language. Finally, based on the second field and the second method, the second code corresponding to the service to be converted is generated.

[0022] In the above scheme, by parsing the archive file that the service to be converted depends on and the sub-project object configuration file in the archive file, the basic class information and dependency relationship are extracted, and then the first field and the first method contained in the first code are determined, and then the field type conversion is performed on the first field and the parameter type conversion is performed on the parameters in the first method. Finally, the second code that conforms to the second programming language is generated, and the automatic conversion of codes between programming types in different languages ​​is realized, which reduces the difficulty and workload of cross-language code conversion and improves the conversion efficiency and the accuracy of the conversion results. BRIEF DESCRIPTION OF THE DRAWINGS

[0023] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:

[0024] Figure 1 A flowchart of a code conversion method provided by an exemplary embodiment of the present application;

[0025] Figure 2 A schematic diagram of a process for extracting basic class information corresponding to each class in a first code provided by an exemplary embodiment of the present application;

[0026] Figure 3 A schematic diagram of a flow chart of performing field type conversion processing on a first field included in a first code provided by an exemplary embodiment of the present application;

[0027] Figure 4A schematic diagram of a flow chart of parameter type conversion processing for parameters in a first method provided by an exemplary embodiment of the present application;

[0028] Figure 5 A flowchart of another code conversion method provided by an exemplary embodiment of the present application;

[0029] Figure 6 A schematic diagram of an application for parsing archive files provided by an exemplary embodiment of the present application;

[0030] Figure 7 A schematic diagram of an application for performing field type conversion processing on a first field provided by an exemplary embodiment of the present application;

[0031] Figure 8 A code conversion device provided for an exemplary embodiment of the present application;

[0032] Figure 9 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0033] To make the purpose, technical solutions, and advantages of this application more clear, the technical solutions of this application will be clearly and completely described below in conjunction with the specific embodiments of this application and the corresponding drawings. Obviously, the embodiments described are only part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.

[0034] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.

[0035] The various models involved in this application (including but not limited to language models or large models) are in compliance with relevant laws and standards.

[0036] With the rapid development of information technology and the continuous evolution of software architecture, enterprises are gradually migrating services based on the Java programming language to services based on the TypeScript programming language to better adapt to the needs of modern web development. As a statically typed superset of JavaScript, TypeScript has become the preferred language for building complex web applications due to its powerful type system, support for object-oriented programming, and seamless integration with modern front-end frameworks and toolchains. However, when migrating services based on Java programming to TypeScript programming, developers need to manually convert Java classes into TypeScript classes. This process is not only time-consuming and tedious, but also significantly increases the workload when faced with large amounts of code.

[0037] Furthermore, Java and TypeScript differ significantly in their language features and programming paradigms. Java is an object-oriented, statically typed language with strict type checking and a complex class inheritance structure. While TypeScript also supports object-oriented programming, its syntax and type system lean towards functional programming and flexible type definitions. For example, TypeScript allows for the use of interfaces to define data structures, while Java relies on classes and abstract classes to implement similar functionality. This difference makes manual conversion prone to errors, resulting in the converted TypeScript code not fully matching the functionality and performance requirements of the original Java code, and may even introduce potential runtime errors. Consequently, developers often need to invest significant time and effort in verifying and fixing code when performing manual conversion, further reducing development efficiency.

[0038] In response to the technical problems of low conversion efficiency and difficulty in ensuring conversion quality when migrating Java programming type services to TypeScript programming type services, the embodiment of the present application proposes a new code conversion solution. In this solution, by parsing the class files and sub-project object configuration files in the archive file that the service to be converted depends on, the basic class information and dependency relationships in the first code corresponding to the service to be converted are extracted, and then the fields and methods included in the first code are determined, and the types of the fields and methods in the code to be converted are converted to generate the target code in the second programming language, thereby realizing automatic code conversion between programming types in different languages. This can reduce the difficulty and workload of cross-language code conversion, reduce the errors that may occur in manual conversion, and thus improve the conversion efficiency and the accuracy of the conversion results.

[0039] The following is a detailed description of the code conversion solution provided in the embodiment of the present application in conjunction with the accompanying drawings.

[0040] Figure 1 The flowchart of a code conversion method provided by the exemplary embodiment of the present application is as follows. Figure 1 As shown, the execution subject of the method may be a code conversion device. Specifically, the method includes the following steps:

[0041] 101. In response to a code conversion request triggered for a service to be converted, obtain first code corresponding to the service to be converted and an archive file on which the service to be converted depends, where the first code is written in a first programming language.

[0042] 102. Parse the class files in the archive file to extract basic class information corresponding to each class in the first code from the class files.

[0043] 103. Parse the sub-project object configuration file in the archive file to obtain the dependency relationship corresponding to the first code.

[0044] 104. Based on the basic class information and dependency relationships corresponding to each class, determine the first field and the first method included in the first code.

[0045] 105. Perform field type conversion on the first field to obtain a converted second field, where the second field is a field in the second programming language.

[0046] 106. Perform parameter type conversion on the parameters in the first method to obtain a converted second method, where the second method is a method in the second programming language.

[0047] 107. Generate a second code corresponding to the service to be converted based on the second field and the second method.

[0048] The transcoding solution provided in the embodiments of the present application can provide a transcoding service between different programming languages ​​for a user terminal. In practical applications, when a user terminal wishes to perform service migration, it can trigger a transcoding request to a transcoding device for the service to be converted, requesting that the first code corresponding to the service to be converted, currently written in a first programming language, be converted into a code file written in a second programming language specified by the user terminal.

[0049] During specific implementation, when a code conversion request triggered for the service to be converted is received, in response to the code conversion request, the first code corresponding to the service to be converted and the archive file on which the service to be converted depends are obtained. The first code is a code written in a first programming language, and the first programming language can be a back-end programming language such as Java. The archive file is usually a packaged file (such as a JAR package, a WAR package, a ZIP package, etc.), and the archive file usually contains class files related to the service to be converted. These class files record the structure, field and method definitions of each class in the service to be converted. The archive file also contains configuration files or metadata files (for example, sub-project object configuration files), which record the dependency relationship between the service to be converted and other external libraries or modules and the configuration information of each object in the service to be converted (such as the type of the field, the parameters of the method, etc.).

[0050] In an optional embodiment, the specific implementation method of obtaining the archive file on which the service to be converted depends can be: obtaining the project object configuration file corresponding to the service to be converted, and based on the project object configuration file, determining the archive file on which the service to be converted depends and the sub-project object configuration file in the archive file.

[0051] For example, you can first determine the Java project corresponding to the service to be converted, and obtain the project object configuration file (pom.xml file) corresponding to the Java project from the code repository to determine the project object configuration file corresponding to the service to be converted. Then, parse the pom.xml file of the Java project to determine the archive files corresponding to each external library that the project depends on, and based on the archive files, determine the archive files that the service to be converted depends on, so as to obtain the archive files that the service to be converted depends on. Among them, a project may include multiple service modules, that is, the project object configuration file corresponding to a project may include multiple sub-project object configuration files corresponding to the services to be converted.

[0052] After obtaining the archive file that the service to be converted depends on, the class files in the archive file are parsed to extract the basic class information corresponding to each class in the first code. The basic class information refers to the core attributes and structure information of each class in the service to be converted. This information is the key data extracted and processed during the code conversion process.

[0053] By parsing the class files in the archive, we can extract basic class information, which provides a basis for subsequent field and method type conversion, ensuring that the generated second code has the same logical structure and functionality as the first code in the target language. Basic class information typically includes package name, class name (Class Name), inheritance relationship (Inheritance Information), field information (Field Information), method information (Method Information), modifiers and access control (Modifiers and Access Control), annotations and documentation information, and other information.

[0054] Furthermore, the class files in the archive file may be parsed using a Java bytecode parsing tool, such as ClassReader, to parse the bytecode information of the class files to extract basic class information corresponding to each class in the first code.

[0055] In practical applications, in order to more accurately implement automatic code conversion processing for the first code, after obtaining the basic class information corresponding to each class in the first code, the basic class information corresponding to each class can be further parsed to parse the class field information to obtain the field name, field type, annotation, and other information corresponding to the field in each class. The method information of each class can also be parsed to obtain the parameters, return value, annotation, and other information corresponding to the method in each class. At the same time, the class inheritance relationship can also be processed based on the basic class information of each class.

[0056] Furthermore, in actual applications, after obtaining the archive file that the service to be converted depends on, the corresponding sub-project object configuration file can be obtained from within the archive file, and the sub-project object configuration file within the archive file can be parsed to obtain the dependency relationships corresponding to the first code. The project object configuration file corresponding to a project includes sub-project object configuration files corresponding to multiple services or modules. The sub-project object configuration files within the archive file define all dependencies of the service to be converted.

[0057] Among them, a project object configuration file parsing tool can be used to parse the sub-project object configuration file in the archive file. For example, PomReader is used to parse the sub-project object configuration file to obtain the dependency relationship corresponding to the first code. The dependency relationship refers to the interdependent structure between various modules, classes or methods in the first code. For example, a class inherits another class, or a method calls a method of another class. During code conversion, the determination of the dependency relationship can ensure that the structure and logic of the generated second code remain consistent with the original code.

[0058] In an alternative embodiment, a decompilation tool can be used to restore the class file to source code. The interdependencies between modules, classes, or methods in the first code can be extracted by analyzing the classes, methods, and fields in the source code. In another alternative embodiment, a tool such as Dom4j, JAXB, or SAX can be used to parse the subproject object configuration file to obtain the interdependencies between modules, classes, or methods in the first code.

[0059] As can be seen from the above description, by parsing the class files within the archive, detailed information about the fields and methods of each class in the first programming language code can be extracted. By parsing the sub-project object configuration files within the archive, the logical structure and dependency hierarchy of the service to be converted can be further clarified, ensuring that the structure and logic of the converted second code are consistent with the first code, thereby ensuring the accuracy of the conversion results.

[0060] Then, based on the class basic information corresponding to each class in the first code and the dependency relationship corresponding to the first code, the first field and the first method contained in the first code are determined. Among them, the main differences between different programming languages ​​are syntax, data types, function definitions and calling methods. Then, in an embodiment of the present application, when performing code conversion processing, the first code corresponding to the service to be converted can be structurally analyzed first to determine the first field and the first method contained in the first code, and the first field in the first code is subjected to field type conversion processing to generate a second field that meets the requirements of the second programming language, and the parameters in the first method in the first code are subjected to parameter type conversion processing to generate a second method that meets the requirements of the second coding language. Among them, the second field is a field under the second programming language, and the second method is a method under the second programming language.

[0061] That is, by converting the field types corresponding to each field in the first code and the parameter types of each method in the first code, it is possible to adapt to the grammatical and semantic differences between the first programming language and the second programming language, thereby ensuring the correctness and consistency of the second code generated in the second programming language.

[0062] Finally, based on the converted second field and the second method, a second code corresponding to the service to be converted is generated. In order to generate a higher quality second code, a code template can be used to generate the second code based on the converted second field and the second method.

[0063] During specific implementation, a code template corresponding to the second programming language can be generated in advance based on the analysis results corresponding to the service to be converted, and the converted second field and second method can be filled into the corresponding code template to generate the second code corresponding to the service to be converted.

[0064] In another optional embodiment, multiple preset code templates corresponding to various programming languages ​​may be pre-set, where each preset code template is a code template of a different type, such as an entity class code template, a service class code template, an enumeration class code template, a type definition template, etc. Thus, after obtaining the converted second field and second method, the second field and second method can be filled into the corresponding preset code template based on the field type corresponding to the second field and the method type corresponding to the second method to generate the second code corresponding to the service to be converted. This allows for more accurate processing of various types of fields and methods, supports the generation of multiple types of code, and generates a second code that better meets the requirements.

[0065] In summary, the embodiment of the present application parses the archive file on which the service to be converted depends and the sub-project object configuration file in the archive file, extracts the basic class information and dependency relationship, and then determines the first field and the first method contained in the first code, and then performs field type conversion on the first field and parameter type conversion on the parameters in the first method, and finally generates a second code that conforms to the second programming language, thereby realizing automatic code conversion between programming types in different languages, reducing the difficulty and workload of cross-language code conversion, and improving the code conversion efficiency and the accuracy of the conversion results.

[0066] In order to facilitate the specific implementation process of extracting the basic class information corresponding to each class in the first code file in the above embodiment, the following introduces a feasible method of parsing the class files in the archive file to extract the basic class information corresponding to each class in the first code from the class files.

[0067] Figure 2 This is a flow chart of extracting the basic information of each class corresponding to the first code provided by the exemplary embodiment of the present application. Figure 2 As shown, this embodiment provides a specific implementation method for parsing class files in an archive file to extract class basic information corresponding to each class in the first code from the class files, which may include the following steps:

[0068] 201. Decompress the archive file to obtain the class file corresponding to the service to be converted.

[0069] 202. Parse the bytecode information of the class file through reflection or a bytecode parsing library to extract basic class information corresponding to each class in the first code from the class file.

[0070] The archive file is usually a compressed file. After the archive file on which the service to be converted depends is obtained, the archive file can be decompressed, and the class file corresponding to the service to be converted can be extracted from the decompressed multiple files.

[0071] Next, the bytecode information of the class file is parsed by transmitting the information to extract the basic class information corresponding to each class in the first code from the class file. Alternatively, the bytecode information of the class file is parsed in conjunction with a bytecode parsing library to extract the basic class information corresponding to each class in the first code from the class file.

[0072] In an optional embodiment, the specific implementation method of parsing the bytecode information of the class file to extract the class basic information corresponding to each class in the first code from the class file can be: reading the constant pool corresponding to the class file. Retrieve the class basic information corresponding to each class from the constant pool. Among them, the constant pool is used to store constants generated during compilation, that is, the constant pool is equivalent to the dictionary of the class file, and the names, types, strings and other information of all constants are stored in the constant pool. In actual applications, each field or each method can query the corresponding name, type, modifier, annotation, etc. from the constant pool. Its class basic information includes at least one of the following: the name of the class, the parent class, the implemented interface, the field and method declaration.

[0073] In an embodiment of the present application, the class file corresponding to the service to be converted is obtained by decompressing the archive file, and the bytecode information of the class file is parsed through reflection or a bytecode parsing library to extract the basic class information corresponding to each class in the first code from the class file. The basic class information corresponding to each class can be automatically obtained, and then the code structure corresponding to the first code can be obtained.

[0074] After extracting the basic class information corresponding to each class, the basic class information corresponding to each class can also be parsed to obtain the field information corresponding to the fields in each class and the method information corresponding to the methods in each class. In this way, in the subsequent execution process, the first field and the first method contained in the first code can be determined directly based on the field information corresponding to each field and the method information corresponding to each method, and combined with the dependency relationship between each field and each method. In this way, the multiple first fields and multiple first methods contained in the first code can be automatically analyzed more accurately to ensure that the generated second code has the same structure and composition as the first code.

[0075] Specifically, in an optional embodiment, the implementation process of using the constant pool to parse the basic class information corresponding to each class to obtain the field information corresponding to the field in each class and the method information corresponding to the method in each class can be: through the index corresponding to each class, retrieve the constant corresponding to each class in the first code from the constant pool; through the constant pool data reading function, read the field information corresponding to each constant to determine the field information corresponding to each class in the first code; through the constant pool data reading function, read the method information corresponding to each constant to determine the method information corresponding to each class in the first code; parse the field information corresponding to each constant to extract the field type corresponding to each field in each class in the first code; parse the method information corresponding to each constant to extract the parameter type corresponding to the parameter in each method in each class in the first code.

[0076] Furthermore, when parsing class field information and class method information, the information in the first code can be parsed layer by layer. For example, the class files in the archive can be parsed first to obtain basic information corresponding to each entity class. Then, each entity class can be parsed to obtain the corresponding short name, full name, inheritance relationship, parent entity, field set, signature information, whether it is an enumeration type, and detailed information about the enumeration. Next, each field in the field set of the entity class can be parsed. When parsing each field, the constant pool can be used to index and retrieve the constants corresponding to each class in the first code. Different parsing processes can then be performed based on the constant type corresponding to each constant. For example, if the constant type is UTF8, the constant can be directly converted to a string. If the constant type is Class, the class name is read. If the constant type is FieldRef / MethodRef, the class name, constant name, and corresponding descriptor can be obtained. If the constant type is NameAndType, the constant name and corresponding descriptor can be obtained.

[0077] In addition, in practical applications, the constant pool can also be used to analyze the dependencies between classes, the dependencies between fields, the dependencies between methods, etc. in the first code.

[0078] The specific implementation process of performing field type conversion processing on the first field included in the first code in each of the above embodiments is described in detail below.

[0079] Figure 3 The following is a flow chart of a process for converting the first field contained in the first code to a field type provided by an exemplary embodiment of the present application. Figure 3 As shown, the embodiment of the present application provides an implementation method for performing field type conversion processing on a first field included in a first code. Specifically, the method includes the following steps:

[0080] 301. Detect whether the field type corresponding to the first field is a preset field type.

[0081] 302. In response to detecting that the first field is of a preset field type, based on a field type conversion mapping relationship between the first programming language and the second programming language, convert a first field type in the first programming language corresponding to the first field into a second field type in the second programming language.

[0082] 303. Generate a converted second field based on the first field and the second field type.

[0083] When performing field type conversion processing on the first field, different conversion processing methods may be used for different field types to implement field conversion processing on fields of various types.

[0084] During specific implementation, it is possible to first detect whether the field type corresponding to the first field is a preset field type. The preset field type may be a preset basic field type. For example, the preset field type may be an integer type, a floating-point type, a character type, a Boolean type, an object type, etc. Furthermore, it is possible to detect whether the field type corresponding to the first field is a preset field type by using regular expression matching or a preset detection function.

[0085] When it is detected that the first field is of the preset field type, in response to detecting that the first field is of the preset field type, based on a field type conversion mapping relationship between the first programming language and the second programming language, the first field type corresponding to the first field in the first coding language is converted to a second field type in the second programming language. Furthermore, a converted second field is generated based on the first field and the second field types.

[0086] In cross-programming language development or data interaction scenarios, the field type conversion mapping relationship is a key issue. Different programming languages ​​have different definitions and processing methods for data types. In order to automatically and accurately transfer data in the first programming language and align it with program logic, in an embodiment of the present application, a mapping relationship between the field type in the first programming language and the field type in the second programming language can be pre-stored. In this way, when the field type conversion process is subsequently performed on the first field in the first code, the field type in the first programming language can be automatically converted to the field type in the second programming language.

[0087] For example, the first programming language is the Java programming language and the second programming language is the TypeScript programming language. The Java to TypeScript type mapping relationship can be pre-stored, for example: the lang.Integer type under the Java programming language corresponds to the number type under the TypeScript programming language; the Integer type in Java corresponds to the number type in TypeScript; the lang.String type in Java corresponds to the string type in TypeScript; the String type in Java corresponds to the string type in TypeScript, the util.List type in Java corresponds to the Array type in TypeScript, and so on.

[0088] In addition, in actual applications, the first field may also be a complex field type, for example, a generic type, an enumeration type, etc. In the embodiment of the present application, in order to better handle the field conversion processing of various complex types and accurately obtain the second field in the second programming language, different processing methods can be used for different complex field types.

[0089] Then, when it is detected that the first field is not of the preset field type, the field type can be further detected and processed by using a generic regular expression to match the string corresponding to the first field to determine whether the field type corresponding to the first field is a generic type.

[0090] Specifically, when it is detected that the field type corresponding to the first field is a generic type, in response to determining that the field type corresponding to the first field is a generic type, deconstruction assignment is used to extract the basic part of the generic type corresponding to the first field and the generic parameter part corresponding to the first field from the string; according to the commas contained in the generic parameter part, the string corresponding to the generic parameter part is split into multiple first parameters; the first string type corresponding to the multiple first parameters is respectively converted to the second string type in the second programming language to obtain multiple converted second parameters; the multiple second parameters are reconnected using commas to generate a new generic type string. Wherein, the first string type is the second string type in the first programming language.

[0091] Similarly, when it is detected that the field type corresponding to the first field is not a generic type, the field type corresponding to the first field is a custom type, and the type definition can be searched to generate a field type that complies with the second programming language based on the type definition. The custom type can be a complex type such as an array or a collection.

[0092] In an embodiment of the present application, by detecting whether the field type corresponding to the first field is a preset field type, when it is detected that the first field is the preset field type, the first field type in the first programming language corresponding to the first field is directly converted to the second field type in the second programming language based on the field type conversion mapping relationship between the first programming language and the second programming language, thereby realizing automatic mapping of the first programming language type to the second programming language type, and can also handle type conversion processing of complex types.

[0093] The above embodiment describes the specific implementation process of performing field type conversion on the first field. The specific implementation process of performing parameter type conversion on the parameters in the first method is described in detail below.

[0094] Figure 4 The following is a flow chart of a parameter type conversion process for parameters in the first method provided by an exemplary embodiment of the present application. Figure 4 As shown, the embodiment of the present application provides an implementation method for performing parameter type conversion processing on the parameters in the first method to obtain the converted second method. Specifically, the method includes the following steps:

[0095] 401. Detect whether a parameter type corresponding to a parameter in the first method is a preset parameter type.

[0096] 402. In response to detecting that a parameter type corresponding to a parameter in the first method is a preset parameter type, based on a parameter type conversion mapping relationship between the first programming language and the second programming language, convert a first parameter type in the first programming language corresponding to the parameter in the first method to a second parameter type in the second programming language.

[0097] 403. Generate a converted second method based on the first method and the second parameter type.

[0098] When performing parameter type conversion on the parameters in the first method, it is possible to first detect whether the parameter type corresponding to the parameter in the first method is a preset parameter type. The preset parameter type here can be an integer type, a floating-point type, a character type, a Boolean type, an object type, etc. Furthermore, it is possible to detect whether the parameter type corresponding to the parameter in the first method is the preset parameter type by regular expression matching or a preset detection function.

[0099] When it is detected that the parameter type corresponding to the parameter in the first method is a preset parameter type, in response to detecting that the parameter type corresponding to the parameter in the first method is the preset parameter type, based on the parameter type conversion mapping relationship between the first programming language and the second programming language, the first parameter type in the first programming language corresponding to the parameter in the first method can be converted to a second parameter type in the second programming language, and a converted second method can be generated based on the first method and the second parameter type.

[0100] That is, when performing programming language conversion on the first method, the parameter types in the first method are mainly converted. After obtaining the corresponding parameter types in the second programming language, the parameter types corresponding to each parameter in the first method are directly replaced to obtain the second method corresponding to the second programming language.

[0101] In addition, in actual applications, when it is detected that the parameter type corresponding to the parameter in the first method is not the preset parameter type, a generic regular expression can be used to match the string corresponding to the parameter to determine whether the parameter type corresponding to the parameter is a generic type. If it is a generic type, the parameter type conversion processing can be performed on the parameter using generic processing rules. If it is not a generic type, the parameter type conversion processing can be performed on the parameter according to the definition corresponding to the custom type. Among them, the specific processing process of the parameter type of the parameter in the first method is roughly the same as the specific implementation process of the field type conversion processing of the first field mentioned above. The specific implementation process of the above embodiment can be referred to and will not be repeated here.

[0102] In an embodiment of the present application, by detecting whether the parameter type corresponding to the parameter in the first method is a preset parameter type, when it is detected that the parameter in the first method is the preset parameter type, the first parameter type in the first programming language in the first method is directly converted to the second parameter type in the second programming language based on the parameter type conversion mapping relationship between the first programming language and the second programming language, thereby realizing automatic mapping of the first programming language type to the second programming language type, and can also handle type conversion processing of complex types.

[0103] Furthermore, in practical applications, the first code often includes a large amount of annotation information. Therefore, to ensure that the converted second code maintains consistency with the first code, when performing code conversion on the first code, after obtaining the converted second field and second method, the annotation information included in the first code can also be detected and converted into annotations in the corresponding second programming language. This process is described in detail in conjunction with the following embodiments.

[0104] Figure 5 FIG. 1 is a flow chart of another code conversion method provided by an exemplary embodiment of the present application. Figure 5As shown, based on the above embodiment, specifically, the method may further include the following steps:

[0105] 501. Detect whether each character string in the first code is a code annotation.

[0106] 502. In response to detecting that the target string is a code annotation, determine an annotation name corresponding to the target string, where the target string is any one of multiple strings in the first code.

[0107] 503. Based on the annotation name, search for a target annotation corresponding to the annotation name from a preset annotation mapping table.

[0108] 504. Generate a second code corresponding to the service to be converted based on the second field, the second method, and the target annotation corresponding to each character string in the first code.

[0109] In practice, you can pre-set a development rule where each service entity class and each field in the application has an SCFMember annotation corresponding to the corresponding type. Pre-store the SCFMember annotations corresponding to each entity class in the application. When performing annotation processing, simply match the corresponding annotation name to obtain the corresponding annotation string.

[0110] When performing annotation processing, it is first possible to detect whether each string in the first code is a code annotation, that is, to determine which strings in the first code are annotations. When it is detected that the target string is a code annotation, in response to detecting that the target string is a code annotation, the target annotation corresponding to the annotation name can be searched from a preset annotation mapping table based on the annotation name corresponding to each code annotation. After obtaining the target annotation corresponding to each string in the first code, the second code corresponding to the to-be-converted service can be generated based on the converted second field, the second method, and the target annotation corresponding to each string in the first code.

[0111] Among them, in an optional embodiment, the specific implementation process of generating the second code corresponding to the service to be converted based on the converted second field, the second method, and the target annotations corresponding to each string in the first code can be: based on the second field, the second field type corresponding to the second field, the field information corresponding to each class in the first code, and the target annotations corresponding to each string in the first code, generate an entity object; fill the entity object into a preset entity class code template to generate a first sub-code; based on the second method, the second parameter type corresponding to the parameter in the second method, the field information corresponding to each class in the first code, and the target annotations corresponding to each string in the first code, generate a service object; fill the service object into a preset service class code template to generate a second sub-code; based on the first sub-code and the second sub-code, generate the second code corresponding to the service to be converted.

[0112] In an embodiment of the present application, by parsing the archive file on which the service to be converted depends and the sub-project object configuration file in the archive file, basic class information and dependency relationships are extracted, and then the first field and the first method contained in the first code are determined, field type conversion is performed on the first field, and parameter type conversion is performed on the parameters in the first method, and code annotations in the first code are detected. Based on the annotation name corresponding to the code annotation, the target annotation corresponding to the annotation name is searched from a preset annotation mapping table, and finally, based on the target annotation corresponding to the second field, the second method and each string in the first code, a second code that conforms to the second programming language is generated, thereby realizing automatic code conversion between programming types in different languages, reducing the difficulty and workload of cross-language code conversion, and improving code conversion efficiency and the accuracy of conversion results.

[0113] In order to facilitate the understanding of the specific implementation process of the above solution, the following Figure 6-7 The implementation process of code conversion for the code of the service to be converted in a specific application scenario is illustrated with examples. When a developer wants to switch a Java type service to a TypeScript type service, the code conversion system provided in the embodiment of this application can be used to complete the service migration. In specific implementation:

[0114] First, the developer can trigger a code conversion request for the service to be converted. Specifically, the code conversion system can also provide a code conversion interface for the user, in which the developer can enter the JAR package path corresponding to the service to be converted to trigger the code conversion request for the service to be converted.

[0115] Upon receiving a code conversion request triggered by a developer, the system responds to the code conversion request triggered for the service to be converted, obtains the first code corresponding to the service to be converted, and the JAR package (archive file) on which the service to be converted depends. It parses the class files in the JAR package to extract basic class information corresponding to each class in the first code. It also parses the sub-project object configuration files in the archive file to obtain the dependency relationships corresponding to the first code.

[0116] Specifically, such as Figure 6 As shown, JarParser is used to decompress the JAR package to read the Class file (class file) in the JAR package, and ClassReader is used to parse the bytecode in the Class file and return the corresponding bytecode information, wherein the bytecode information includes the basic class information corresponding to each class in the first code extracted from the Class file, such as the package name, class name, parent class, etc. The pom.xml file (sub-project object configuration file) in the JAR package is read, and PomReader is used to parse the pom.xml file to obtain the dependency information corresponding to the first code and return the corresponding dependency information.

[0117] After obtaining the class basic information corresponding to each class in the first code and the dependency relationship corresponding to the first code, the first field and the first method included in the first code are determined based on the class basic information and the dependency relationship corresponding to each class.

[0118] In actual applications, in order to more accurately analyze the code structure and corresponding semantics corresponding to the first code, after obtaining the basic class information corresponding to each class, the field information of each class and the method information of the class can be further parsed, and then based on the field information of each class and the method information of the class and the corresponding dependency relationship, the first field and the first method contained in the first code can be determined.

[0119] In an optional embodiment, code annotations contained in the first code may also be simultaneously verified. Specifically, each string in the first code is detected to determine whether it is a code annotation; in response to detecting that a target string is a code annotation, an annotation name corresponding to the target string is determined, where the target string is any one of the multiple strings in the first code; and based on the annotation name, a target annotation corresponding to the annotation name is searched from a preset annotation mapping table.

[0120] Next, the first field is converted to a second field, and the parameters in the first method are converted to a second method. The second field is a field in TypeScript, and the second method is a method in TypeScript.

[0121] Different conversion processing methods can be used for different types of fields, such as Figure 7 When performing field type conversion processing on the first field, the field type corresponding to the first field can be first determined, and it can be judged whether the field type corresponding to the first field is a preset field type. When it is detected that the field type corresponding to the first field is the preset field type, in response to detecting that the field type corresponding to the first field is the preset field type, the field type corresponding to the first field is mapped to the corresponding TypeScript type directly based on the mapping relationship between the Java type and the TypeScript type to obtain a converted field type, and based on the converted field type and the first field, a converted second field is generated.

[0122] When it is detected that the field type corresponding to the first field is not the preset field type, a generic regular expression is then used to match the string corresponding to the first field to determine whether the field type corresponding to the first field is a generic type. If it is detected that the field type corresponding to the first field is a generic type, in response to the fact that the field type corresponding to the first field is a generic type, deconstruction assignment is used to extract the basic part of the generic type corresponding to the first field and the generic parameter part corresponding to the first field from the string. According to the commas contained in the generic parameter part, the string corresponding to the generic parameter part is split into multiple first parameters. The first string types corresponding to the multiple first parameters are respectively converted to the second string type under TypeScript to obtain multiple converted second parameters. The multiple second parameters are reconnected using commas to generate a new generic type string. Based on the new generic type string, the converted second field is generated.

[0123] When it is detected that the field type corresponding to the first field is not a generic type, in response to the fact that the field type corresponding to the first field is not a generic type, a custom type processing method is used to perform field type conversion processing on the first field to find the type definition corresponding to the first field, and based on the type definition, a converted second field is generated.

[0124] Next, parameter type conversion is performed on the parameters in the first method. Specifically, it is detected whether the parameter type corresponding to the parameter in the first method is a preset parameter type. In response to detecting that the parameter type corresponding to the parameter in the first method is the preset parameter type, based on the mapping relationship between Java types and TypeScript types, the first parameter type under Java corresponding to the parameter in the first method is converted to the second parameter type under TypeScript. Based on the first method and the second parameter type, a converted second method is generated. Similarly, the parameter type conversion processing of the parameters in the first method can also be processed in a similar manner to the above-mentioned field conversion processing, which will not be repeated here.

[0125] After obtaining the converted second field, the converted second method, and the target annotation in the first code, a template engine can be used to generate a corresponding code template based on the pom.xml file corresponding to the service to be converted. Code templates can include entity class templates, service class templates, enumeration class templates, type definition templates, etc. The converted second field, the converted second method, and the target annotation in the first code are then added to the corresponding code template to generate the second code corresponding to the service to be converted.

[0126] Among them, an optional specific implementation method for generating the second code is: based on the second field, the second field type corresponding to the second field, the field information corresponding to each class in the first code, and the target annotation corresponding to each string in the first code, generate an entity object, fill the entity object into a preset entity class code template, and generate a first sub-code. Based on the second method, the second parameter type corresponding to the parameter in the second method, the field information corresponding to each class in the first code, and the target annotation corresponding to each string in the first code, generate a service object, fill the service object into a preset service class code template, and generate a second sub-code. Based on the first sub-code and the second sub-code, generate the second code corresponding to the service to be converted.

[0127] In this way, the above method can automatically complete the code conversion from Java to TypeScript, reduce the workload of manual conversion, speed up the service migration, and avoid human conversion errors to improve the accuracy of the code conversion structure.

[0128] The following will describe in detail the code conversion device of one or more embodiments of the present application. Those skilled in the art will understand that these devices can be constructed by using commercially available hardware components and configuring them according to the steps taught in this solution.

[0129] Figure 8 A code conversion device is provided for an exemplary embodiment of the present application, such as Figure 8 As shown, the device includes: an acquisition module 11, an extraction module 12, a parsing module 13, a first conversion module 14, a second conversion module 15, and a generation module 16.

[0130] The acquisition module 11 is configured to acquire, in response to a code conversion request triggered for a service to be converted, a first code corresponding to the service to be converted and an archive file on which the service to be converted depends, wherein the first code is written in a first programming language.

[0131] The extraction module 12 is configured to parse the class files in the archive file to extract basic class information corresponding to each class in the first code from the class files.

[0132] The parsing module 13 is used to parse the sub-project object configuration file in the archive file, obtain the dependency relationship corresponding to the first code, and determine the first field and the first method contained in the first code based on the class basic information corresponding to each class and the dependency relationship.

[0133] The first conversion module 14 is configured to perform field type conversion on the first field to obtain a converted second field, where the second field is a field in the second programming language.

[0134] The second conversion module 15 is configured to perform parameter type conversion on the parameters in the first method to obtain a converted second method, where the second method is a method in the second programming language.

[0135] The generating module 16 is configured to generate a second code corresponding to the service to be converted based on the second field and the second method.

[0136] Optionally, the first conversion module 14 is specifically used to: detect whether the field type corresponding to the first field is a preset field type; in response to detecting that the field type corresponding to the first field is a preset field type, based on the field type conversion mapping relationship between the first programming language and the second programming language, convert the first field type in the first programming language corresponding to the first field into a second field type in the second programming language; based on the first field and the second field types, generate a converted second field.

[0137] Optionally, the first conversion module 14 is also used to: in response to detecting that the first field is not a preset field type, use a generic regular expression to match the string corresponding to the first field to determine whether the field type corresponding to the first field is a generic type; in response to determining that the field type corresponding to the first field is the generic type, use deconstruction assignment to extract the basic part of the generic type corresponding to the first field and the generic parameter part corresponding to the first field from the string; according to the commas contained in the generic parameter part, split the string corresponding to the generic parameter part into multiple first parameters; convert the first string types corresponding to the multiple first parameters into the second string type under the second programming language respectively, and obtain multiple second parameters after conversion, the first string type is the second string type under the first programming language; use commas to reconnect the multiple second parameters to generate a new generic type string.

[0138] Optionally, the second conversion module 15 is specifically used to: detect whether the parameter type corresponding to the parameter in the first method is a preset parameter type; in response to detecting that the parameter type corresponding to the parameter in the first method is the preset parameter type, based on the parameter type conversion mapping relationship between the first programming language and the second programming language, convert the first parameter type in the first programming language corresponding to the parameter in the first method into a second parameter type in the second programming language; based on the first method and the second parameter type, generate a converted second method.

[0139] Optionally, before generating the second code corresponding to the service to be converted based on the second field and the second method, the device also includes a code annotation module, which is specifically used to: detect whether each character string in the first code is a code annotation; in response to detecting that the target character string is a code annotation, determine the annotation name corresponding to the target character string, and the target character string is any one of the multiple characters in the first code; based on the annotation name, search for the target annotation corresponding to the annotation name from a preset annotation mapping table.

[0140] Optionally, the generating module 16 is specifically configured to generate a second code corresponding to the service to be converted based on the second field, the second method, and target annotations corresponding to each character string in the first code.

[0141] Optionally, the acquisition module 11 is specifically configured to: acquire a project object configuration file corresponding to the service to be converted; and determine, based on the project object configuration file, an archive file on which the service to be converted depends and a sub-project object configuration file in the archive file.

[0142] Optionally, the extraction module 12 is specifically used to: decompress the archive file to obtain the class file corresponding to the service to be converted; parse the bytecode information of the class file through reflection or bytecode parsing library to extract the basic class information corresponding to each class in the first code from the class file.

[0143] Optionally, the extraction module 12 is specifically used to: read a constant pool corresponding to the class file, the constant pool being used to store constants generated during compilation; and retrieve basic class information corresponding to each class from the constant pool, the basic class information including at least one of the following: the name of the class, the parent class, the implemented interface, the field and method declaration.

[0144] Optionally, the extraction module 12 is further used to: retrieve the constants corresponding to each class in the first code from the constant pool through the index corresponding to each class; read the field information corresponding to each constant through the constant pool data reading function to determine the field information corresponding to each class in the first code; read the method information corresponding to each constant through the constant pool data reading function to determine the method information corresponding to each class in the first code; parse the field information corresponding to each constant to extract the field type corresponding to each field in each class in the first code; parse the method information corresponding to each constant to extract the parameter type corresponding to the parameter in each method in each class in the first code.

[0145] Optionally, the generation module 16 is specifically used to: generate an entity object based on the second field, the second field type corresponding to the second field, the field information corresponding to each class in the first code, and the target annotation corresponding to each string in the first code; fill the entity object into a preset entity class code template to generate a first sub-code; generate a service object based on the second method, the second parameter type corresponding to the parameters in the second method, the field information corresponding to each class in the first code, and the target annotation corresponding to each string in the first code; fill the service object into a preset service class code template to generate a second sub-code; generate a second code corresponding to the service to be converted based on the first sub-code and the second sub-code.

[0146] Figure 9 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Figure 9 As shown, in practice, the electronic device includes: a memory 21 and a processor 22.

[0147] The memory 21 is used to store computer programs and can be configured to store various other data to support operations on the electronic device. Examples of such data include instructions for any application or method operating on the electronic device, data structures, contact data, phone book data, messages, images, videos, etc.

[0148] The processor 22 is coupled to the memory 21 and is configured to execute the computer program in the memory 21 to implement the code conversion method provided in the aforementioned embodiment.

[0149] Further, if Figure 9 As shown, the electronic device also includes: a communication component 23, a display 24, a power component 25, an audio component 26 and other components. Figure 9 Only some components are shown schematically, which does not mean that the electronic device only includes Figure 9The electronic device of this embodiment can be implemented as a terminal device such as a desktop computer, a laptop computer, a smart phone or an IOT device, or a server device such as a conventional server, a cloud server or a server array.

[0150] The above-mentioned memory can be implemented by any type of volatile or non-volatile memory device or a combination thereof, such as static random-access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk.

[0151] The communication component is configured to facilitate wired or wireless communication between the device in which the communication component resides and other devices. The device in which the communication component resides can access a wireless network based on a communication standard, such as a 2G, 3G, 4G / LTE, 5G, or other mobile communication network, or a combination thereof. In an exemplary embodiment, the communication component receives broadcast signals or broadcast-related information from an external broadcast management system via a broadcast channel.

[0152] The above-mentioned display includes a screen, which may include a liquid crystal display (LCD) and a touch panel (TP). If the screen includes a touch panel, the screen may be implemented as a touch screen to receive input signals from the user. The touch panel includes one or more touch sensors to sense touch, slide, and gestures on the touch panel. The touch sensor can not only sense the boundary of the touch or slide action, but also detect the duration and pressure associated with the touch or slide operation.

[0153] The power supply assembly provides power to various components of the device in which the power supply assembly is located. The power supply assembly may include a power management system, one or more power supplies, and other components associated with generating, managing, and distributing power to the device in which the power supply assembly is located.

[0154] The above-mentioned audio component can be configured to output and / or input audio signals. For example, the audio component includes a microphone (MIC), and when the device where the audio component is located is in an operating mode, such as call mode, recording mode, and voice recognition mode, the microphone is configured to receive external audio signals. The received audio signal can be further stored in a memory or sent via a communication component. In some embodiments, the audio component also includes a speaker for outputting audio signals.

[0155] Accordingly, an embodiment of the present application further provides a computer-readable storage medium storing a computer program. When the computer program is executed by a processor, the processor is enabled to implement each step in the above method embodiment.

[0156] The computer-readable storage medium may be volatile or non-volatile, or a combination thereof, and may be removable or non-removable. Examples of computer-readable storage media include, but are not limited to, phase-change random access memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, magnetic tape, disk storage or other magnetic storage devices, or any other non-transmission medium.

[0157] Accordingly, an embodiment of the present application further provides a computer program product, which includes a computer program or instructions. When the computer program or instructions are executed by a processor, the processor is enabled to implement the steps in the above method embodiment.

[0158] It should be understood that each process or a combination of multiple processes in the above method processes can be implemented by computer programs or instructions. In addition, these computer programs or instructions can be applied to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device, so that the processor of the general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing device can be implemented as a device for implementing the corresponding functions in the above method embodiments.

[0159] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.

Claims

1. A code conversion method, characterized in that: include: In response to a code conversion request triggered for a service to be converted, obtaining a first code corresponding to the service to be converted and an archive file on which the service to be converted depends, wherein the first code is written in a first programming language; Parsing the class files in the archive file to extract basic class information corresponding to each class in the first code from the class files; Parsing the sub-project object configuration file in the archive file to obtain dependency relationships corresponding to the first code, and determining a first field and a first method included in the first code based on basic class information corresponding to each class and the dependency relationships; Performing field type conversion on the first field to obtain a converted second field, where the second field is a field in the second programming language; Performing parameter type conversion on the parameters in the first method to obtain a converted second method, where the second method is a method in the second programming language; Based on the second field and the second method, a second code corresponding to the service to be converted is generated.

2. The method according to claim 1, characterized in that The performing field type conversion on the first field to obtain the converted second field includes: Detecting whether the field type corresponding to the first field is a preset field type; In response to detecting that the field type corresponding to the first field is a preset field type, based on a field type conversion mapping relationship between the first programming language and the second programming language, converting the first field type in the first programming language corresponding to the first field to a second field type in the second programming language; A converted second field is generated based on the first field and the second field type.

3. The method according to claim 2, characterized in that The method further comprises: In response to detecting that the first field is not of a preset field type, matching a string corresponding to the first field using a generic regular expression to determine whether the field type corresponding to the first field is a generic type; In response to determining that the field type corresponding to the first field is the generic type, extracting a basic portion of the generic type corresponding to the first field and a generic parameter portion corresponding to the first field from the string using destructuring assignment; Splitting the string corresponding to the generic parameter part into multiple first parameters according to the commas contained in the generic parameter part; Converting first string types corresponding to the multiple first parameters into second string types in the second programming language respectively, to obtain multiple converted second parameters, where the first string type is the second string type in the first programming language; The multiple second parameters are reconnected using commas to generate a new generic type string.

4. The method according to claim 1, wherein The performing parameter type conversion on the parameters in the first method to obtain the converted second method includes: Detecting whether the parameter type corresponding to the parameter in the first method is a preset parameter type; In response to detecting that a parameter type corresponding to a parameter in the first method is a preset parameter type, converting a first parameter type in the first programming language corresponding to the parameter in the first method to a second parameter type in the second programming language based on a parameter type conversion mapping relationship between the first programming language and the second programming language; A converted second method is generated based on the first method and the second parameter type.

5. The method according to claim 1, wherein Before generating the second code corresponding to the service to be converted based on the second field and the second method, the method further includes: Detecting whether each character string in the first code is a code annotation; In response to detecting that a target character string is a code annotation, determining an annotation name corresponding to the target character string, the target character string being any one of a plurality of character strings in the first code; Based on the annotation name, searching for a target annotation corresponding to the annotation name from a preset annotation mapping table; The step of generating a second code corresponding to the service to be converted based on the second field and the second method includes: Based on the second field, the second method, and the target annotations corresponding to the respective character strings in the first code, a second code corresponding to the service to be converted is generated.

6. The method according to claim 1, characterized in that The obtaining of the archive file on which the service to be converted depends includes: Obtaining a project object configuration file corresponding to the service to be converted; Based on the project object configuration file, the archive file on which the service to be converted depends and the sub-project object configuration file in the archive file are determined.

7. The method according to claim 1, characterized in that Parsing the class files in the archive file to extract basic class information corresponding to each class in the first code from the class files includes: Decompress the archive file to obtain the class file corresponding to the service to be converted; The bytecode information of the class file is parsed through reflection or a bytecode parsing library to extract basic class information corresponding to each class in the first code from the class file.

8. The method according to claim 7, characterized in that Parsing the bytecode information of the class file to extract basic class information corresponding to each class in the first code includes: Read the constant pool corresponding to the class file, where the constant pool is used to store constants generated during compilation; The basic class information corresponding to each class is retrieved from the constant pool, where the basic class information includes at least one of the following: a class name, a parent class, an implemented interface, a field, and a method declaration.

9. The method according to claim 8, characterized in that The method further comprises: Retrieving constants corresponding to the respective classes in the first code from the constant pool according to the indexes corresponding to the respective classes; Reading the field information corresponding to each constant through the constant pool data reading function to determine the field information corresponding to each class in the first code; Reading method information corresponding to each constant through a constant pool data reading function to determine method information corresponding to each class in the first code; Parsing the field information corresponding to each constant to extract the field type corresponding to each field in each class in the first code; The method information corresponding to each constant is parsed to extract the parameter type corresponding to the parameter in each method in each class in the first code.

10. The method according to claim 9, characterized in that The generating, based on the second field, the second method, and the target annotation corresponding to each string in the first code, a second code corresponding to the service to be converted includes: Generate an entity object based on the second field, a second field type corresponding to the second field, field information corresponding to each class in the first code, and a target annotation corresponding to each string in the first code; Filling the entity object into a preset entity class code template to generate a first subcode; Generate a service object based on the second method, a second parameter type corresponding to a parameter in the second method, field information corresponding to each class in the first code, and a target annotation corresponding to each string in the first code; Filling the service object into a preset service class code template to generate a second subcode; A second code corresponding to the service to be converted is generated based on the first sub-code and the second sub-code.

11. An electronic device, characterized in that: include: memory and processor; The memory is used to store a computer program; the processor is coupled to the memory and is used to execute the computer program to implement the steps in the code conversion method according to any one of claims 1 to 10.

12. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the processor is caused to implement the steps in the code conversion method according to any one of claims 1 to 10.

13. A computer program product comprising a computer program / instructions, characterized in that When the computer program / instructions are executed by a processor, the processor is caused to implement the steps in the code conversion method according to any one of claims 1 to 10.

Citation Information

Cited By

  • Zero-intrusion-oriented compiling period type conversion generation method

    CN121979502A