CN113778398A - Code generation method and device, electronic equipment and storage medium - Google Patents

Code generation method and device, electronic equipment and storage medium Download PDF

Info

Publication number
CN113778398A
CN113778398A CN202111143523.8A CN202111143523A CN113778398A CN 113778398 A CN113778398 A CN 113778398A CN 202111143523 A CN202111143523 A CN 202111143523A CN 113778398 A CN113778398 A CN 113778398A
Authority
CN
China
Prior art keywords
code
character string
target
interface
executed
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202111143523.8A
Other languages
Chinese (zh)
Inventor
翟陆续
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Dajia Internet Information Technology Co Ltd
Original Assignee
Beijing Dajia Internet Information Technology 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 Beijing Dajia Internet Information Technology Co Ltd filed Critical Beijing Dajia Internet Information Technology Co Ltd
Priority to CN202111143523.8A priority Critical patent/CN113778398A/en
Publication of CN113778398A publication Critical patent/CN113778398A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/33Intelligent editors
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • G06F8/447Target code generation

Abstract

The present disclosure relates to a code generation method, apparatus, electronic device, and storage medium, the method comprising: executing a code generation logic corresponding to the code generation tool by responding to the code generation instruction, and calling a preset plug-in; the preset plug-in comprises a plurality of code editing methods, and the target character string in the generated code is replaced by the preset character string after being executed; secondly, in the execution process of the code generation logic, when detecting that the current segment to be executed belongs to any target segment, switching to a code editing method corresponding to the current segment to be executed in the execution plug-in, and after the execution of the code editing method is finished, returning to execute the next segment of the current segment to be executed; and then returning to execute the previous step until the code generation logic is completely executed to obtain the generated code. The method and the device realize real-time replacement of the target character string in the code generation process, so that the character string which is easy to cause confusion in the code is replaced by the character string with clear semantics, and the generated code quality is improved.

Description

