CN111240681B - Conversion method and device for different programming languages - Google Patents

Conversion method and device for different programming languages Download PDF

Info

Publication number
CN111240681B
CN111240681B CN201911400722.5A CN201911400722A CN111240681B CN 111240681 B CN111240681 B CN 111240681B CN 201911400722 A CN201911400722 A CN 201911400722A CN 111240681 B CN111240681 B CN 111240681B
Authority
CN
China
Prior art keywords
metadata
programming language
class
code
conversion
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201911400722.5A
Other languages
Chinese (zh)
Other versions
CN111240681A (en
Inventor
陈盼盼
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Ruiting Network Technology Shanghai Co ltd
Original Assignee
Ruiting Network Technology Shanghai Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Ruiting Network Technology Shanghai Co ltd filed Critical Ruiting Network Technology Shanghai Co ltd
Priority to CN201911400722.5A priority Critical patent/CN111240681B/en
Publication of CN111240681A publication Critical patent/CN111240681A/en
Application granted granted Critical
Publication of CN111240681B publication Critical patent/CN111240681B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

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

Abstract

The embodiment of the invention provides a conversion method and a conversion device for different programming languages. The method comprises the following steps: acquiring metadata corresponding to each class in the initial function code; wherein the initial function code is a code conforming to a first programming language specification comprising a plurality of different types of classes; adjusting the codes identical to the keywords to codes different from the keywords, and adjusting the identical method name codes to different method name codes to obtain intermediate metadata; registering classes in the intermediate metadata, and respectively performing differentiation processing on the generic class, the base class and the enumeration class in the intermediate metadata to obtain target metadata conforming to the second programming language specification; and inputting the target metadata into a preset template to generate a target function code. The embodiment of the invention can automatically convert the function codes conforming to the first programming language specification into the function codes conforming to the second programming language specification, the whole conversion process is convenient and simple, and meanwhile, various problems caused by manual conversion are solved.

Description

Conversion method and device for different programming languages
Technical Field
The present invention relates to the field of programming language conversion, and in particular, to a method and apparatus for converting different programming languages.
Background
Java is an object-oriented programming language, not only absorbs various advantages of the C++ language, but also abandons concepts such as multiple inheritance and pointers which are difficult to understand in the C++, so that the Java language has two characteristics of powerful functions, simplicity and easiness in use. Java is thus one of the most popular programming languages. PHP (Hypertext preprocessor, PHP: hypertext Preprocessor) is a universal open source scripting language. PHP is a script language executed on the server side, and is a common website programming language similar to the C language.
Many software projects are currently written in Java, with some of the functional code sometimes being required to be applied in the context of other programming languages. Such as client code in a Java-written RPC (remote procedure call ) service.
However, client code written in Java cannot be used directly in the context of the PHP programming language. The client code for the PHP version is typically written manually by a PHP software engineer. However, the manual writing mode has higher requirements on software engineers, large writing difficulty and long writing time, and simultaneously, great learning cost is also required.
Disclosure of Invention
The embodiment of the invention provides a conversion method and a conversion device for different programming languages, which are used for solving the problems of high difficulty, long time consumption and high error rate caused by manually converting Java codes into PHP codes in the prior art.
In a first aspect, an embodiment of the present invention provides a method for converting different programming languages, where the method includes:
acquiring metadata corresponding to each class in the initial function code; wherein the initial function code is code conforming to a first programming language specification comprising a plurality of different types of classes;
detecting whether the metadata has the same code as the keywords in a preset information table or not and whether the metadata corresponding to the same class has the same method name code or not; the same code as the keyword is adjusted to be different from the keyword, the same method name code is adjusted to be different method name codes, and intermediate metadata are obtained, wherein the preset information table comprises a plurality of keywords in a second programming language;
registering classes in the intermediate metadata, and respectively performing differentiation processing on the generic class, the basic class and the enumeration class in the intermediate metadata to obtain target metadata conforming to a second programming language specification;
and inputting the target metadata into a preset template to generate target function codes, wherein the preset template is a template which is generated according to the initial function codes and accords with the second programming language specification.
Optionally, the step of obtaining metadata corresponding to each class in the initial function code includes:
and acquiring metadata of each class in the initial function code according to a reflection mechanism in the first programming language specification, and establishing a corresponding relation between the metadata and the class.
Optionally, the step of detecting whether the metadata has the same code as the keyword in the preset information table includes:
and sequentially acquiring each keyword in the preset information table, and inquiring codes identical to each keyword in the metadata according to a preset regular expression.
Optionally, registering the class in the intermediate metadata, and performing differentiation processing on the generic class, the base class and the enumeration class in the intermediate metadata respectively, so as to obtain the target metadata meeting the second programming language specification, where the step of obtaining the target metadata includes:
converting classes except for the difference types in the intermediate metadata into data structures conforming to a second programming language specification to obtain first conversion metadata, wherein the difference types are generic, basic and enumerated classes;
registering entity classes in the intermediate metadata to generate registration information conforming to a second programming language specification;
for the generic type and the base class respectively, adding an identification field in the registration information and assigning the registration information, and taking the registration information added with the identification field and assigned as second conversion metadata;
converting the enumeration class in the intermediate metadata into second programming language enumeration according to a pre-generated enumeration list meeting a second programming language specification, and taking the second programming language enumeration as third conversion metadata;
and generating target metadata according to the first conversion metadata, the second conversion metadata and the third conversion metadata.
Optionally, the first programming language is Java programming language; the second programming language is a PHP programming language.
Optionally, after the step of entering the target metadata into a preset template and generating a target function code, the method further includes:
generating an annotation extraction regular expression according to the annotation rule in the first programming language;
extracting regular expressions according to the annotations, and extracting annotation information in the metadata;
and generating an annotation document according to the extracted annotation information.
In a second aspect, an embodiment of the present invention further provides a conversion apparatus for different programming languages, where the apparatus includes:
the acquisition module is used for acquiring metadata corresponding to each class in the initial function code; wherein the initial function code is code conforming to a first programming language specification comprising a plurality of different types of classes;
the first processing module is used for detecting whether the metadata have the same codes as the keywords in the preset information table or not and whether the metadata corresponding to the same type have the same method name codes or not; the same code as the keyword is adjusted to be different from the keyword, the same method name code is adjusted to be different method name codes, and intermediate metadata are obtained, wherein the preset information table comprises a plurality of keywords in a second programming language;
the second processing module is used for registering classes in the intermediate metadata and respectively carrying out differentiation processing on the generic class, the basic class and the enumeration class in the intermediate metadata to obtain target metadata conforming to a second programming language specification;
the generation module is used for inputting the target metadata into a preset template to generate target function codes, wherein the preset template is generated according to the initial function codes and accords with the second programming language specification.
Optionally, the acquiring module is specifically configured to acquire metadata of each class in the initial function code according to a reflection mechanism in the first programming language specification, and establish a correspondence between the metadata and the class.
Optionally, the first processing module is specifically configured to obtain each keyword in the preset information table in sequence, and query, according to a preset regular expression, a code identical to each keyword in the metadata.
Optionally, the second processing module includes:
the first conversion unit is used for converting classes except for the difference type in the intermediate metadata into a data structure conforming to the second programming language specification to obtain first conversion metadata, wherein the difference type is a generic type, a basic type and an enumeration type;
a registration unit, configured to register entity classes in the intermediate metadata, and generate registration information that accords with a second programming language specification;
the second conversion unit is used for adding an identification field into the registration information and assigning values for the generic class and the base class respectively, and taking the registration information added with the identification field and assigned values as second conversion metadata;
the third conversion unit is used for converting the enumeration class in the intermediate metadata into second programming language enumeration according to a pre-generated enumeration list meeting the second programming language specification, and taking the second programming language enumeration as third conversion metadata;
and the generating unit is used for generating target metadata according to the first conversion metadata, the second conversion metadata and the third conversion metadata.
Optionally, the first programming language is Java programming language; the second programming language is a PHP programming language.
Optionally, the apparatus further comprises:
the regular expression module is used for generating an annotation extraction regular expression according to the annotation rule in the first programming language;
the extraction module is used for extracting regular expressions according to the annotations and extracting annotation information in the metadata;
and the annotation module is used for generating an annotation document according to the extracted annotation information.
In a third aspect, an embodiment of the present invention further provides an electronic device, where the electronic device includes a memory, a processor, and a computer program stored on the memory and executable on the processor, where the processor implements steps in the method of converting different programming languages as described above when the computer program is executed.
In a fourth aspect, embodiments of the present invention also provide a computer readable storage medium having stored thereon a computer program which, when executed by a processor, implements steps in a method of converting different programming languages as described above.
In the embodiment of the invention, metadata corresponding to each class in the initial function code is firstly acquired; wherein the initial function code is a code conforming to a first programming language specification comprising a plurality of different types of classes; detecting whether the metadata has the same code as the keywords in the preset information table or not and whether the metadata corresponding to the same class has the same method name code or not; and adjusting the codes identical to the keywords to codes different from the keywords, and adjusting the identical method name codes to different method name codes to obtain intermediate metadata, wherein the preset information table comprises a plurality of keywords in a second programming language. Since the intermediate metadata does not contain keywords in the second programming language, problems caused by the keywords are avoided when the intermediate metadata is directly used in codes conforming to the second programming language specification; and no duplication exists in the method names corresponding to the same class in the intermediate metadata, so that the method names in the intermediate metadata accord with the specification of the second programming language. Registering classes in the intermediate metadata, and respectively performing differentiation processing on the generic class, the base class and the enumeration class in the intermediate metadata to obtain target metadata conforming to the second programming language specification; and performing differentiation processing based on the differences of the universal type, the base class and the enumeration class in the first programming language and the second programming language, so that the target metadata obtained after processing accords with the specification of the second programming language. And inputting the target metadata into a preset template to generate target function codes, wherein the preset template is a template which is generated according to the initial function codes and accords with the second programming language specification. The embodiment of the invention can automatically convert the function codes conforming to the first programming language specification into the function codes conforming to the second programming language specification, and solves the problems of high difficulty, long time consumption and high error rate caused by manually converting Java codes into PHP codes in the prior art.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings that are needed in the description of the embodiments of the present invention will be briefly described below, it being obvious that the drawings in the following description are only some embodiments of the present invention, and that other drawings may be obtained according to these drawings without inventive effort for a person skilled in the art.
FIG. 1 is a flowchart illustrating a method for converting different programming languages according to an embodiment of the present invention;
FIG. 2 is a flowchart of the steps of the differentiation processing according to the embodiment of the present invention;
FIG. 3 is a schematic diagram illustrating an application of a conversion method of different programming languages according to an embodiment of the present invention;
FIG. 4 is a schematic diagram of a special grammar processing procedure according to an embodiment of the invention;
FIG. 5 is a block diagram illustrating a configuration of a conversion device according to an embodiment of the present invention;
FIG. 6 is a block diagram illustrating a second processing module according to an embodiment of the present invention;
fig. 7 is a block diagram of an electronic device according to an embodiment of the present invention.
Detailed Description
The following description of the embodiments of the present invention will be made clearly and fully with reference to the accompanying drawings, in which it is evident that the embodiments described are some, but not all embodiments of the invention. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
It should be appreciated that reference throughout this specification to "one embodiment" or "an embodiment" means that a particular feature, structure or characteristic described in connection with the embodiment is included in at least one embodiment of the present invention. Thus, the appearances of the phrases "in one embodiment" or "in an embodiment" in various places throughout this specification are not necessarily all referring to the same embodiment. Furthermore, the particular features, structures, or characteristics may be combined in any suitable manner in one or more embodiments.
In various embodiments of the present invention, it should be understood that the sequence numbers of the following processes do not mean the order of execution, and the order of execution of the processes should be determined by the functions and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
Referring to fig. 1, an embodiment of the present invention provides a method for converting different programming languages, including:
step 101, obtaining metadata corresponding to each class in the initial function code.
It should be noted that the initial function code is code conforming to the first programming language specification including a plurality of different types of classes; preferably, the first programming language is Java programming language; the second programming language is the PHP programming language. The initial function code is RPC client written in Java programming language. The metadata mainly comprises code annotation, class metadata information and RPC framework defined metadata information; the acquired metadata has an association relationship with classes, and the classes are classes in a programming language.
The metadata may be obtained by using a reflection mechanism in the Java programming language, and specifically, the step of obtaining metadata corresponding to each class in the initial function code includes: and acquiring metadata of each class in the initial function code according to a reflection mechanism in the first programming language specification, and establishing a corresponding relation between the metadata and the class.
Step 102, detecting whether the metadata has the same code as the keywords in the preset information table and whether the metadata corresponding to the same class has the same method name code; and adjusting the codes identical to the keywords to codes different from the keywords, and adjusting the identical method name codes to different method name codes to obtain the intermediate metadata.
It should be noted that the preset information table includes a plurality of keywords in the second programming language. In order to avoid that the code in the metadata is identified as a keyword in the second programming language, the code in the metadata that is the same as the keyword in the second programming language needs to be modified to be different from the code of the keyword in the second programming language, for example, an underline may be added and then a number sequence number may be added next to the underline, but is not limited thereto. Because of the grammar limitation in PHP, the same name method cannot exist in the same class, so the same method name code in the same class in metadata needs to be adjusted to different method name codes. So that the same code as the key in the second programming language does not exist in the intermediate metadata; and the method names corresponding to the same class have no rename.
Preferably, the step of detecting whether the same code as the keyword in the preset information table exists in the metadata includes: and sequentially acquiring each keyword in the preset information table, and inquiring the codes identical to each keyword in the metadata according to the preset regular expression. When detecting whether the same method name code exists in the metadata corresponding to the same class, regular expressions can be utilized as well, and details are not repeated here.
And 103, registering classes in the intermediate metadata, and respectively performing differentiation processing on the generic class, the basic class and the enumeration class in the intermediate metadata to obtain target metadata conforming to the second programming language specification.
It should be noted that the PHP programming language is a weak language, on the one hand parameters may not specify types and Java is that all parameters have to specify types, on the other hand the basic types of PHP definitions and Java cannot be in one-to-one correspondence. Therefore, differentiation processing is required for these differences.
And 104, inputting the target metadata into a preset template to generate a target function code.
It should be noted that the preset template is a template generated from the initial function code and conforming to the second programming language specification. The preset templates may be pre-written according to the initial function code.
In the embodiment of the invention, metadata corresponding to each class in an initial function code is firstly obtained; wherein the initial function code is a code conforming to a first programming language specification comprising a plurality of different types of classes; detecting whether the metadata has the same code as the keywords in the preset information table or not and whether the metadata corresponding to the same class has the same method name code or not; and adjusting the codes identical to the keywords to codes different from the keywords, and adjusting the identical method name codes to different method name codes to obtain intermediate metadata, wherein the preset information table comprises a plurality of keywords in a second programming language. Since the intermediate metadata does not contain keywords in the second programming language, problems caused by the keywords are avoided when the intermediate metadata is directly used in codes conforming to the second programming language specification; and no duplication exists in the method names corresponding to the same class in the intermediate metadata, so that the method names in the intermediate metadata accord with the specification of the second programming language. Registering classes in the intermediate metadata, and respectively performing differentiation processing on the generic class, the base class and the enumeration class in the intermediate metadata to obtain target metadata conforming to the second programming language specification; and performing differentiation processing based on the differences of the universal type, the base class and the enumeration class in the first programming language and the second programming language, so that the target metadata obtained after processing accords with the specification of the second programming language. And inputting the target metadata into a preset template to generate target function codes, wherein the preset template is a template which is generated according to the initial function codes and accords with the second programming language specification. The embodiment of the invention can automatically convert the function codes conforming to the first programming language specification into the function codes conforming to the second programming language specification, and solves the problems of high difficulty, long time consumption and high error rate caused by manually converting Java codes into PHP codes in the prior art.
As shown in fig. 2, in order to implement the differentiation processing, in the embodiment of the present invention, the steps of registering a class in the intermediate metadata and performing the differentiation processing on a generic class, a basic class and an enumeration class in the intermediate metadata respectively, to obtain target metadata conforming to the second programming language specification include:
step 201, converting classes except for the difference type in the intermediate metadata into a data structure conforming to the second programming language specification, and obtaining the first conversion metadata.
It should be noted that if the difference type is directly converted into a data structure conforming to the second programming language specification, an error will occur during the program operation, and therefore, additional processing of the difference type is required during the conversion. Preferably, the difference types are generic, base and enumeration types.
And 202, registering entity classes in the intermediate metadata to generate registration information conforming to the second programming language specification.
And 203, adding an identification field in the registration information and assigning the identification field for the generic class and the base class respectively, and taking the registration information added with the identification field and assigned as second conversion metadata.
It should be noted that the generic type and the base class are generic types and base classes in the Java programming language, and are not described herein.
Step 204, according to the pre-generated enumeration list meeting the second programming language specification, converting the enumeration class in the intermediate metadata into the second programming language enumeration, and taking the second programming language enumeration as the third conversion metadata.
It should be noted that the enumeration class model in the PHP programming language is written in advance, and then the enumeration class in the Java programming language is converted using the enumeration class model.
In step 205, target metadata is generated according to the first conversion metadata, the second conversion metadata, and the third conversion metadata.
It should be noted that the target metadata is metadata conforming to the second programming language specification, and may be directly entered into a template written in the second programming language.
In order to facilitate a programmer to view the target function code, in the embodiments of the present invention, after the step of entering the target metadata into the preset template and generating the target function code, the method further includes:
generating an annotation extraction regular expression according to an annotation rule in the first programming language;
extracting regular expressions according to the notes, and extracting note information in the metadata;
and generating an annotation document according to the extracted annotation information.
It should be noted that an annotation document conforming to the PSR specification can be generated according to the PSR (PHP Standards Recommendation) specification.
FIG. 3 is a schematic diagram illustrating the conversion method of different programming languages according to an embodiment of the present invention; comprising the following steps:
step 301, start.
Step 302, maven Config. A Maven plug-in tool is provided to generate PHP clients. The generation parameters are first configured, including whether to initialize entity default values, whether to process reloads, serialized versions, version information, and the like.
Step 303, is jar (Java archive), judging whether the file is jar format file, if yes, executing step 304, and if not, executing step 306. Mainly provides two supports, one is that Java service side directly operates plug-in generation under the current project warehouse, and the other is that for supporting the generation of third party service, jar and source information is required to be downloaded through maven
Step 304, downloading jar.
Step 305, download source.
In step 306, metadata is extracted, and all metadata information is extracted by reflection technology in combination with RPC custom annotations.
Step 307, special grammar, which is to process the differences between two programming languages, including type differences, keyword differences, enumeration support, reload support, map key type conversion, etc.; clients are generated using pre-refined templates and metadata after special grammar processing.
In step 308, annotation information is generated according to the pre-refined Regex (regular expression) and the generated clients.
In step 309, other units are generated, and in order to facilitate debugging, corresponding unit tests of all RPCs are generated at the same time, and only a very small configuration is required for development to realize the call to the remote method. And generating legal composer structure, interface proxy class, user can directly make remote call based on composer dependent client, and no other extra code is needed. And version information, author information and the like are generated, so that the traceability problem is facilitated.
At step 310, the git (distributed version control system) is submitted, supporting direct submission of the git to the remote repository based on the configuration. One key is realized to complete all PHP client related work.
Step 311 ends.
FIG. 4 is a schematic diagram of a special syntax processing procedure according to an embodiment of the present invention; comprising the following steps:
step 401, start.
Step 402, detecting whether the metadata has the same code as the keyword, if yes, executing step 403, and if not, executing step 404. Wherein the keywords are all keywords in the PHP programming language.
In step 403, the mapping process modifies the same code as the key to a different code from the key.
Step 404, reloading, detecting whether the same method name code exists in the metadata corresponding to the same class; if yes, go to step 405, if not, go to step 406.
And step 405, performing alias processing, namely modifying the same method name code in the metadata corresponding to the same class into different method name codes.
In step 406, the type is registered.
Step 407, detecting whether the metadata has a generic type, if yes, executing step 408, otherwise executing step 409.
And 408, adding identification information to the generic type by the parameter type, and carrying out assignment.
Step 409, detecting whether there is a base class in the metadata, if yes, executing step 410, and if not, executing step 411.
In step 410, the type is registered, the identification information is added to the base class, and assignment is performed.
Step 411, is Map, detect if there is enumeration class in the metadata, if yes, go to step 412, if not, go to step 413.
In step 412, key toSrting, rewrites the_tosrring method.
Step 413, packaging parameters.
Step 414, end.
Having described the conversion methods of different programming languages provided by the embodiments of the present invention, the conversion apparatus of different programming languages provided by the embodiments of the present invention will be described below with reference to the accompanying drawings.
Referring to fig. 5 and 6, the embodiment of the present invention further provides a conversion device for different programming languages, where the device includes:
an obtaining module 51, configured to obtain metadata corresponding to each class in the initial function code; wherein the initial function code is a code conforming to a first programming language specification comprising a plurality of different types of classes;
a first processing module 52, configured to detect whether the metadata has the same code as the keyword in the preset information table, and whether the metadata corresponding to the same class has the same method name code; the same code as the keyword is adjusted to be different from the keyword, the same method name code is adjusted to be different method name codes, and intermediate metadata are obtained, wherein a preset information table comprises a plurality of keywords in a second programming language;
the second processing module 53 is configured to register a class in the intermediate metadata, and perform differential processing on the generic class, the basic class, and the enumerated class in the intermediate metadata, so as to obtain target metadata that meets a second programming language specification;
the generating module 54 is configured to enter the target metadata into a preset template, and generate a target function code, where the preset template is a template that is generated according to the initial function code and meets the second programming language specification.
Optionally, the obtaining module 51 is specifically configured to obtain metadata of each class in the initial function code according to a reflection mechanism in the first programming language specification, and establish a correspondence between the metadata and the class.
Optionally, the first processing module 52 is specifically configured to sequentially obtain each keyword in the preset information table, and query, according to a preset regular expression, the metadata for the same code as each keyword.
Optionally, the second processing module 53 includes:
the first conversion unit 531 is configured to convert classes in the intermediate metadata except for the difference types into a data structure conforming to the second programming language specification, so as to obtain first conversion metadata, where the difference types are a generic class, a basic class and an enumeration class;
a registration unit 532, configured to register entity classes in the intermediate metadata, and generate registration information that accords with the second programming language specification;
a second conversion unit 533, configured to add an identification field to the registration information and perform assignment for the generic class and the base class, and use the registration information added with the identification field and assigned as second conversion metadata;
a third conversion unit 534, configured to convert the enumeration class in the intermediate metadata into a second programming language enumeration according to a pre-generated enumeration list that conforms to the second programming language specification, and use the second programming language enumeration as third conversion metadata;
the generating unit 535 is configured to generate target metadata according to the first conversion metadata, the second conversion metadata, and the third conversion metadata.
Optionally, the first programming language is a Java programming language; the second programming language is the PHP programming language.
Optionally, the apparatus further comprises:
the regular expression module is used for generating an annotation extraction regular expression according to the annotation rule in the first programming language;
the extraction module is used for extracting regular expressions according to the annotations and extracting annotation information in the metadata;
and the annotation module is used for generating an annotation document according to the extracted annotation information.
The conversion device for different programming languages provided in the embodiment of the present invention can implement each process implemented by the conversion method for different programming languages in the method embodiments of fig. 1 and fig. 2, and in order to avoid repetition, a description is omitted here.
In the embodiment of the invention, metadata corresponding to each class in an initial function code is firstly obtained; wherein the initial function code is a code conforming to a first programming language specification comprising a plurality of different types of classes; detecting whether the metadata has the same code as the keywords in the preset information table or not and whether the metadata corresponding to the same class has the same method name code or not; and adjusting the codes identical to the keywords to codes different from the keywords, and adjusting the identical method name codes to different method name codes to obtain intermediate metadata, wherein the preset information table comprises a plurality of keywords in a second programming language. Since the intermediate metadata does not contain keywords in the second programming language, problems caused by the keywords are avoided when the intermediate metadata is directly used in codes conforming to the second programming language specification; and no duplication exists in the method names corresponding to the same class in the intermediate metadata, so that the method names in the intermediate metadata accord with the specification of the second programming language. Registering classes in the intermediate metadata, and respectively performing differentiation processing on the generic class, the base class and the enumeration class in the intermediate metadata to obtain target metadata conforming to the second programming language specification; and performing differentiation processing based on the differences of the universal type, the base class and the enumeration class in the first programming language and the second programming language, so that the target metadata obtained after processing accords with the specification of the second programming language. And inputting the target metadata into a preset template to generate target function codes, wherein the preset template is a template which is generated according to the initial function codes and accords with the second programming language specification. The embodiment of the invention can automatically convert the function codes conforming to the first programming language specification into the function codes conforming to the second programming language specification, and solves the problems of high difficulty, long time consumption and high error rate caused by manually converting Java codes into PHP codes in the prior art.
In another aspect, an embodiment of the present invention further provides an electronic device, including a memory, a processor, a bus, and a computer program stored in the memory and capable of running on the processor, where the processor implements steps in the above-mentioned conversion method of different programming languages when executing the program.
For example, fig. 7 shows a schematic physical structure of an electronic device.
As shown in fig. 7, the electronic device may include: processor 710, communication interface (Communications Interface) 720, memory 730, and communication bus 740, wherein processor 710, communication interface 720, memory 730 communicate with each other via communication bus 740. Processor 710 may call logic instructions in memory 730 to perform the following method:
acquiring metadata corresponding to each class in the initial function code; wherein the initial function code is a code conforming to a first programming language specification comprising a plurality of different types of classes;
detecting whether the metadata has the same code as the keywords in the preset information table or not and whether the metadata corresponding to the same class has the same method name code or not; the same code as the keyword is adjusted to be different from the keyword, the same method name code is adjusted to be different method name codes, and intermediate metadata are obtained, wherein a preset information table comprises a plurality of keywords in a second programming language;
registering classes in the intermediate metadata, and respectively performing differentiation processing on the generic class, the base class and the enumeration class in the intermediate metadata to obtain target metadata conforming to the second programming language specification;
and inputting the target metadata into a preset template to generate target function codes, wherein the preset template is a template which is generated according to the initial function codes and accords with the second programming language specification.
Further, the logic instructions in the memory 730 described above may be implemented in the form of software functional units and may be stored in a computer readable storage medium when sold or used as a stand alone product. Based on this understanding, the technical solution of the present invention may be embodied essentially or in a part contributing to the prior art or in a part of the technical solution, in the form of a software product stored in a storage medium, comprising several instructions for causing a computer device (which may be a personal computer, a server, a network device, etc.) to perform all or part of the steps of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a random access Memory (RAM, random Access Memory), a magnetic disk, or an optical disk, or other various media capable of storing program codes.
In still another aspect, an embodiment of the present invention further provides a computer readable storage medium having stored thereon a computer program, which when executed by a processor is implemented to perform the method for converting different programming languages provided in the above embodiments, for example, including:
acquiring metadata corresponding to each class in the initial function code; wherein the initial function code is a code conforming to a first programming language specification comprising a plurality of different types of classes;
detecting whether the metadata has the same code as the keywords in the preset information table or not and whether the metadata corresponding to the same class has the same method name code or not; the same code as the keyword is adjusted to be different from the keyword, the same method name code is adjusted to be different method name codes, and intermediate metadata are obtained, wherein a preset information table comprises a plurality of keywords in a second programming language;
registering classes in the intermediate metadata, and respectively performing differentiation processing on the generic class, the base class and the enumeration class in the intermediate metadata to obtain target metadata conforming to the second programming language specification;
and inputting the target metadata into a preset template to generate target function codes, wherein the preset template is a template which is generated according to the initial function codes and accords with the second programming language specification.
The apparatus embodiments described above are merely illustrative, wherein the elements illustrated as separate elements may or may not be physically separate, and the elements shown as elements may or may not be physical elements, may be located in one place, or may be distributed over a plurality of network elements. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment. Those of ordinary skill in the art will understand and implement the present invention without undue burden.
From the above description of the embodiments, it will be apparent to those skilled in the art that the embodiments may be implemented by means of software plus necessary general hardware platforms, or of course may be implemented by means of hardware. Based on this understanding, the foregoing technical solution may be embodied essentially or in a part contributing to the prior art in the form of a software product, which may be stored in a computer readable storage medium, such as ROM/RAM, a magnetic disk, an optical disk, etc., including several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute the method described in the respective embodiments or some parts of the embodiments.
Finally, it should be noted that: the above embodiments are only for illustrating the technical solution of the present invention, and are not limiting; although the invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical scheme described in the foregoing embodiments can be modified or some technical features thereof can be replaced by equivalents; such modifications and substitutions do not depart from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims (9)

1. A method of converting a different programming language, the method comprising:
acquiring metadata corresponding to each class in the initial function code; wherein the initial function code is code conforming to a first programming language specification comprising a plurality of different types of classes;
detecting whether the metadata has the same code as the keywords in a preset information table or not and whether the metadata corresponding to the same class has the same method name code or not; the same code as the keyword is adjusted to be different from the keyword, the same method name code is adjusted to be different method name codes, and intermediate metadata are obtained, wherein the preset information table comprises a plurality of keywords in a second programming language;
registering classes in the intermediate metadata, and respectively performing differentiation processing on the generic class, the basic class and the enumeration class in the intermediate metadata to obtain target metadata conforming to a second programming language specification;
inputting the target metadata into a preset template to generate target function codes, wherein the preset template is a template which is generated according to the initial function codes and accords with the second programming language specification;
registering classes in the intermediate metadata, and respectively performing differentiation processing on the generic class, the basic class and the enumeration class in the intermediate metadata to obtain target metadata conforming to a second programming language specification, wherein the step of obtaining target metadata comprises the following steps of:
converting classes except for the difference types in the intermediate metadata into data structures conforming to a second programming language specification to obtain first conversion metadata, wherein the difference types are generic, basic and enumerated classes;
registering entity classes in the intermediate metadata to generate registration information conforming to a second programming language specification;
for the generic type and the base class respectively, adding an identification field in the registration information and assigning the registration information, and taking the registration information added with the identification field and assigned as second conversion metadata;
converting the enumeration class in the intermediate metadata into second programming language enumeration according to a pre-generated enumeration list meeting a second programming language specification, and taking the second programming language enumeration as third conversion metadata;
and generating target metadata according to the first conversion metadata, the second conversion metadata and the third conversion metadata.
2. The method of claim 1, wherein the step of obtaining metadata corresponding to each class in the initial function code comprises:
and acquiring metadata of each class in the initial function code according to a reflection mechanism in the first programming language specification, and establishing a corresponding relation between the metadata and the class.
3. The method of claim 1, wherein the step of detecting whether the same code as the key in the preset information table exists in the metadata comprises:
and sequentially acquiring each keyword in the preset information table, and inquiring codes identical to each keyword in the metadata according to a preset regular expression.
4. The method of claim 1, wherein the first programming language is Java programming language; the second programming language is a hypertext preprocessor PHP programming language.
5. The method of claim 1, wherein after the step of entering the target metadata into a preset template, generating a target function code, the method further comprises:
generating an annotation extraction regular expression according to the annotation rule in the first programming language;
extracting regular expressions according to the annotations, and extracting annotation information in the metadata;
and generating an annotation document according to the extracted annotation information.
6. A conversion apparatus for different programming languages, the apparatus comprising:
the acquisition module is used for acquiring metadata corresponding to each class in the initial function code; wherein the initial function code is code conforming to a first programming language specification comprising a plurality of different types of classes;
the first processing module is used for detecting whether the metadata have the same codes as the keywords in the preset information table or not and whether the metadata corresponding to the same type have the same method name codes or not; the same code as the keyword is adjusted to be different from the keyword, the same method name code is adjusted to be different method name codes, and intermediate metadata are obtained, wherein the preset information table comprises a plurality of keywords in a second programming language;
the second processing module is used for registering classes in the intermediate metadata and respectively carrying out differentiation processing on the generic class, the basic class and the enumeration class in the intermediate metadata to obtain target metadata conforming to a second programming language specification;
the generation module is used for inputting the target metadata into a preset template to generate target function codes, wherein the preset template is a template which is generated according to the initial function codes and accords with the second programming language specification;
the second processing module includes:
the first conversion unit is used for converting classes except for the difference types in the intermediate metadata into data structures conforming to the second programming language specification to obtain first conversion metadata, wherein the difference types are generic, basic and enumerated;
the registration unit is used for registering entity classes in the intermediate metadata and generating registration information conforming to the second programming language specification;
the second conversion unit is used for adding an identification field into the registration information and assigning values for the generic class and the base class respectively, and taking the registration information added with the identification field and assigned values as second conversion metadata;
the third conversion unit is used for converting enumeration classes in the intermediate metadata into second programming language enumeration according to a pre-generated enumeration list meeting the second programming language specification, and taking the second programming language enumeration as third conversion metadata;
and the generating unit is used for generating target metadata according to the first conversion metadata, the second conversion metadata and the third conversion metadata.
7. The apparatus of claim 6, wherein the obtaining module is specifically configured to obtain metadata of each class in the initial function code according to a reflection mechanism in the first programming language specification, and establish a correspondence between metadata and class.
8. An electronic device comprising a processor, a memory and a computer program stored on the memory and executable on the processor, characterized in that the computer program when executed by the processor implements the steps of the method of converting different programming languages according to any of claims 1 to 5.
9. A computer readable storage medium, characterized in that the computer readable storage medium has stored thereon a computer program which, when executed by a processor, implements the steps of the method of converting different programming languages according to any of claims 1 to 5.
CN201911400722.5A 2019-12-30 2019-12-30 Conversion method and device for different programming languages Active CN111240681B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201911400722.5A CN111240681B (en) 2019-12-30 2019-12-30 Conversion method and device for different programming languages

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201911400722.5A CN111240681B (en) 2019-12-30 2019-12-30 Conversion method and device for different programming languages

Publications (2)

Publication Number Publication Date
CN111240681A CN111240681A (en) 2020-06-05
CN111240681B true CN111240681B (en) 2023-08-15

Family

ID=70864955

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201911400722.5A Active CN111240681B (en) 2019-12-30 2019-12-30 Conversion method and device for different programming languages

Country Status (1)

Country Link
CN (1) CN111240681B (en)

Families Citing this family (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112764763B (en) * 2020-12-31 2024-01-26 亿企赢网络科技有限公司 Code conversion method, system, equipment and computer readable storage medium
CN113377638B (en) * 2021-06-11 2023-10-27 北京百度网讯科技有限公司 Method and device for generating performance test tool

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101169718A (en) * 2006-10-23 2008-04-30 国际商业机器公司 System and method for instantiating abstract class
CN105426711A (en) * 2015-11-18 2016-03-23 北京理工大学 Similarity detection method of computer software source code
CN109271168A (en) * 2018-08-09 2019-01-25 上海鲸鱼机器人科技有限公司 A kind of code conversion method, device and equipment, storage medium

Family Cites Families (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7543271B2 (en) * 2003-09-08 2009-06-02 Microsoft Corporation Compiling source code using generic classes

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101169718A (en) * 2006-10-23 2008-04-30 国际商业机器公司 System and method for instantiating abstract class
CN105426711A (en) * 2015-11-18 2016-03-23 北京理工大学 Similarity detection method of computer software source code
CN109271168A (en) * 2018-08-09 2019-01-25 上海鲸鱼机器人科技有限公司 A kind of code conversion method, device and equipment, storage medium

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
马强 ; 刘佩林 ; .基于编程语言结构的通用配置代码生成方法.计算机工程.2010,(18),全文. *

Also Published As

Publication number Publication date
CN111240681A (en) 2020-06-05

Similar Documents

Publication Publication Date Title
US8091071B2 (en) Method and system for template-based code generation
CN109460219B (en) Method for quickly serializing interface control file
US20030115548A1 (en) Generating class library to represent messages described in a structured language schema
US10592220B2 (en) Metadata-driven binding of converted source code to original source code
CN111240681B (en) Conversion method and device for different programming languages
CN110109681B (en) Method and system for converting codes between different platforms
CN109857389B (en) Model data generation method and device, computer equipment and storage medium
CN109255107A (en) Configuration software processing method, device, computer equipment and storage medium
US20150278231A1 (en) System and method for customizing archive of a device driver generator tool for a user
CN112000320A (en) Automatic code generation method, device, equipment and storage medium
CN104461531A (en) Implementing method for self-defined functions of reporting system
CN111460241A (en) Data query method and device, electronic equipment and storage medium
CN110262784A (en) A kind of cloud notes implementation method and device
CN116227505A (en) Internationalization file translation method, device, equipment and medium
WO2023035563A1 (en) Applet cross-application migration method, device, terminal, system and storage medium
CN106570095B (en) XML data operation method and equipment
Rinner et al. Creating ISO/EN 13606 archetypes based on clinical information needs
Zhang et al. Automated Extraction of Grammar Optimization Rule Configurations for Metamodel-Grammar Co-evolution
EP3611616A1 (en) Software code optimizer and method
CN108932225A (en) For natural language demand to be converted into the method and system of semantic modeling language statement
CN111708542B (en) Test case generation method, system, equipment and storage medium
CN110955433B (en) Automatic deployment script generation method and device
CN110162307B (en) Method and device for converting JSON file into DLL file
CN117910433A (en) Document editing method, device, equipment and storage medium
US10929210B2 (en) Collaboration system protocol processing

Legal Events

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