Code automatic generation method, device, equipment, medium and program product
By automatically generating strategy pattern code sets, the problems of code duplication and reuse in strategy pattern development are solved, and code quality and maintainability are improved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-08
- Publication Date
- 2026-03-17
AI Technical Summary
In software development, implementing the strategy pattern requires manually writing a large amount of repetitive and tedious code, and the strategy pattern code written by different developers is difficult to reuse and integrate.
By customizing configuration parameters, the system generates enumeration classes, strategy interfaces, and implementation classes using pre-configured strategy component templates. The instantiation and storage of these classes are managed by the strategy factory class, and strategy annotations are automatically added to identify enumeration values, thus achieving automatic generation of strategy pattern code sets.
It simplifies the development process, improves code quality and maintainability, and achieves consistent code style and flexible extensibility for the strategy pattern.
Smart Images

Figure CN119597279B_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of artificial intelligence or other technical fields, and more specifically to a method, apparatus, device, medium and program product for automatic code generation. Background Technology
[0002] In software development, code generation technology is widely used to automatically generate corresponding data access layer code, entity class code, and some business logic code based on the database table structure. However, when faced with scenarios requiring the implementation of the strategy pattern, a significant amount of code still needs to be written manually. Furthermore, as the number of strategy classes increases, this manual writing becomes repetitive and tedious, greatly increasing the development workload. At the same time, the strategy pattern can be implemented in various ways, and strategy pattern code written by different developers is often difficult to reuse and integrate. Summary of the Invention
[0003] In view of the above problems, embodiments of this disclosure provide a method, apparatus, device, medium, and program product for automatic code generation.
[0004] According to a first aspect of this disclosure, a method for automatic code generation is provided, the method comprising: in response to a request for code generation initiated by a target user, obtaining configuration parameters input by the target user; loading a pre-configured strategy component template, and generating an enumeration class and a strategy interface based on the configuration parameters and the strategy component template; generating at least one strategy implementation class based on the enumeration values of the enumeration class using the strategy component template, wherein the strategy implementation class is used to implement the strategy interface; and integrating the enumeration class, the strategy interface, and the strategy implementation class to form a strategy pattern code set.
[0005] According to an embodiment of this disclosure, the method further includes: generating a strategy annotation based on the strategy component template; automatically adding the strategy annotation to the strategy implementation class, wherein the attributes of the strategy annotation include the corresponding enumeration values.
[0006] According to an embodiment of this disclosure, generating at least one strategy implementation class based on the enumeration values of the enumeration class using the strategy component template includes: parsing the enumeration class and extracting multiple enumeration values defined by the enumeration class; traversing each enumeration value and generating a strategy implementation class framework according to the strategy component template; and filling the strategy interface implementation logic in the strategy implementation class framework according to the enumeration values to obtain the strategy implementation class corresponding to each enumeration value.
[0007] According to an embodiment of this disclosure, the method further includes: creating a strategy factory class based on the strategy component template, and configuring the strategy factory class to reference a preset container; retrieving all corresponding strategy implementation classes from the preset container according to different types of the strategy interface through the strategy factory class; creating corresponding strategy class instances according to different types of strategy implementation classes and storing them according to the type of the strategy interface, wherein the strategy class instances are used to represent instantiated objects of the strategy implementation classes.
[0008] According to an embodiment of this disclosure, the step of creating corresponding strategy class instances based on different types of strategy implementation classes and storing them according to the type of the strategy interface further includes: traversing all retrieved strategy implementation classes and determining whether each strategy implementation class is annotated with the strategy annotation; when the strategy implementation class is not annotated with the strategy annotation, filtering the strategy implementation class and storing the strategy class instances corresponding to the filtered strategy implementation classes.
[0009] According to an embodiment of this disclosure, the method further includes: responding to a call request to the strategy interface, determining an enumeration value of the call request based on parameters carried by the call request; obtaining a corresponding strategy class instance based on the enumeration value and the strategy annotation, and executing a preset method in the strategy class instance.
[0010] According to embodiments of this disclosure, the strategy component template includes at least: an enumeration class template, a strategy annotation class template, a strategy interface template, a strategy implementation class template, and a strategy factory template.
[0011] A second aspect of this disclosure provides an automatic code generation apparatus, comprising: an acquisition module, configured to acquire configuration parameters input by the target user in response to a code generation request initiated by the target user; a first generation module, configured to load a pre-configured strategy component template and generate an enumeration class and a strategy interface based on the configuration parameters and the strategy component template; a second generation module, configured to generate at least one strategy implementation class based on the enumeration values of the enumeration class and using the strategy component template, wherein the strategy implementation class is used to implement the strategy interface; and an integration module, configured to integrate the enumeration class, the strategy interface, and the strategy implementation class to form a strategy pattern code set.
[0012] A third aspect of this disclosure provides an electronic device comprising: one or more processors; and a memory for storing one or more computer programs, wherein the one or more processors execute the one or more computer programs to implement the steps of the method described above.
[0013] A fourth aspect of this disclosure also provides a computer-readable storage medium having a computer program or instructions stored thereon, which, when executed by a processor, implement the steps of the above-described method.
[0014] The fifth aspect of this disclosure also provides a computer program product, including a computer program or instructions that, when executed by a processor, implement the steps of the above-described method.
[0015] The code automatic generation method, apparatus, device, medium, and program product provided in this disclosure automatically generate a strategy pattern code set by using custom configuration parameters and strategy component templates. This enables the generation of code sets for scenarios requiring the implementation of strategy patterns, avoiding the need to repeatedly create different classes during development and simplifying the development process. Furthermore, the automatically generated strategy pattern code set has a consistent style, significantly improving code quality. Attached Figure Description
[0016] The foregoing contents, as well as other objects, features, and advantages of this disclosure, will become clearer from the following description of embodiments with reference to the accompanying drawings, in which:
[0017] Figure 1 The illustrations depict application scenarios of the code automatic generation method, apparatus, device, medium, and program product according to embodiments of the present disclosure.
[0018] Figure 2 A flowchart illustrating an automatic code generation method according to an embodiment of the present disclosure is shown schematically;
[0019] Figure 3 Another flowchart of a code automatic generation method according to an embodiment of the present disclosure is illustrated schematically;
[0020] Figure 4 A flowchart illustrating an instance of a storage policy class according to an embodiment of this disclosure is shown schematically;
[0021] Figure 5 A schematic block diagram of a code automatic generation apparatus according to embodiments of the present disclosure is shown; and
[0022] Figure 6 A block diagram schematically illustrates an electronic device suitable for implementing an automatic code generation method according to an embodiment of the present disclosure. Detailed Implementation
[0023] The embodiments of the present disclosure will now be described with reference to the accompanying drawings. However, it should be understood that these descriptions are exemplary only and are not intended to limit the scope of the disclosure. In the following detailed description, numerous specific details are set forth to provide a thorough understanding of the embodiments of the present disclosure for ease of explanation. However, it will be apparent that one or more embodiments may be practiced without these specific details. Furthermore, descriptions of well-known structures and techniques are omitted in the following description to avoid unnecessarily obscuring the concepts of the present disclosure.
[0024] The terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit this disclosure. The terms “comprising,” “including,” etc., as used herein indicate the presence of the stated features, steps, operations, and / or components, but do not exclude the presence or addition of one or more other features, steps, operations, or components.
[0025] All terms used herein (including technical and scientific terms) have the meanings commonly understood by those skilled in the art, unless otherwise defined. It should be noted that the terms used herein are to be interpreted in a manner consistent with the context of this specification, and not in an idealized or overly rigid way.
[0026] When using expressions such as "at least one of A, B and C", they should generally be interpreted in accordance with the meaning that is commonly understood by those skilled in the art (e.g., "a system having at least one of A, B and C" should include, but is not limited to, a system having A alone, a system having B alone, a system having C alone, a system having A and B, a system having A and C, a system having B and C, and / or a system having A, B and C, etc.).
[0027] It should be noted that the code automatic generation method and apparatus specified in this disclosure can be used in the financial field and the field of artificial intelligence, as well as in any field other than the financial field. The application fields of the code automatic generation method and apparatus disclosed in this disclosure are not limited.
[0028] It should be noted that in the embodiments of this application, certain software, components, models and other existing solutions in the industry may be mentioned. These should be regarded as exemplary and are only intended to illustrate the feasibility of implementing the technical solution of this application. However, it does not mean that the applicant has used or necessarily used the solution.
[0029] In the technical solution disclosed herein, the user information (including but not limited to user personal information, user image information, user device information, such as location information) and data (including but not limited to data used for analysis, stored data, and displayed data) involved are all information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, storage, use, processing, transmission, provision, disclosure, and application of related data all comply with relevant laws, regulations, and standards, necessary confidentiality measures have been taken, and they do not violate public order and good morals. Corresponding operation entry points are provided for users to choose to authorize or refuse.
[0030] In scenarios involving automated decision-making using personal information, the methods, devices, and systems provided in this disclosure all offer users corresponding entry points for choosing to agree to or reject the automated decision-making results. If the user chooses to reject, the process proceeds to the expert decision-making stage. Here, "automated decision-making" refers to the activity of automatically analyzing and evaluating an individual's behavioral habits, interests, or economic, health, and credit status through computer programs, and then making a decision. Here, "expert decision-making" refers to the activity of making decisions by personnel who specialize in a particular field, possess specialized experience, knowledge, and skills, and have reached a certain level of professional expertise.
[0031] Embodiments of this disclosure provide a method, apparatus, device, medium, and program product for automatic code generation.
[0032] Figure 1 The illustrations depict application scenarios of the code automatic generation method, apparatus, device, medium, and program product according to embodiments of the present disclosure.
[0033] like Figure 1 As shown, application scenario 100 according to this embodiment may include terminal devices 101, 102, and 103, a network 104, and a server 105. Network 104 serves as a medium for providing communication links between the first terminal device 101, the second terminal device 102, the third terminal device 103, and the server 105. Network 104 may include various connection types, such as wired or wireless communication links, or fiber optic cables, etc.
[0034] Users can use the first terminal device 101, the second terminal device 102, and the third terminal device 103 to interact with the server 105 via the network 104 to receive or send messages, etc. Various communication client applications can be installed on the first terminal device 101, the second terminal device 102, and the third terminal device 103, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social media platform software, etc. (for example only).
[0035] The first terminal device 101, the second terminal device 102, and the third terminal device 103 can be various electronic devices with displays and support web browsing, including but not limited to smartphones, tablets, laptops, and desktop computers.
[0036] Server 105 can be a server that provides various services, such as a backend management server that supports websites browsed by users using the first terminal device 101, the second terminal device 102, and the third terminal device 103 (this is just an example). The backend management server can analyze and process data such as received user requests, and feed back the processing results (such as web pages, information, or data obtained or generated according to user requests) to the terminal devices.
[0037] It should be noted that the automatic code generation method provided in this embodiment can generally be executed by server 105. Correspondingly, the automatic code generation device provided in this embodiment can generally be located in server 105. The automatic code generation method provided in this embodiment can also be executed by a server or server cluster that is different from server 105 and capable of communicating with the first terminal device 101, the second terminal device 102, the third terminal device 103, and / or server 105. Correspondingly, the automatic code generation device provided in this embodiment can also be located in a server or server cluster that is different from server 105 and capable of communicating with the first terminal device 101, the second terminal device 102, the third terminal device 103, and / or server 105.
[0038] It should be understood that Figure 1 The number of terminal devices, networks, and servers shown is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, and servers can be included.
[0039] The following will be based on Figure 1 The described scene, through Figures 2 to 3 The automatic code generation method of the present disclosure embodiments will be described in detail.
[0040] Figure 2 A flowchart illustrating an automatic code generation method according to an embodiment of the present disclosure is shown schematically.
[0041] like Figure 2 As shown, the automatic code generation method in this embodiment includes operations S210 to S240.
[0042] In operation S210, in response to the target user's request to generate code, the configuration parameters input by the target user are obtained.
[0043] In embodiments of this disclosure, user consent or authorization can be obtained before acquiring configuration parameters input by the target user. For example, before operation S210, a request can be sent to the user to acquire configuration parameters input by the target user. If the user consents or authorizes the acquisition of configuration parameters input by the target user, operation S210 is executed.
[0044] In operation S220, a pre-configured strategy component template is loaded, and an enumeration class and strategy interface are generated based on the configuration parameters and the strategy component template.
[0045] In operation S230, based on the enumeration values of the enumeration class, at least one strategy implementation class is generated using the strategy component template, wherein the strategy implementation class is used to implement the strategy interface.
[0046] In the embodiments of this disclosure, a corresponding operation entry point can be provided to the user, allowing the user to choose to agree to or reject the automated decision result. That is, before generating at least one policy implementation class processing / decision using the policy component template, the user's instruction to agree to or reject the processing / decision can be obtained through the corresponding operation entry point. If the user agrees to the processing / decision, at least one policy implementation class processing / decision is generated using the policy component template, i.e., step S230 is executed. If the user rejects the processing / decision, the expert decision-making process is initiated.
[0047] In operation S240, the enumeration class, strategy interface, and strategy implementation class are integrated to form a strategy pattern code set.
[0048] Figure 3 Another flowchart illustrating an automatic code generation method according to an embodiment of the present disclosure is shown.
[0049] In some exemplary embodiments, such as Figure 3As shown, the target user can initiate a code generation request through a graphical user interface or command-line interface. The system first receives and parses the request, and then prompts the user to enter necessary configuration parameters, including but not limited to enumeration type values, names, method parameters, and base package names. In addition, the system has a pre-built set of strategy component templates, containing a basic framework of enumeration classes, strategy interfaces, and strategy implementation classes. After receiving the user's configuration parameters, the system loads the strategy component template matching the request and generates an enumeration class. The class name, enumeration objects, and enumeration object names can all be dynamically generated using custom values defined on the front-end page. Simultaneously, the system can generate a strategy interface, which defines the methods that all strategy implementation classes must implement. The interface name can be dynamically generated, and the interface methods can be defined by the front-end, typically related to the specific execution logic of the strategy. Based on the enumeration values of the enumeration class, one or more strategy implementation classes are generated for each strategy type to implement the methods defined in the strategy interface. The specific strategy execution logic can be defined according to the user-provided configuration parameters or default logic. During the generation of strategy implementation classes, the system can also further customize the implementation classes according to additional user needs, such as adding logging and exception handling. Finally, the generated enumeration classes, strategy interfaces, and all strategy implementation classes are integrated to form a complete strategy pattern code set. This code set can be directly exported to a user-specified file format and saved to a user-specified location.
[0050] For example, when an e-commerce platform needs to generate corresponding strategy code for different promotional activities, the target user first initiates a code generation request through a graphical user interface and inputs the following configuration parameters: promotional strategy (strategy name), discounts, promotions, and gifts (strategy type), order amount, and product category (execution conditions). Based on these configuration parameters, the system loads a pre-set strategy component template and generates the following code set: an enumeration class, PromotionStrategyEnum, containing three enumeration values: FULL_REDUCTION, DISCOUNT, and GIFT; a strategy interface, PromotionStrategyInterface, defining an execute() method for executing specific promotional strategies; and strategy implementation classes, where FullReductionStrategy implements the specific logic of the discount strategy, DiscountStrategy implements the specific logic of the promotion strategy, and GiftStrategy implements the specific logic of the gift strategy.
[0051] Understandably, during development, when encountering situations requiring different specific logic to be handled based on different types, the method provided in this disclosure can be used to replace the cumbersome conditional logic judgments in the development process, optimize the code structure, facilitate future expansion, comply with the open / closed principle, and improve code maintainability and extensibility. Specifically, based on different input types, strategy pattern code is automatically generated, avoiding the need to repeatedly create different classes during development and simplifying the development process. At the same time, the automatically generated strategy pattern code has a consistent style, enabling reuse and integration.
[0052] In the embodiments of this disclosure, the strategy component template includes at least: an enumeration class template, a strategy annotation class template, a strategy interface template, a strategy implementation class template, and a strategy factory template.
[0053] To better implement the strategy pattern, a template engine can be used to configure a series of templates, such as enumeration class templates, strategy annotation class templates, strategy interface templates, strategy implementation class templates, and strategy factory templates. Specifically, the enumeration class template can generate enumeration classes to define strategy types, with each enumeration value representing a specific strategy. Simultaneously, the strategy annotation class template can generate corresponding strategy annotations to mark strategy implementation classes, allowing the strategy factory to find them via reflection. The strategy interface template defines the strategy interface to standardize strategy behavior, implements specific strategy classes to execute different algorithms, generates strategy implementation classes using the strategy implementation class template, and uses the strategy factory template to generate strategy factories to create and manage strategy objects.
[0054] Understandably, strategy component templates can be viewed as a code reuse mechanism. By defining templates, common parts can be extracted and reused when generating specific classes, thereby helping to reduce code redundancy and improve code readability and maintainability.
[0055] Furthermore, generating at least one strategy implementation class based on the enumeration values of the enumeration class and using the strategy component template includes: parsing the enumeration class and extracting multiple enumeration values defined by the enumeration class; traversing each enumeration value and generating a strategy implementation class framework according to the strategy component template; and filling the strategy interface implementation logic in the strategy implementation class framework according to the enumeration values to obtain the strategy implementation class corresponding to each enumeration value.
[0056] Specifically, the process parses an enumeration class containing multiple enumeration values. This enumeration class defines all possible strategy types or behaviors, and each enumeration value represents a specific strategy or behavior, potentially containing additional information such as description and priority. Then, it extracts and iterates through all defined enumeration values from the enumeration class. Simultaneously, it generates a framework for strategy implementation classes based on the strategy implementation class template in the strategy component template. This framework can include class declarations, necessary fields, constructors, and placeholders for the strategy interface implementation methods. Within the strategy implementation class framework, corresponding algorithm or behavior code can be written based on the specific meaning of the enumeration values, filling in the implementation logic of the strategy interface, such as conditional judgments, data processing, and external system calls, thereby generating multiple strategy implementation classes.
[0057] It should be noted that the name of the strategy implementation class can be dynamically generated based on the enumeration values. For example, the names of the enumeration values can be converted to uppercase letters and separated by underscores (if applicable), and then used as part of the class name.
[0058] Understandably, when a new enumeration value is added to an enumeration class, the generation process can be rerun to automatically create the corresponding strategy implementation class. This allows the system to easily handle new requirements, improving its flexibility and scalability. Furthermore, associating the strategy implementation class with the enumeration value makes strategy management and maintenance easier.
[0059] In embodiments of this disclosure, the method further includes: generating a strategy annotation based on a strategy component template; automatically adding the strategy annotation to the strategy implementation class, wherein the attributes of the strategy annotation contain corresponding enumeration values.
[0060] Based on the definition of the strategy annotation class template in the strategy component template, strategy annotation classes can be generated. The attribute values of the annotations can automatically include the corresponding enumeration values and their additional information (if the enumeration values contain this information). Then, the previously generated strategy annotations can be automatically added to the generated strategy implementation class. The attribute values of the annotations can be populated based on the enumeration values and their additional information (if the enumeration values contain this information) to identify the type, description, priority, and other attributes of the strategy through the strategy annotations.
[0061] For example, the type attribute of a strategy annotation can be set to the name of the enumeration value, the description attribute can be set to the description information defined on the enumeration value (if any), and the priority attribute can be set to the priority defined on the enumeration value (if any).
[0062] It should be noted that for any details not covered in this embodiment, please refer to the implementation details of the foregoing embodiments.
[0063] Understandably, by adding strategy annotations to strategy implementation classes, different strategy implementation classes can be obtained from enumeration values. At runtime or compile time, the corresponding strategy implementation class can be easily identified and processed through reflection, thus making strategy selection more flexible and extensible.
[0064] Figure 4 A flowchart illustrating an instance of a storage policy class according to an embodiment of this disclosure is shown schematically.
[0065] Based on the above embodiments, in this embodiment, as follows: Figure 4 As shown, a strategy factory class is created based on the strategy component template and configured so that the strategy factory class references the preset container. Through the strategy factory class, all corresponding strategy implementation classes are retrieved from the preset container according to the different types of strategy interfaces. According to the different types of strategy implementation classes, corresponding strategy class instances are created and stored according to the type of strategy interface. The strategy class instance is used to represent the instantiated object of the strategy implementation class.
[0066] In some exemplary embodiments, a strategy factory class can be created based on the strategy factory template in an existing strategy component template. This class retrieves and creates corresponding strategy class instances from a pre-defined container (such as the Spring container in the open-source Spring framework) based on the type of the strategy interface. The strategy factory class is then configured; for example, it can be configured as an object (such as a bean) within the Spring container to leverage Spring's dependency injection and lifecycle management features. Simultaneously, the strategy factory class can be configured to implement a callback interface (such as the ApplicationContextAware interface in the open-source Spring framework) to obtain a reference to the Spring container during initialization. Furthermore, the strategy factory class can receive the Spring container reference based on the ApplicationContextAware mechanism provided by Spring. After the strategy factory class has a reference to the container, it can utilize Spring's bean lookup functionality (such as the getBean method or getBeansOfType method) to retrieve all corresponding strategy implementation classes from the container based on the type of the strategy interface. For each strategy implementation class, the strategy factory class creates a strategy class instance and stores it according to the type of the strategy interface. For example, this can be implemented using a Map data structure, where the key is the type (or name) of the strategy interface, and the value is the collection of corresponding strategy class instances.
[0067] Understandably, the introduction of the strategy factory class decouples the creation and management of strategy implementation classes from the specific business logic code. That is, the business logic code only needs to obtain the required strategy class instance through the strategy factory class, ignoring the specific implementation and registration process of the strategy implementation class. At the same time, it allows for easy addition of new strategy implementation classes, simplifying the configuration process.
[0068] Furthermore, creating corresponding strategy class instances based on different types of strategy implementation classes and storing them according to the type of the strategy interface also includes: traversing all retrieved strategy implementation classes and determining whether each strategy implementation class is annotated with a strategy annotation; when a strategy implementation class is not annotated with a strategy annotation, filtering the strategy implementation classes and storing the strategy class instances corresponding to the filtered strategy implementation classes.
[0069] Building upon the above embodiments, after obtaining references to all strategy implementation classes in the Spring container, the strategy factory class iterates through these classes and checks them to determine whether they are annotated with a specific strategy annotation. If a strategy implementation class is not annotated with a strategy annotation, it is considered an invalid strategy implementation class and excluded from subsequent processing. For strategy implementation classes annotated with strategy annotations, the strategy factory class creates corresponding instances and stores these instances according to the type of the strategy interface.
[0070] It should be noted that when storing strategy class instances, the strategy implementation classes can be further subdivided and stored based on the attributes (if any) in the strategy annotations. For example, if an annotation contains an attribute indicating the strategy priority, the strategy implementation classes can be stored in order of priority based on this attribute.
[0071] Understandably, by introducing filtering mechanisms for strategy annotations and strategy factory classes, it can be ensured that only strategy implementation classes that truly meet the requirements will be created and stored. This not only improves the system's flexibility and scalability but also enhances the readability and maintainability of the code.
[0072] In embodiments of this disclosure, the method further includes: responding to a call request to the strategy interface, determining an enumeration value of the call request based on the parameters carried in the call request; obtaining a corresponding strategy class instance based on the enumeration value and the strategy annotation, and executing a preset method in the strategy class instance.
[0073] In some exemplary embodiments, when an external system or component needs to execute a certain strategy, it sends a call request to the strategy factory. This request typically contains key information about the required strategy, such as the strategy type or name, and possible parameters. Upon receiving the call request, the strategy factory first parses the parameters carried in the request, such as enumeration values, to identify the strategy type to which the call request points. Then, the strategy factory can use the enumeration values and previously stored strategy annotation information to retrieve the corresponding strategy class instance. For example, it can look up the key in a Map data structure (or other similar data structure) that matches the enumeration value and obtain the set of strategy class instances corresponding to that key. It can then select the most suitable instance from these instances based on other conditions (such as the priority attribute in the strategy annotation) to execute the preset method in the instance and return the execution result to the caller.
[0074] It is understood that the embodiments of this disclosure can directly obtain the corresponding strategy implementation class based on the enumeration type, thereby simplifying the strategy selection and execution process and enabling the caller to more flexibly select the appropriate strategy according to actual needs.
[0075] Based on the above-described automatic code generation method, this disclosure also provides an automatic code generation apparatus. The following will be combined with... Figure 5 The device is described in detail.
[0076] Figure 5 A schematic block diagram of a code automatic generation apparatus according to an embodiment of the present disclosure is shown.
[0077] like Figure 5 As shown, the code automatic generation device 500 of this embodiment includes an acquisition module 510, a first generation module 520, a second generation module 530, and an integration module 540.
[0078] The acquisition module 510 is used to acquire configuration parameters input by the target user in response to a request for code generation initiated by the target user. In one embodiment, the acquisition module 510 can be used to perform the operation S210 described above, which will not be repeated here.
[0079] The first generation module 520 is used to load a pre-configured strategy component template and generate an enumeration class and a strategy interface based on the configuration parameters and the strategy component template. In one embodiment, the first generation module 520 can be used to perform the operation S220 described above, which will not be repeated here.
[0080] The second generation module 530 is used to generate at least one strategy implementation class based on the enumeration values of the enumeration class using a strategy component template, wherein the strategy implementation class is used to implement the strategy interface. In one embodiment, the second generation module 530 can be used to perform the operation S230 described above, which will not be repeated here.
[0081] The integration module 540 is used to integrate the enumeration class, strategy interface, and strategy implementation class to form a strategy pattern code set. In one embodiment, the integration module 540 can be used to perform the operation S240 described above, which will not be repeated here.
[0082] According to embodiments of this disclosure, the strategy component template includes at least: an enumeration class template, a strategy annotation class template, a strategy interface template, a strategy implementation class template, and a strategy factory template.
[0083] According to embodiments of this disclosure, the first generation module 520 can also be used to: generate strategy annotations based on strategy component templates; automatically add strategy annotations to strategy implementation classes, wherein the attributes of the strategy annotations contain corresponding enumeration values.
[0084] According to an embodiment of this disclosure, the second generation module 530 is specifically used for: parsing an enumeration class and extracting multiple enumeration values defined in the enumeration class; traversing each enumeration value and generating a strategy implementation class framework according to the strategy component template; and filling the strategy interface implementation logic in the strategy implementation class framework according to the enumeration values to obtain the strategy implementation class corresponding to each enumeration value.
[0085] According to embodiments of this disclosure, the integration module 540 can also be used to: create a strategy factory class based on a strategy component template, and configure the strategy factory class so that the strategy factory class references a preset container; retrieve all corresponding strategy implementation classes from the preset container according to different types of strategy interfaces through the strategy factory class; create corresponding strategy class instances according to different types of strategy implementation classes and store them according to the type of strategy interface, wherein the strategy class instance is used to represent the instantiated object of the strategy implementation class.
[0086] According to embodiments of this disclosure, the integration module 540 can also be used to: traverse all retrieved strategy implementation classes and determine whether each strategy implementation class is annotated with a strategy annotation; when a strategy implementation class is not annotated with a strategy annotation, filter the strategy implementation classes and store the strategy class instance corresponding to the filtered strategy implementation class.
[0087] According to embodiments of this disclosure, the integration module 540 can also be used to: respond to a call request to the strategy interface, determine the enumeration value of the call request based on the parameters carried in the call request; obtain the corresponding strategy class instance based on the enumeration value and the strategy annotation, and execute the preset method in the strategy class instance.
[0088] According to embodiments of this disclosure, any plurality of modules among the acquisition module 510, the first generation module 520, the second generation module 530, and the integration module 540 can be combined into one module, or any one of these modules can be split into multiple modules. Alternatively, at least part of the functionality of one or more of these modules can be combined with at least part of the functionality of other modules and implemented in one module. According to embodiments of this disclosure, at least one of the acquisition module 510, the first generation module 520, the second generation module 530, and the integration module 540 can be at least partially implemented as hardware circuitry, such as a field-programmable gate array (FPGA), a programmable logic array (PLA), a system-on-a-chip, a system-on-a-substrate, a system-on-package, an application-specific integrated circuit (ASIC), or implemented in hardware or firmware by any other reasonable means of integrating or packaging circuitry, or implemented in any one of software, hardware, and firmware methods, or in a suitable combination of any of these methods. Alternatively, at least one of the acquisition module 510, the first generation module 520, the second generation module 530, and the integration module 540 may be implemented at least partially as a computer program module, which can perform corresponding functions when the computer program module is run.
[0089] Figure 6 A block diagram schematically illustrates an electronic device suitable for implementing an automatic code generation method according to an embodiment of the present disclosure.
[0090] like Figure 6 As shown, an electronic device 600 according to an embodiment of this disclosure includes a processor 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage portion 608 into a random access memory (RAM) 603. The processor 601 may include, for example, a general-purpose microprocessor (e.g., a CPU), an instruction set processor and / or an associated chipset and / or a special-purpose microprocessor (e.g., an application-specific integrated circuit (ASIC)), etc. The processor 601 may also include onboard memory for caching purposes. The processor 601 may include a single processing unit or multiple processing units for performing different actions of the method flow according to an embodiment of this disclosure.
[0091] RAM 603 stores various programs and data required for the operation of electronic device 600. Processor 601, ROM 602, and RAM 603 are interconnected via bus 604. Processor 601 performs various operations of the method flow according to embodiments of the present disclosure by executing programs in ROM 602 and / or RAM 603. It should be noted that the programs may also be stored in one or more memories other than ROM 602 and RAM 603. Processor 601 may also perform various operations of the method flow according to embodiments of the present disclosure by executing programs stored in said one or more memories.
[0092] According to embodiments of this disclosure, the electronic device 600 may further include an input / output (I / O) interface 605, which is also connected to a bus 604. The electronic device 600 may also include one or more of the following components connected to the input / output (I / O) interface 605: an input section 606 including a keyboard, mouse, etc.; an output section 607 including a cathode ray tube (CRT), liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 608 including a hard disk, etc.; and a communication section 609 including a network interface card such as a LAN card, modem, etc. The communication section 609 performs communication processing via a network such as the Internet. A drive 610 is also connected to the input / output (I / O) interface 605 as needed. A removable medium 611, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on the drive 610 as needed so that computer programs read from it can be installed into the storage section 608 as needed.
[0093] This disclosure also provides a computer-readable storage medium, which may be included in the device / apparatus / system described in the above embodiments; or it may exist independently and not assembled into the device / apparatus / system. The computer-readable storage medium carries one or more programs that, when executed, implement the method according to the embodiments of this disclosure.
[0094] According to embodiments of this disclosure, the computer-readable storage medium may be a non-volatile computer-readable storage medium, such as including, but not limited to: portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof. In this disclosure, the computer-readable storage medium may be any tangible medium that contains or stores a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. For example, according to embodiments of this disclosure, the computer-readable storage medium may include ROM 602 and / or RAM 603 and / or one or more memories other than ROM 602 and RAM 603 described above.
[0095] Embodiments of this disclosure also include a computer program product comprising a computer program containing program code for performing the methods shown in the flowchart. When the computer program product is run on a computer system, the program code is used to enable the computer system to implement the automatic code generation method provided in the embodiments of this disclosure.
[0096] When the computer program is executed by the processor 601, it performs the functions defined in the system / apparatus of this disclosure embodiments. According to embodiments of this disclosure, the systems, apparatuses, modules, units, etc., described above can be implemented by computer program modules.
[0097] In one embodiment, the computer program may rely on a tangible storage medium such as an optical storage device or a magnetic storage device. In another embodiment, the computer program may also be transmitted and distributed in the form of signals over a network medium, and downloaded and installed via the communication section 609, and / or installed from the removable medium 611. The program code contained in the computer program can be transmitted using any suitable network medium, including but not limited to: wireless, wired, etc., or any suitable combination thereof.
[0098] In such an embodiment, the computer program can be downloaded and installed from a network via the communication section 609, and / or installed from the removable medium 611. When the computer program is executed by the processor 601, it performs the functions defined in the system of this disclosure embodiment. According to embodiments of this disclosure, the systems, devices, apparatuses, modules, units, etc., described above can be implemented by computer program modules.
[0099] According to embodiments of this disclosure, program code for executing the computer programs provided in embodiments of this disclosure can be written in any combination of one or more programming languages. Specifically, these computational programs can be implemented using high-level procedural and / or object-oriented programming languages, and / or assembly / machine languages. Programming languages include, but are not limited to, languages such as Java, C++, Python, "C", or similar programming languages. The program code can execute entirely on a user's computing device, partially on a user's device, partially on a remote computing device, or entirely on a remote computing device or server. In cases involving remote computing devices, the remote computing device can be connected to the user's computing device via any type of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computing device (e.g., via the Internet using an Internet service provider).
[0100] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this disclosure. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram or flowchart, and combinations of blocks in a block diagram or flowchart, may be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0101] Those skilled in the art will understand that the features described in the various embodiments of this disclosure can be combined and / or combined in various ways, even if such combinations or combinations are not explicitly described in this disclosure. In particular, the features described in the various embodiments of this disclosure can be combined and / or combined in various ways without departing from the spirit and teachings of this disclosure. All such combinations and / or combinations fall within the scope of this disclosure.
[0102] The embodiments of this disclosure have been described above. However, these embodiments are for illustrative purposes only and are not intended to limit the scope of this disclosure. Although various embodiments have been described above, this does not mean that the measures in the various embodiments cannot be used advantageously in combination. Various substitutions and modifications can be made by those skilled in the art without departing from the scope of this disclosure, and all such substitutions and modifications should fall within the scope of this disclosure.
Claims
1. A method for automatically generating codes, characterized by, The method comprises: in response to a request for code generation initiated by a target user, obtaining configuration parameters input by the target user; loading a pre-configured policy component template, and generating an enumeration class and a policy interface according to the configuration parameters and the policy component template; parsing the enumeration class, and extracting a plurality of enumeration values defined by the enumeration class; traversing each of the enumeration values, generating a policy implementation class framework according to the policy component template, and filling the implementation logic of the policy interface in the policy implementation class framework according to the enumeration values to obtain the policy implementation class corresponding to each of the enumeration values, wherein the policy implementation class is used to implement the policy interface; generating a policy annotation based on the policy component template, and automatically adding the policy annotation to each of the policy implementation classes, wherein the attributes of the policy annotation contain the corresponding enumeration values; creating a policy factory class based on the policy component template, and configuring the policy factory class to enable the policy factory class to reference a pre-set container; retrieving all corresponding policy implementation classes from the pre-set container through the policy factory class according to different types of the policy interface; creating corresponding policy class instances according to different types of the policy implementation classes and storing the policy class instances according to the types of the policy interface, wherein the policy class instances are used to represent the instantiated objects of the policy implementation classes; integrating the enumeration class, the policy interface and the policy implementation class to form a policy pattern code set.
2. The method of claim 1, wherein, The method further comprises: traversing all the policy implementation classes retrieved, and determining whether each of the policy implementation classes is annotated with the policy annotation; when the policy implementation class is not annotated with the policy annotation, filtering the policy implementation class, and storing the policy class instance corresponding to the filtered policy implementation class.
3. The method according to claim 1 or 2, characterized in that, The method further comprises: in response to a call request for the policy interface, determining the enumeration value of the call request according to the parameters carried by the call request; based on the enumeration value and the policy annotation, obtaining the corresponding policy class instance, and executing a pre-set method in the policy class instance.
4. The method of claim 1, wherein, The policy component template at least comprises an enumeration class template, a policy annotation class template, a policy interface template, a policy implementation class template and a policy factory template.
5. An apparatus for automatically generating a code, characterized by comprising: The apparatus comprises: an obtaining module configured to obtain configuration parameters input by a target user in response to a request for code generation initiated by the target user; a first generating module configured to load a pre-configured policy component template, and generate an enumeration class and a policy interface according to the configuration parameters and the policy component template; a second generating module configured to parse the enumeration class, extract a plurality of enumeration values defined by the enumeration class, traverse each of the enumeration values, generate a policy implementation class framework according to the policy component template, and fill the implementation logic of the policy interface in the policy implementation class framework according to the enumeration values to obtain the policy implementation class corresponding to each of the enumeration values, wherein the policy implementation class is used to implement the policy interface; The integration module is configured to generate a policy annotation based on the policy component template, and automatically add the policy annotation on each of the policy implementation classes, wherein an attribute of the policy annotation contains the corresponding enumeration value; create a policy factory class based on the policy component template, and configure the policy factory class to enable the policy factory class to refer to a preset container; retrieve all corresponding policy implementation classes from the preset container according to different types of the policy interface through the policy factory class; create corresponding policy class instances according to different types of the policy implementation classes and store the policy class instances according to the types of the policy interface, wherein the policy class instances are used to represent instantiated objects of the policy implementation classes; and integrate the enumeration class, the policy interface and the policy implementation classes to form a policy pattern code set. 6.An electronic device, comprising: one or more processors; storage configured to store one or more computer programs, wherein the one or more programs, when executed by the one or more processors, cause the one or more processors to perform the method of any of claims 1-4.
7. A computer-readable storage medium having stored thereon executable instructions that cause a processor-based system to perform steps comprising: The instructions, when executed by a processor, cause the processor to perform the method of any of claims 1-4.
8. A computer program product comprising a computer program, characterized in that, The computer program, when executed by a processor, implements the method of any of claims 1-4.
Citation Information
Patent Citations
Program development method and device
CN113778388A
Code generation method and device, equipment and storage medium
CN116880856A