Code generation method and device, electronic equipment and storage medium
Technical Field
The present disclosure relates to the field of computer technologies, and in particular, to a code generation method and apparatus, an electronic device, and a storage medium.
Background
MyBatis Generator (code Generator of persistence layer framework) is a code generation tool provided by MyBatis (persistence layer framework based on Java), and can generate a persistent object (DO) corresponding to a database table, an interface (Mapper) for operating a database, a query condition class (XXXXEmple), and the like.
However, in the partial code generated by MyBatis Generator, the partial class name and the method name are easy to cause confusion, and the official plug-in cannot completely replace the characters causing the confusion, so the semantics of the generated interface code is fuzzy, and the code generation quality is low.
Disclosure of Invention
The present disclosure provides a code generation method, apparatus, electronic device, and storage medium to at least solve the problem of low quality of code generation in the related art. The technical scheme of the disclosure is as follows:
according to a first aspect of embodiments of the present disclosure, there is provided a code generation method, including:
responding to the code generation instruction, executing code generation logic corresponding to the code generation tool, and calling a preset plug-in; the preset plug-in comprises a plurality of code editing methods, and the target character strings in the generated codes are replaced by preset character strings after the code editing methods are executed; the code editing methods are obtained by respectively rewriting based on a plurality of target segments in the code generation logic;
in the execution process of the code generation logic, when detecting that the current segment to be executed belongs to any target segment, switching to execute a code editing method corresponding to the current segment to be executed in the plug-in, and after the execution of the code editing method is finished, returning to execute the next segment of the current segment to be executed;
and returning to execute the previous step until the code generation logic is completely executed to obtain the generated code.
In an exemplary embodiment of the present invention,
when detecting that the current segment to be executed belongs to any one of the target segments, the method changes to execute the code editing method corresponding to the current segment to be executed in the plug-in, and comprises the following steps:
when detecting that the current segment to be executed belongs to the first target segment, switching to execute a first code editing method to obtain the preset character string; the first code editing method is a method for rewriting the first target segment;
when detecting that the current segment to be executed belongs to a second target segment, switching to execute a second code editing method to update the class name and the interface method name which comprise the target character string in the generated code, and replacing the target character string in the class name and the interface method name with a preset character string; the second code editing method is a method for rewriting the second target segment;
when detecting that the current segment to be executed belongs to a third target segment, switching to executing a third code editing method to update interface parameters and annotation information containing the target character string in the generated code, and replacing the target character string in the interface parameters and the annotation information with a preset character string; the third code editing method is a method for rewriting the third target segment;
when detecting that the current segment to be executed belongs to a fourth target segment, switching to executing a fourth code editing method so as to update configuration information in an interface configuration file containing the target character string in the generated code, and replacing the target character string in the configuration information with a preset character string; the fourth code editing method is a method of rewriting for the fourth target segment.
In an exemplary embodiment, the code generation tool is a code generator of MyBatis, and the code generation logic is generation logic that operates a database interface; the first code editing method is a rewritten parameter verification method;
the executing the first code editing method to obtain the preset character string includes:
calling a variable acquisition method function contained in the rewritten parameter verification method to acquire the following preset character strings: a first string for updating a class name of the query condition object class; a second string for updating an interface method name of the operational database interface; a third string for updating the interface parameters and the annotation information; and the fourth character string is used for updating the configuration information in the interface configuration file of the operation database.
In an exemplary embodiment, the second code editing method is a rewritten initialization method;
the executing the second code editing method to update the class name and the interface method name of the target character string included in the generated code, and replacing the target character string in the class name and the interface method name with a preset character string includes:
calling a first parameter replacement method function contained in the rewritten initialization method, and replacing the target character string contained in the class name of the generated query condition object class with the first character string;
and calling a second parameter replacement method function contained in the rewritten initialization method, and replacing the target character string contained in the interface method name in the generated operation database interface code with the second character string.
In an exemplary embodiment, the third code editing method is a rewritten generation calling method;
the executing the third code editing method to update the interface parameter and the annotation information containing the target character string in the generated code, and replacing the target character string in the interface parameter and the annotation information with a preset character string includes:
calling a parameter acquisition method function contained in the rewritten generated calling method, and replacing the target character string contained in the interface parameter in the generated operation database interface code with the third character string;
and calling an annotation acquisition method function contained in the rewritten generation calling method, and replacing the target character string contained in the release information in the generated operation database interface code with the third character string.
In an exemplary embodiment, the fourth code editing method is a rewritten database element generating method;
the executing the fourth code editing method to update the configuration information in the configuration file corresponding to the interface including the target character string in the generated code, and replacing the target character string of the configuration information in the configuration file corresponding to the interface with a preset character string includes:
and calling the rewritten database element generation method, and replacing the target character string contained in the configuration information in the configuration file corresponding to the interface in the generated code with the fourth character string.
In an exemplary embodiment, before the calling the preset plug-in, the method further includes:
acquiring a plug-in adapter of the code generation tool, and generating the preset plug-in based on the plug-in adapter, wherein the preset plug-in is generated by inheriting the plug-in adapter;
installing the preset plug-in the code generation tool.
According to a second aspect of the embodiments of the present disclosure, there is provided a code generation apparatus including:
the plug-in calling unit is configured to execute a code generation logic corresponding to the code generation tool in response to the code generation instruction, and call a preset plug-in; the preset plug-in comprises a plurality of code editing methods, and the target character strings in the generated codes are replaced by preset character strings after the code editing methods are executed; the code editing methods are obtained by respectively rewriting based on a plurality of target segments in the code generation logic;
the plug-in execution unit is configured to execute, in the execution process of the code generation logic, when detecting that a current segment to be executed belongs to any one of the target segments, a code editing method corresponding to the current segment to be executed in the plug-in is switched to be executed, and after the execution of the code editing method is finished, the next segment of the current segment to be executed is returned to be executed;
and the code generation unit is configured to execute the previous step and obtain the generated code until the code generation logic is completely executed.
In an exemplary embodiment, the plug-in execution unit is further configured to execute
When detecting that the current segment to be executed belongs to the first target segment, switching to execute a first code editing method to obtain the preset character string; the first code editing method is a method for rewriting the first target segment;
when detecting that the current segment to be executed belongs to a second target segment, switching to execute a second code editing method to update the class name and the interface method name which comprise the target character string in the generated code, and replacing the target character string in the class name and the interface method name with a preset character string; the second code editing method is a method for rewriting the second target segment;
when detecting that the current segment to be executed belongs to a third target segment, switching to executing a third code editing method to update interface parameters and annotation information containing the target character string in the generated code, and replacing the target character string in the interface parameters and the annotation information with a preset character string; the third code editing method is a method for rewriting the third target segment;
when detecting that the current segment to be executed belongs to a fourth target segment, switching to executing a fourth code editing method so as to update configuration information in an interface configuration file containing the target character string in the generated code, and replacing the target character string in the configuration information with a preset character string; the fourth code editing method is a method of rewriting for the fourth target segment.
In an exemplary embodiment, the code generation tool is a code generator of MyBatis, and the code generation logic is generation logic that operates a database interface; the first code editing method is a rewritten parameter verification method;
the plug-in execution unit is further configured to execute a variable obtaining method function included in the rewritten parameter verification method so as to obtain the following preset character strings: a first string for updating a class name of the query condition object class; a second string for updating an interface method name of the operational database interface; a third string for updating the interface parameters and the annotation information; and the fourth character string is used for updating the configuration information in the interface configuration file of the operation database.
In an exemplary embodiment, the second code editing method is a rewritten initialization method;
the plug-in execution unit is further configured to execute a first parameter replacement method function included in calling the rewritten initialization method, and replace the target character string included in the class name of the generated query condition object class with the first character string; and calling a second parameter replacement method function contained in the rewritten initialization method, and replacing the target character string contained in the interface method name in the generated operation database interface code with the second character string.
In an exemplary embodiment, the third code editing method is a rewritten generation calling method;
the plug-in execution unit is further configured to execute a parameter obtaining method function included in the rewritten generation calling method, and replace the target character string included in the interface parameter in the generated operation database interface code with the third character string; and calling an annotation acquisition method function contained in the rewritten generation calling method, and replacing the target character string contained in the release information in the generated operation database interface code with the third character string.
In an exemplary embodiment, the fourth code editing method is a rewritten database element generating method;
the plug-in execution unit is further configured to execute calling the rewritten database element generation method, and replace the target character string included in the configuration information in the configuration file corresponding to the interface in the generated code with the fourth character string.
In an exemplary embodiment, the code generation apparatus further includes a plug-in installation unit;
the plug-in installation unit is configured to execute a plug-in adapter for acquiring the code generation tool, and generate the preset plug-in based on the plug-in adapter, wherein the preset plug-in is generated by inheriting the plug-in adapter; installing the preset plug-in the code generation tool.
According to a third aspect of the embodiments of the present disclosure, there is provided an electronic apparatus including: a processor; a memory for storing the processor-executable instructions; wherein the processor is configured to execute the instructions to implement the code generation method of any of the first aspects.
According to a fourth aspect of embodiments of the present disclosure, there is provided a computer-readable storage medium, wherein instructions, when executed by a processor of an electronic device, enable the electronic device to perform the code generation method of any one of the first aspect.
According to a fifth aspect of embodiments of the present disclosure, there is provided a computer program product comprising instructions which, when executed by a processor of an electronic device, enable the electronic device to perform the code generation method of any one of the first aspects.
The technical scheme provided by the embodiment of the disclosure at least brings the following beneficial effects:
executing a code generation logic corresponding to the code generation tool by responding to the code generation instruction, and calling a preset plug-in; the preset plug-in comprises a plurality of code editing methods, and the target character strings in the generated codes are replaced by the preset character strings after the code editing methods are executed; the plurality of code editing methods are obtained by respectively rewriting based on a plurality of target segments in the code generation logic; secondly, in the execution process of the code generation logic, when detecting that the current segment to be executed belongs to any target segment, switching to a code editing method corresponding to the current segment to be executed in the execution plug-in, and after the execution of the code editing method is finished, returning to execute the next segment of the current segment to be executed; then returning to execute the previous step until the code generation logic is completely executed to obtain a generated code; the method and the device realize real-time replacement of the target character string in the code generating process, so that the character string which is easy to cause confusion in the code is replaced by the character string with clear semantics, and the generated code quality is improved.
It is to be understood that both the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the disclosure.
Drawings
The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the disclosure and are not to be construed as limiting the disclosure.
FIG. 1 is a flow diagram illustrating a method of code generation according to an example embodiment.
FIG. 2 is a flowchart illustrating steps of a method for executing code edits in a plug-in corresponding to a currently to-be-executed segment in accordance with an illustrative embodiment.
FIG. 3 is a flow chart illustrating yet another method of code generation according to an exemplary embodiment.
FIG. 4 is a block diagram illustrating a code generation apparatus according to an example embodiment.
FIG. 5 is a block diagram illustrating an electronic device in accordance with an example embodiment.
Detailed Description
In order to make the technical solutions of the present disclosure better understood by those of ordinary skill in the art, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below with reference to the accompanying drawings.
It should be noted that the terms "first," "second," and the like in the description and claims of the present disclosure and in the above-described drawings are used for distinguishing between similar elements and not necessarily for describing a particular sequential or chronological order. It is to be understood that the data so used is interchangeable under appropriate circumstances such that the embodiments of the disclosure described herein are capable of operation in sequences other than those illustrated or otherwise described herein. The implementations described in the exemplary embodiments below are not intended to represent all implementations consistent with the present disclosure. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the present disclosure, as detailed in the appended claims.
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 for presentation, analyzed data, etc.) referred to in the present disclosure are information and data authorized by the user or sufficiently authorized by each party; correspondingly, the present disclosure also provides a corresponding user authorization entry for the user to select authorization or to select denial.
The code generation method provided by the disclosure can be independently applied to a terminal or a server; optionally, the method can also be applied to an interaction mode of the terminal and the server. Specifically, the terminal responds to a code generation instruction, executes a code generation logic corresponding to a code generation tool, and calls a preset plug-in; the preset plug-in comprises a plurality of code editing methods, and the target character strings in the generated codes are replaced by the preset character strings after the code editing methods are executed; the plurality of code editing methods are obtained by respectively rewriting based on a plurality of target segments in the code generation logic; in the execution process of the code generation logic, when detecting that the current segment to be executed belongs to any target segment, the terminal switches to a code editing method corresponding to the current segment to be executed in the execution plug-in unit, and after the execution of the code editing method is finished, the terminal returns to execute the next segment of the current segment to be executed; and the terminal returns to execute the previous step until the code generation logic is completely executed to obtain the generated code.
The terminal can be, but is not limited to, various smart phones, tablet computers, notebook computers, wearable devices, and the like, and the server can be implemented by an independent server or a server cluster formed by a plurality of servers.
Fig. 1 is a flowchart illustrating a code generation method according to an exemplary embodiment, where the code generation method is used in a terminal, as shown in fig. 1, and includes the following steps.
In step S110, in response to the code generation instruction, executing a code generation logic corresponding to the code generation tool, and calling a preset plug-in; the preset plug-in comprises a plurality of code editing methods, and the target character strings in the generated codes are replaced by the preset character strings after the code editing methods are executed; the plurality of code editing methods are methods obtained by rewriting the target segments in the code generation logic.
The target character string refers to a character string which is generated in the original code generation logic and needs to be replaced, and the preset character string refers to a character string which is preset in a preset plug-in and is used for replacing the target character string; for example, when the code generation logic is not intervened, the generated code is AABBAA; a certain code editing method of the preset plug-in is used for identifying the character string 'B' and replacing the character string 'B' with the character string 'C'; after the preset plug-in intervenes the code generation logic, replacing 'B' in the generated code with 'C', namely the generated code is 'AACCAA', the visible character string 'B' is a target character string, and the character string C is a preset character string.
Wherein the code generation logic is comprised of a plurality of segments, each segment capable of executing different content; the target segment refers to the corresponding segment against which the rewriting process is performed.
Specifically, the terminal responds to a code generation instruction, controls a code generation tool to execute a code generation logic and call a preset plug-in, and monitors the execution process of the code generation logic; when a certain condition is monitored to be met, for example, a certain logic segment is executed, the preset plug-in is enabled to execute a code editing method corresponding to the currently executed segment in the plurality of code editing methods contained in the preset plug-in.
In step S120, in the execution process of the code generation logic, when it is detected that the current to-be-executed segment belongs to any target segment, the execution is switched to the code editing method corresponding to the current to-be-executed segment in the execution plug-in, and after the execution of the code editing method is finished, the execution is returned to the next segment of the current to-be-executed segment.
In the Mybatis plug-in mechanism, when interception calling is allowed to be performed at a certain point in the execution process of the mapping statement, that is, a code generation logic corresponding to the code generation tool is executed, the called preset plug-in can intercept the code generation logic and convert the intercepted code generation logic into a code editing method for executing the preset plug-in, so that application of the code editing method in the preset plug-in is realized, that is, a target character string is replaced.
Specifically, a plurality of code editing methods in the preset plug-in correspond to a plurality of different logic execution segments, when the execution process of the code generation logic reaches a certain segment, the execution process of the code generation logic is intercepted, the preset plug-in is switched to be called to execute the code editing method which meets the condition, and the next segment of the code generation logic is returned to be executed after the execution of the code editing method is finished.
For example, a code generation logic may be represented as four segments a1-a2-A3-a4, wherein the a2 segment generates code that includes a target string "B"; meanwhile, a certain code editing method of the preset plug-in can replace a target character string 'B' with a preset character string 'C', when the execution of A2 is finished, the character string 'B' appearing in the code is converted into a code editing method corresponding to the current segment to be executed in the execution plug-in, the preset plug-in is called to execute the code editing method, the character string 'B' is replaced with the preset character string 'C', after the replacement is finished, the execution of the code editing method is equivalent to the end of the execution of the code editing method, and then the next segment in the execution code generation logic is returned, namely the A3 segment.
In step S130, the previous step is returned to until the code generation logic is completely executed, and the generated code is obtained.
Specifically, if the code generation logic can be represented as a1-a2-A3-a4, after the last step of the code generation logic, i.e., the a4 step, is completed, it is determined that the code generation logic has been completed, and the obtained code is also the generated code.
It should be noted that the preset plug-in may be a plug-in written according to an extended interface based on MyBatis Generator, and may also be obtained by modifying a plug-in called rename exampleclasssplug (the present disclosure also refers to a preset class name renaming plug-in, which refers to a plug-in for renaming a certain class name) provided by MyBatis officials; the plug-in renamemeExpleClassPlugin can rename the generated XXXXsample class (also called the class name containing the target character string in the present disclosure) by using a regular expression mode, and is completed by configuring searchString and replaceString attributes; therefore, the preset plug-in can inherit the inherent function of the RenameExampleClassPlugin, namely, the replacement of the Example in the file name of UserDOExample.java (namely, the replacement of the class name containing the target character string) and the replacement of the Example character eye contained in the UserDOMapper.java and UserDOMapper.xml files at least, namely, the replacement of the target character string in the interface method name, the interface parameter, the annotation information and the configuration information.
In the code generation method, a code generation logic corresponding to a code generation tool is executed by responding to a code generation instruction, and a preset plug-in is called; the preset plug-in comprises a plurality of code editing methods, and the target character strings in the generated codes are replaced by the preset character strings after the code editing methods are executed; the plurality of code editing methods are obtained by respectively rewriting based on a plurality of target segments in the code generation logic; secondly, in the execution process of the code generation logic, when detecting that the current segment to be executed belongs to any target segment, switching to a code editing method corresponding to the current segment to be executed in the execution plug-in, and after the execution of the code editing method is finished, returning to execute the next segment of the current segment to be executed; then returning to execute the previous step until the code generation logic is completely executed to obtain a generated code; the method and the device realize real-time replacement of the target character string in the code generating process, so that the character string which is easy to cause confusion in the code is replaced by the character string with clear semantics, and the generated code quality is improved.
In an exemplary embodiment, as shown in fig. 2, in step S120, when it is detected that the current to-be-executed segment belongs to any target segment, the method for executing the code editing corresponding to the current to-be-executed segment in the plug-in is changed to the method for executing the code editing corresponding to the current to-be-executed segment, which may specifically be implemented by the following steps:
in step S210, when it is detected that the current segment to be executed belongs to the first target segment, the first code editing method is switched to be executed to obtain a preset character string; the first code editing method is a method of rewriting for a first target segment.
In step S220, when it is detected that the current segment to be executed belongs to the second target segment, the method is switched to execute the second code editing method to update the class name and the interface method name of the generated code, which include the target character string, and the target character string in the class name and the interface method name is replaced with a preset character string; the second code editing method is a method of rewriting for the second target segment.
In step S230, when it is detected that the current segment to be executed belongs to the third target segment, the third code editing method is switched to be executed to update the interface parameter and the annotation information containing the target character string in the generated code, and the target character string in the interface parameter and the annotation information is replaced by a preset character string; the third code editing method is a method for rewriting a third target segment.
In step S240, when it is detected that the current segment to be executed belongs to a fourth target segment, the fourth code editing method is switched to be executed, so as to update the configuration information in the interface configuration file containing the target character string in the generated code, and replace the target character string in the configuration information with a preset character string; the fourth code editing method is a method of rewriting for a fourth target segment.
The preset plug-in not only inherits the adaptor PluginAdap provided by Mybatis-generator, but also needs to be rewritten according to the following steps of RenameExampleClassPlugin plug-in:
1.validate;
2.initialized;
3.clientCountByExampleMethodGenerated;
4.clientDeleteByExampleMethodGenerated;
5.clientSelectByExampleWithoutBLOBsMethodGenerated;
6.clientUpdateByExampleSelectiveMethodGenerated;
7.clientUpdateByExampleWithoutBLOBsMethodGenerated;
8.sqlMapExampleWhereClauseElementGenerated。
the first code editing method is a rewritten parameter verification method, corresponds to a '1. valid' segment of the plug-in step and is used for acquiring a preset character string;
the second code editing method is an initialized method after rewriting, corresponds to the '2. initialized' segment of the plug-in step, and is used for updating the class name and the interface method name containing the target character string in the generated code and replacing the target character string in the class name and the interface method name with a preset character string;
the third code editing method is a parameter information replacement method after rewriting, corresponding to a segment corresponding to any one of the steps 3-7 of the plug-in, the third code editing method is used for respectively updating the interface parameter and the annotation information of the generated code containing the target character string, and replacing the target character string in the interface parameter and the annotation information with a preset character string;
the fourth code editing method is a rewritten configuration information replacing method, and is a fragment corresponding to the 8. sqlMaxampleWhereLauseElementGenerated of the plug-in step, and is used for updating the configuration information in the interface configuration file of the generated code containing the target character string, and replacing the target character string in the configuration information with a preset character string.
Specifically, the terminal monitors the execution process of the code generation logic, and if it is detected that the code generation logic is executed to obtain a segment of a preset character string, the terminal switches to execute a first code method, that is, executes a rewritten parameter verification method valid, to obtain at least one preset character string preset by a user, where each preset character string corresponds to a target character string to be replaced, and the corresponding method may be that the preset character string is identified by identifying character strings of codes in different code regions.
After the preset character string is obtained, continuously executing the next segment of the code generation logic; for example, if target character strings exist in the class name and the interface method name in the executed segment generation code, the second code editing method is executed, and the target character strings existing in the class name and the interface method name are replaced by preset character strings. Similarly, the third code editing method may replace the target character string included in the interface parameter and the annotation information in the generated code, and the fourth code editing method may replace the target character string included in the configuration information in the interface configuration file in the generated code.
According to the technical scheme provided by the embodiment of the disclosure, the execution segment of the code generation logic can be detected, and when the execution of the corresponding segment is detected, the execution is switched to the execution of the corresponding code editing method; after the code editing method is executed, continuing to execute the next segment of the code generation logic until the code generation logic is executed or the code is generated; the character strings in the code generation process are detected and replaced in real time, the replaceable scenes are expanded through the detection of a plurality of execution conditions, and the range of the character strings capable of being replaced is enlarged.
In an exemplary embodiment, the code generation tool is a code generator of MyBatis, and the code generation logic is generation logic that operates a database interface; the first code editing method is a rewritten parameter verification method;
in step S210, a first code editing method is executed to obtain a preset character string, including: calling a variable acquisition method function contained in the rewritten parameter verification method to acquire the following preset character strings: a first string for updating a class name of the query condition object class; a second string for updating an interface method name of the operational database interface; a third string for updating the interface parameters and the annotation information; and the fourth character string is used for updating the configuration information in the interface configuration file of the operation database.
The plug-in can use a regular expression mode to rename the generated XXXXsample class by configuring searchString and replaceString attributes. The first code editing method is realized by rewriting the parameter verification method valid in the plug-in RenameExampleClassPlugin, which contains a variable acquisition method function.
For Example, the summary code of the first code editing method may be as follows, where the character string Example is a target character string:
configuration for obtaining alternative Example File name set by user
String replaceNameSearchString=this.properties.getProperty("replaceNameSearchString");
this.replaceNameString=this.properties.getProperty("replaceNameString");
// configuration for obtaining user-set interface name in user-substituted Mapper
replaceMethodString=properties.getProperty("replaceMethodString");
Obtaining user-set configuration of interface parameter name in user-replaced Mapper
replaceParamString=properties.getProperty("replaceParamString");
Specifically, the rewritten parameter verification method includes a variable obtaining method function, which is capable of obtaining a class name of a query condition object class preset by a user as a first character string, obtaining an interface method name of an operation database interface as a second character string, obtaining a third character string of an interface parameter and annotation information as a third character string, and obtaining configuration information in an interface configuration file of the operation database as a fourth character string. In the above code, "replayNameSearchString", "replayNameString", "replayMethodString", and "replayParamString" are each character string obtained by the first code editing method; getProperty is a variable acquisition method function included in the parameter verification method.
According to the technical scheme provided by the embodiment of the disclosure, the first code editing method of the preset plug-in can be called to obtain the preset character string, the preset character string which is obtained in advance can provide data for replacement of other code editing methods in the later period, and the efficiency of replacing the target character string is improved.
In an exemplary embodiment, the second code editing method is a rewritten initialization method;
in step S220, executing the second code editing method to update the class name and the interface method name of the generated code, which include the target character string, and replace the target character string in the class name and the interface method name with a preset character string, including: calling a first parameter replacement method function contained in the rewritten initialization method, and replacing a target character string contained in the class name of the generated query condition object class with a first character string; and calling a second parameter replacement method function contained in the rewritten initialization method, and replacing a target character string contained in the interface method name in the generated operation database interface code with a second character string.
The second code editing method is an initialization method initialized obtained by rewriting an initialized method in a RenameExpleClassPlugin, and the initialization method initialized comprises a parameter replacement method function introssedTable.
The summary code of the second code editing method may be as follows:
public void initialized(IntrospectedTable introspectedTable){
modification of the name of the Example class
String oldType=introspectedTable.getExampleType();
Matcher matcher=this.pattern.matcher(oldType);
oldType=matcher.replaceAll(this.replaceNameString);
introspectedTable.setExampleType(oldType);
V/replace Mapper interface method name
introspectedTable.setCountByExampleStatementId("countBy"+replaceMethodString);
introspectedTable.setDeleteByExampleStatementId("seleteBy"+replaceMethodString);
introspectedTable.setSelectByExampleStatementId("selectBy"+replaceMethodString);
introspectedTable.setUpdateByExampleSelectiveStatementId("updateBy"+replaceMethodString+"Selective");
introspectedTable.setUpdateByExampleStatementId("updateBy"+replaceMethodString);
}
Taking XXXXEXple as an example, the code can replace XXXX + replaceNameString; then replace the name XXX + repolaceMethodString of the interface in the Mapper interface and xml file.
Specifically, in the process of executing the second code editing method, the preset plug-in identifies that the generated code contains the class name and the interface method name of the target character string, replaces the class name by using the first character string obtained in the first code editing method, and replaces the interface method name by using the second character string obtained in the first code editing method.
According to the technical scheme provided by the embodiment of the disclosure, the target character strings in the class names and the interface method names are replaced, so that characters which are easy to cause confusion do not exist in the class names and the interface method names in the codes, the code semantics are clear, and the quality of code generation is further improved.
In an exemplary embodiment, the third code editing method is a rewritten generation calling method; in step S230, a third code editing method is executed to update the interface parameter and the annotation information of the generated code, which include the target character string, and replace the target character string in the interface parameter and the annotation information with a preset character string, including: calling a parameter acquisition method function contained in the rewritten generated calling method, and replacing a target character string contained in an interface parameter in the generated operation database interface code with a third character string; and calling an annotation acquisition method function contained in the rewritten generated calling method, and replacing a target character string contained in the release information in the generated operation database interface code with a third character string.
The third code editing method is obtained by rewriting the parameter information replacement method in the method step 3-7 in the plug-in RenameExampleClassPlugin; a method like clientXXXMethodGenerated is a method that generates a call, which when called generates each method of a Java client implementation class and/or a method of a Java client interface; the parameter acquisition method function included in the generated calling method can be realized by a Java reflection technology.
The rewritten third code editing method can replace the interface parameter containing the target character string in the generated operation database interface code with the third character string, and replace the comment information containing the target character string in the generated operation database interface code with the third character string.
According to the technical scheme provided by the embodiment of the disclosure, the target character strings in the interface parameters and the annotation information are replaced, so that characters which are easy to cause confusion do not exist in the interface parameters and the annotation information in the codes, the semantics of the codes are clear, and the generation quality of the codes is improved.
In an exemplary embodiment, the fourth code editing method is a rewritten database element generating method; in step S230, a fourth code editing method is executed to update the configuration information in the interface corresponding configuration file containing the target character string in the generated code, and replace the target character string of the configuration information in the interface corresponding configuration file with a preset character string, where the method includes: and calling the rewritten database element generation method, and replacing the target character string contained in the configuration information in the configuration file corresponding to the interface in the generated code with a fourth character string.
The fourth code editing method is that corresponding elements in the database can be replaced by rewriting a configuration information replacement method sqlMapExampleWhereLauseElementGenerated in a plug-in RenameExampleClassPlugin, namely the fourth code editing method is used as a database element generation method; the rewritten database element generation method can modify the configuration information in the configuration file corresponding to the interface; for Example Update _ By _ Example _ Where _ class sql in xxxmapper. If "example. orderceria" is replaced by "XXXX. orderceria" in the modified code, the target character string example in the configuration information is replaced by the preset character string of XXXX.
According to the technical scheme provided by the embodiment of the disclosure, the target character string in the configuration information in the configuration file corresponding to the interface is replaced, so that characters which are easy to cause confusion do not exist in the configuration information, the code semantics are clear, and the quality of code generation is improved.
In an exemplary embodiment, before the calling the preset plug-in, in step S110, the method further includes: acquiring a plug-in adapter of a code generation tool, and generating a preset plug-in based on the plug-in adapter, wherein the preset plug-in is generated by inheriting the plug-in adapter; a preset plug-in is installed in the code generation tool.
Specifically, the preset plug-in is obtained based on a renameexpampletalsplug-in provided by MyBatis officials, so that the preset plug-in needs to be capable of simultaneously inheriting an adapter plug-in adapter provided by MyBatis-generator, and the completeness of functions is realized. The terminal firstly obtains a plug-in adapter of the code generation tool, and obtains a preset plug-in on the basis of the plug-in adapter; and after the preset plug-in is installed on the code generation tool, the preset plug-in can be operated, and the target character string in the code is replaced.
According to the technical scheme provided by the embodiment of the disclosure, the preset plug-in inherits the plug-in adapter, and is not required to be adjusted again when being installed in the code generation tool, so that the method and the device can adapt to the original plug-in installation mechanism, and the success of the preset plug-in adaptation is improved.
FIG. 3 is a flowchart illustrating yet another code generation method according to an exemplary embodiment, as shown in FIG. 3, the code generation method including the steps of:
in step S310, a plug-in adapter of the code generation tool is obtained, and a preset plug-in is generated based on the plug-in adapter, where the preset plug-in is generated by inheriting from the plug-in adapter; installing a preset plug-in a code generation tool;
in step S320, in response to the code generation instruction, executing a code generation logic corresponding to the code generation tool, and calling a preset plug-in to include a plurality of code editing methods, where the plurality of code editing methods are executed to replace a target character string in the generated code with a preset character string; the plurality of code editing methods are obtained by respectively rewriting based on a plurality of target segments in the code generation logic;
in step S330, when it is detected that the current segment to be executed belongs to the first target segment, the method shifts to execute the first code editing method to call a variable obtaining method function included in the rewritten parameter verification method to obtain the following preset character string: a first string for updating a class name of the query condition object class; a second string for updating an interface method name of the operational database interface; a third string for updating the interface parameters and the annotation information; a fourth string for updating configuration information in the operational database interface configuration file; returning to the execution code generation logic after the acquisition is completed;
in step S340, when it is detected that the current segment to be executed belongs to the second target segment, the method is switched to execute the second code editing method, a first parameter replacement method function included in the rewritten initialization method is called, and a target character string included in the class name of the generated query condition object class is replaced with the first character string; calling a second parameter replacement method function contained in the rewritten initialization method, and replacing a target character string contained in an interface method name in the generated operation database interface code with a second character string; returning to the execution code generation logic after the replacement is finished;
in step S350, when it is detected that the current segment to be executed belongs to the third target segment, the method is switched to execute the third code editing method, the rewritten parameter obtaining method function included in the generated calling method is called, and the target character string included in the interface parameter in the generated operation database interface code is replaced with the third character string; calling an annotation acquisition method function contained in the rewritten generated calling method, and replacing a target character string contained in release information in the generated operation database interface code with a third character string; returning to the execution code generation logic after the replacement is finished;
in step S360, when it is detected that the current segment to be executed belongs to the fourth target segment, the fourth code editing method is switched to be executed, the rewritten database element generation method is called, and the target character string included in the configuration information in the configuration file corresponding to the interface in the generated code is replaced with the fourth character string; and returning to the execution code generation logic after the replacement is completed.
An application example for generating the Mapper file by applying the embodiment is provided as follows:
a User query condition object is created, then the selectByCondition method of the User DoMapper is called, and the query is executed by using the query condition object.
Before modification:
// create query condition object: retrieving information from persons older than 20 years of age
UserDOExample userDOExample=new UserDOExample();
userDOExample.createCriteria().andAgeGreaterThan(value:20);
V/according to the condition, perform the query
List<UserDO>UserDOlist=UserDOMapper.selectByCondition(userDOExample);
After modification:
// create query condition object: retrieving information from persons older than 20 years of age
UserCondition userCondition=new UserCondition();
userCondition.createCriteria().andAgeGreaterThan(value:20);
V/according to the condition, perform the query
List<UserDO>UserDOlist=UserDOMapper.selectByCondition(userCondition);
It can be seen that the target character string "DOExample" is replaced by a code editing method in the preset plug-in to "Condition", and is semantically easier to understand.
According to the technical scheme provided by the embodiment of the disclosure, the target character string in the code is replaced in real time in the code generation process, so that the character string which is easy to cause confusion in the code is replaced by the character string with clear semantics, and the generated code quality is improved.
It should be understood that although the various steps in the flowcharts of fig. 1-3 are shown in order as indicated by the arrows, the steps are not necessarily performed in order as indicated by the arrows. The steps are not performed in the exact order shown and described, and may be performed in other orders, unless explicitly stated otherwise. Moreover, at least some of the steps in fig. 1-3 may include multiple steps or multiple stages, which are not necessarily performed at the same time, but may be performed at different times, which are not necessarily performed in sequence, but may be performed in turn or alternately with other steps or at least some of the other steps or stages.
It is understood that the same/similar parts between the embodiments of the method described above in this specification can be referred to each other, and each embodiment focuses on the differences from the other embodiments, and it is sufficient that the relevant points are referred to the descriptions of the other method embodiments.
FIG. 4 is a block diagram illustrating a code generation apparatus according to an example embodiment. Referring to fig. 4, the apparatus includes a plug-in calling unit 402, a plug-in executing unit 404, and a code generating unit 406.
The plug-in calling unit 402 is configured to execute a code generation logic corresponding to the code generation tool in response to the code generation instruction, and call a preset plug-in; the preset plug-in comprises a plurality of code editing methods, and the target character strings in the generated codes are replaced by the preset character strings after the code editing methods are executed; the plurality of code editing methods are obtained by respectively rewriting based on a plurality of target segments in the code generation logic;
the plug-in execution unit 404 is configured to execute, in the execution process of the code generation logic, when it is detected that the current to-be-executed segment belongs to any target segment, a code editing method corresponding to the current to-be-executed segment in the plug-in is switched to be executed, and after the execution of the code editing method is finished, the next segment of the current to-be-executed segment is returned to be executed;
the code generating unit 406 is configured to execute the previous step until the code generating logic is executed, so as to obtain the generated code.
In an exemplary embodiment, the plug-in execution unit 404 is further configured to execute, when it is detected that the current to-be-executed segment belongs to the first target segment, to switch to execute the first code editing method to obtain the preset character string; the first code editing method is a method for rewriting a first target segment; when detecting that the current segment to be executed belongs to a second target segment, switching to execute a second code editing method to update a class name and an interface method name containing a target character string in a generated code, and replacing the target character string in the class name and the interface method name with a preset character string; the second code editing method is a method for rewriting a second target segment; when detecting that the current segment to be executed belongs to a third target segment, switching to executing a third code editing method so as to update interface parameters and annotation information containing target character strings in the generated codes, and replacing the target character strings in the interface parameters and the annotation information with preset character strings; the third code editing method is a method for rewriting a third target segment; when detecting that the current segment to be executed belongs to a fourth target segment, switching to executing a fourth code editing method so as to update configuration information in an interface configuration file containing a target character string in the generated code, and replacing the target character string in the configuration information with a preset character string; the fourth code editing method is a method of rewriting for a fourth target segment.
In an exemplary embodiment, the code generation tool is a code generator of MyBatis, and the code generation logic is generation logic that operates a database interface; the first code editing method is a method for rewriting a parameter verification method in the generation logic; the first code editing method is a rewritten parameter verification method;
the plug-in execution unit 404 is further configured to execute a variable obtaining method function included in the rewritten parameter verification method to obtain the following preset character strings: a first string for updating a class name of the query condition object class; a second string for updating an interface method name of the operational database interface; a third string for updating the interface parameters and the annotation information; and the fourth character string is used for updating the configuration information in the interface configuration file of the operation database.
In an exemplary embodiment, the second code editing method is a rewritten initialization method;
the plug-in execution unit 404 is further configured to execute a first parameter replacement method function included in the calling rewritten initialization method, and replace the target character string included in the class name of the generated query condition object class with the first character string; and calling a second parameter replacement method function contained in the rewritten initialization method, and replacing a target character string contained in the interface method name in the generated operation database interface code with a second character string.
In an exemplary embodiment, the third code editing method is a rewritten generation calling method;
the plug-in execution unit 404 is further configured to execute a parameter obtaining method function included in the generated calling method after the calling is rewritten, and replace a target character string included in the interface parameter in the generated operation database interface code with a third character string; and calling an annotation acquisition method function contained in the rewritten generated calling method, and replacing a target character string contained in the release information in the generated operation database interface code with a third character string.
In an exemplary embodiment, the fourth code editing method is a rewritten database element generating method;
the plug-in execution unit 404 is further configured to execute the method for generating the database element after the call rewriting, and replace the target character string included in the configuration information in the configuration file corresponding to the interface in the generated code with a fourth character string.
In an exemplary embodiment, the code generation apparatus further includes a plug-in installation unit;
the plug-in installation unit is configured to execute a plug-in adapter for acquiring the code generation tool, and generate a preset plug-in based on the plug-in adapter, wherein the preset plug-in is generated by inheriting the plug-in adapter; a preset plug-in is installed in the code generation tool.
With regard to the apparatus in the above-described embodiment, the specific manner in which each module performs the operation has been described in detail in the embodiment related to the method, and will not be elaborated here.
FIG. 5 is a block diagram illustrating an electronic device 500 for code generation in accordance with an example embodiment. For example, the electronic device 500 may be a mobile phone, a computer, a digital broadcast terminal, a messaging device, a gaming console, a tablet device, a medical device, a fitness device, a personal digital assistant, and so forth.
Referring to fig. 5, electronic device 500 may include one or more of the following components: processing component 502, memory 504, power component 506, multimedia component 508, audio component 510, interface to input/output (I/O) 512, sensor component 514, and communication component 516.
The processing component 502 generally controls overall operation of the electronic device 500, such as operations associated with display, telephone calls, data communications, camera operations, and recording operations. The processing components 502 may include one or more processors 520 to execute instructions to perform all or a portion of the steps of the methods described above. Further, the processing component 502 can include one or more modules that facilitate interaction between the processing component 502 and other components. For example, the processing component 502 can include a multimedia module to facilitate interaction between the multimedia component 508 and the processing component 502.
The memory 504 is configured to store various types of data to support operations at the electronic device 500. Examples of such data include instructions for any application or method operating on the electronic device 500, contact data, phonebook data, messages, pictures, videos, and so forth. The memory 504 may be implemented by any type or combination of volatile or non-volatile storage devices, 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, optical disk, or graphene memory.
The power supply component 506 provides power to the various components of the electronic device 500. The power components 506 may include a power management system, one or more power sources, and other components associated with generating, managing, and distributing power for the electronic device 500.
The multimedia component 508 includes a screen that provides an output interface between the electronic device 500 and a user. In some embodiments, the screen 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 an input signal from a user. The touch panel includes one or more touch sensors to sense touch, slide, and gestures on the touch panel. The touch sensor may not only sense the boundary of a touch or slide action, but also detect the duration and pressure associated with the touch or slide operation. In some embodiments, the multimedia component 508 includes a front facing camera and/or a rear facing camera. The front camera and/or the rear camera may receive external multimedia data when the electronic device 500 is in an operating mode, such as a shooting mode or a video mode. Each front camera and rear camera may be a fixed optical lens system or have a focal length and optical zoom capability.
The audio component 510 is configured to output and/or input audio signals. For example, the audio component 510 includes a Microphone (MIC) configured to receive external audio signals when the electronic device 500 is in an operational mode, such as a call mode, a recording mode, and a voice recognition mode. The received audio signals may further be stored in the memory 504 or transmitted via the communication component 516. In some embodiments, audio component 510 also includes a speaker for outputting audio signals.
The I/O interface 512 provides an interface between the processing component 502 and peripheral interface modules, which may be keyboards, click wheels, buttons, etc. These buttons may include, but are not limited to: a home button, a volume button, a start button, and a lock button.
The sensor assembly 514 includes one or more sensors for providing various aspects of status assessment for the electronic device 500. For example, the sensor assembly 514 may detect an open/closed state of the electronic device 500, the relative positioning of components, such as a display and keypad of the electronic device 500, the sensor assembly 514 may detect a change in the position of the electronic device 500 or components of the electronic device 500, the presence or absence of user contact with the electronic device 500, orientation or acceleration/deceleration of the device 500, and a change in the temperature of the electronic device 500. The sensor assembly 514 may include a proximity sensor configured to detect the presence of a nearby object without any physical contact. The sensor assembly 514 may also include a light sensor, such as a CMOS or CCD image sensor, for use in imaging applications. In some embodiments, the sensor assembly 514 may also include an acceleration sensor, a gyroscope sensor, a magnetic sensor, a pressure sensor, or a temperature sensor.
The communication component 516 is configured to facilitate wired or wireless communication between the electronic device 500 and other devices. The electronic device 500 may access a wireless network based on a communication standard, such as WiFi, a carrier network (such as 2G, 3G, 4G, or 5G), or a combination thereof. In an exemplary embodiment, the communication component 516 receives a broadcast signal or broadcast related information from an external broadcast management system via a broadcast channel. In an exemplary embodiment, the communication component 516 further includes a Near Field Communication (NFC) module to facilitate short-range communications. For example, the NFC module may be implemented based on Radio Frequency Identification (RFID) technology, infrared data association (IrDA) technology, Ultra Wideband (UWB) technology, Bluetooth (BT) technology, and other technologies.
In an exemplary embodiment, the electronic device 500 may be implemented by one or more Application Specific Integrated Circuits (ASICs), Digital Signal Processors (DSPs), Digital Signal Processing Devices (DSPDs), Programmable Logic Devices (PLDs), Field Programmable Gate Arrays (FPGAs), controllers, micro-controllers, microprocessors or other electronic components for performing the above-described methods.
In an exemplary embodiment, a computer-readable storage medium comprising instructions, such as the memory 504 comprising instructions, executable by the processor 520 of the electronic device 500 to perform the above-described method is also provided. For example, the computer readable storage medium may be a ROM, a Random Access Memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, an optical data storage device, and the like.
In an exemplary embodiment, a computer program product is also provided, which includes instructions executable by the processor 520 of the electronic device 500 to perform the above-described method.
It should be noted that the descriptions of the above-mentioned apparatus, the electronic device, the computer-readable storage medium, the computer program product, and the like according to the method embodiments may also include other embodiments, and specific implementations may refer to the descriptions of the related method embodiments, which are not described in detail herein.
Other embodiments of the disclosure will be apparent to those skilled in the art from consideration of the specification and practice of the disclosure disclosed herein. This disclosure is intended to cover any variations, uses, or adaptations of the disclosure following, in general, the principles of the disclosure and including such departures from the present disclosure as come within known or customary practice within the art to which the disclosure pertains. It is intended that the specification and examples be considered as exemplary only, with a true scope and spirit of the disclosure being indicated by the following claims.
It will be understood that the present disclosure is not limited to the precise arrangements described above and shown in the drawings and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.

Claims (10)

1. A code generation method, comprising:
responding to the code generation instruction, executing code generation logic corresponding to the code generation tool, and calling a preset plug-in; the preset plug-in comprises a plurality of code editing methods, and the target character strings in the generated codes are replaced by preset character strings after the code editing methods are executed; the code editing methods are obtained by respectively rewriting based on a plurality of target segments in the code generation logic;
in the execution process of the code generation logic, when detecting that the current segment to be executed belongs to any target segment, switching to execute a code editing method corresponding to the current segment to be executed in the plug-in, and after the execution of the code editing method is finished, returning to execute the next segment of the current segment to be executed;
and returning to execute the previous step until the code generation logic is completely executed to obtain the generated code.
2. The method according to claim 1, wherein when it is detected that the current segment to be executed belongs to any one of the target segments, the method is switched to execute a code editing method corresponding to the current segment to be executed in the plug-in, and includes:
when detecting that the current segment to be executed belongs to the first target segment, switching to execute a first code editing method to obtain the preset character string; the first code editing method is a method for rewriting the first target segment;
when detecting that the current segment to be executed belongs to a second target segment, switching to execute a second code editing method to update the class name and the interface method name which comprise the target character string in the generated code, and replacing the target character string in the class name and the interface method name with a preset character string; the second code editing method is a method for rewriting the second target segment;
when detecting that the current segment to be executed belongs to a third target segment, switching to executing a third code editing method to update interface parameters and annotation information containing the target character string in the generated code, and replacing the target character string in the interface parameters and the annotation information with a preset character string; the third code editing method is a method for rewriting the third target segment;
when detecting that the current segment to be executed belongs to a fourth target segment, switching to executing a fourth code editing method so as to update configuration information in an interface configuration file containing the target character string in the generated code, and replacing the target character string in the configuration information with a preset character string; the fourth code editing method is a method of rewriting for the fourth target segment.
3. The method of claim 2, wherein the code generation tool is a code generator of MyBatis, and the code generation logic is generation logic that operates a database interface; the first code editing method is a rewritten parameter verification method;
the executing the first code editing method to obtain the preset character string includes:
calling a variable acquisition method function contained in the rewritten parameter verification method to acquire the following preset character strings:
a first string for updating a class name of the query condition object class;
a second string for updating an interface method name of the operational database interface;
a third string for updating the interface parameters and the annotation information;
and the fourth character string is used for updating the configuration information in the interface configuration file of the operation database.
4. The method of claim 3, wherein the second code editing method is a rewritten initialization method;
the executing the second code editing method to update the class name and the interface method name of the target character string included in the generated code, and replacing the target character string in the class name and the interface method name with a preset character string includes:
calling a first parameter replacement method function contained in the rewritten initialization method, and replacing the target character string contained in the class name of the generated query condition object class with the first character string;
and calling a second parameter replacement method function contained in the rewritten initialization method, and replacing the target character string contained in the interface method name in the generated operation database interface code with the second character string.
5. The method of claim 3, wherein the third code editing method is a rewritten generate call method;
the executing the third code editing method to update the interface parameter and the annotation information containing the target character string in the generated code, and replacing the target character string in the interface parameter and the annotation information with a preset character string includes:
calling a parameter acquisition method function contained in the rewritten generated calling method, and replacing the target character string contained in the interface parameter in the generated operation database interface code with the third character string;
and calling an annotation acquisition method function contained in the rewritten generation calling method, and replacing the target character string contained in the release information in the generated operation database interface code with the third character string.
6. The method of claim 3, wherein the fourth code editing method is a rewritten database element generation method;
the executing the fourth code editing method to update the configuration information in the configuration file corresponding to the interface including the target character string in the generated code, and replacing the target character string of the configuration information in the configuration file corresponding to the interface with a preset character string includes:
and calling the rewritten database element generation method, and replacing the target character string contained in the configuration information in the configuration file corresponding to the interface in the generated code with the fourth character string.
7. A code generation apparatus, comprising:
the plug-in calling unit is configured to execute a code generation logic corresponding to the code generation tool in response to the code generation instruction, and call a preset plug-in; the preset plug-in comprises a plurality of code editing methods, and the target character strings in the generated codes are replaced by preset character strings after the code editing methods are executed; the code editing methods are obtained by respectively rewriting based on a plurality of target segments in the code generation logic;
the plug-in execution unit is configured to execute, in the execution process of the code generation logic, when detecting that a current segment to be executed belongs to any one of the target segments, a code editing method corresponding to the current segment to be executed in the plug-in is switched to be executed, and after the execution of the code editing method is finished, the next segment of the current segment to be executed is returned to be executed;
and the code generation unit is configured to execute the previous step and obtain the generated code until the code generation logic is completely executed.
8. An electronic device, comprising:
a processor;
a memory for storing the processor-executable instructions;
wherein the processor is configured to execute the instructions to implement the code generation method of any of claims 1 to 6.
9. A computer-readable storage medium, wherein instructions in the computer-readable storage medium, when executed by a processor of an electronic device, enable the electronic device to perform the code generation method of any of claims 1 to 6.
10. A computer program product comprising instructions therein, which when executed by a processor of an electronic device, enable the electronic device to perform the code generation method of any of claims 1 to 6.
CN202111143523.8A 2021-09-28 2021-09-28 Code generation method and device, electronic equipment and storage medium Pending CN113778398A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202111143523.8A CN113778398A (en) 2021-09-28 2021-09-28 Code generation method and device, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202111143523.8A CN113778398A (en) 2021-09-28 2021-09-28 Code generation method and device, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
CN113778398A true CN113778398A (en) 2021-12-10

Family

ID=78854033

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202111143523.8A Pending CN113778398A (en) 2021-09-28 2021-09-28 Code generation method and device, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN113778398A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114579168A (en) * 2022-05-05 2022-06-03 苏州浪潮智能科技有限公司 Code updating method and device, electronic equipment and computer readable storage medium

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1609792A (en) * 2003-10-24 2005-04-27 微软公司 Programming interface for a computer program
CN104077147A (en) * 2014-07-11 2014-10-01 东南大学 Software reusing method based on code clone automatic detection and timely prompting
CN111984241A (en) * 2020-07-08 2020-11-24 福建亿能达信息技术股份有限公司 Method, device, equipment and medium for dynamically taking effect of online codes
CN113238752A (en) * 2021-05-17 2021-08-10 北京达佳互联信息技术有限公司 Code generation method and device, electronic equipment and storage medium

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1609792A (en) * 2003-10-24 2005-04-27 微软公司 Programming interface for a computer program
CN104077147A (en) * 2014-07-11 2014-10-01 东南大学 Software reusing method based on code clone automatic detection and timely prompting
CN111984241A (en) * 2020-07-08 2020-11-24 福建亿能达信息技术股份有限公司 Method, device, equipment and medium for dynamically taking effect of online codes
CN113238752A (en) * 2021-05-17 2021-08-10 北京达佳互联信息技术有限公司 Code generation method and device, electronic equipment and storage medium

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114579168A (en) * 2022-05-05 2022-06-03 苏州浪潮智能科技有限公司 Code updating method and device, electronic equipment and computer readable storage medium

Similar Documents

Publication Publication Date Title
WO2018000585A1 (en) Interface theme recommendation method, apparatus, terminal and server
WO2017071078A1 (en) Communication module firmware and plug-in generation method and apparatus
CN106339384B (en) Storage process conversion method and device
CN104462296B (en) File management method and device and terminal
CN107463372B (en) Data-driven page updating method and device
CN113238752A (en) Code generation method and device, electronic equipment and storage medium
CN108803892B (en) Method and device for calling third party application program in input method
CN115185717A (en) Interface calling method and device, electronic equipment and storage medium
CN107945552A (en) Become method, apparatus and the storage medium that the lamp time is prompted to signal lamp
CN111061452A (en) Voice control method and device of user interface
CN104951522B (en) Method and device for searching
CN111338971B (en) Application testing method and device, electronic equipment and storage medium
CN113778398A (en) Code generation method and device, electronic equipment and storage medium
CN113010157A (en) Code generation method and device
CN112433787A (en) Target object serialization method and device, electronic device and storage medium
CN111382161A (en) State data processing method and device, electronic equipment and storage medium
WO2020024436A1 (en) Method and system for updating user information, and server
CN112333233B (en) Event information reporting method and device, electronic equipment and storage medium
CN114924769A (en) Component updating method and device, electronic equipment and storage medium
CN114239531A (en) Template recommendation method and device, electronic equipment and storage medium
CN110457084B (en) Loading method and device
CN111488267B (en) Interface test script generation method and device and electronic equipment
CN113934452B (en) Data processing method and device, electronic equipment and storage medium
CN114885211B (en) Media resource template generation method, media resource generation method and device
CN112003788B (en) Data request sending method, device, equipment and medium

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