Code automatic generation method and device, equipment, medium and program product
Through the automatic code generation method, the policy model is used to generate a policy pattern code set, which solves the problem of poor code repetition and reusability in the policy pattern implementation, and achieves the effect of simplifying the development process and improving the code quality.
Patent Information
- Application Number
- CN202411588202.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-08
- Publication Date
- 2025-03-11
- Estimated Expiration
- 2044-11-08
AI Technical Summary
In the process of software development, when facing scenarios where policy patterns need to be implemented, a large amount of repetitive and tedious code needs to be written manually, and the policy pattern code of different developers is difficult to reuse and integrate.
Provides an automatic code generation method, which can generate enumeration classes, policy interfaces and policy implementation classes by responding to the request of the target user, obtain configuration parameters, load pre-configured policy component templates, generate enumeration classes, policy interfaces and policy implementation classes, and integrate them into a policy pattern code set.
Simplifies the development process, avoids the repeated creation of different classes, and the generated policy patterns are unified in the code style, improves the code quality, and is easy to reuse and integrate.
Smart Images

Figure CN119597279A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of artificial intelligence or other technical fields, and more specifically to a method, device, equipment, medium and program product for automatic code generation. Background Art
[0002] In the software development process, code generation technology has been widely used to automatically generate corresponding data access layer code, entity class code, and some business logic code according to the database table structure. However, when faced with scenarios that require the implementation of strategy patterns, a large amount of code still needs to be written manually, and as the number of strategy classes increases, this manual writing work becomes repetitive and cumbersome, greatly increasing the development workload. At the same time, the implementation methods of strategy patterns are flexible and diverse, and strategy pattern codes written by different developers are often difficult to reuse and integrate. Summary of the invention
[0003] In view of the above problems, embodiments of the present disclosure provide a method, apparatus, device, medium and program product for automatic code generation.
[0004] According to a first aspect of the present 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 policy component template, and generating an enumeration class and a policy interface according to the configuration parameters and the policy component template; based on the enumeration value of the enumeration class, generating at least one policy implementation class using the policy component template, wherein the policy implementation class is used to implement the policy interface; integrating the enumeration class, the policy interface and the policy implementation class to form a policy pattern code set.
[0005] According to an embodiment of the present disclosure, the method further includes: generating a policy annotation based on the policy component template; and automatically adding the policy annotation to the policy implementation class, wherein the attributes of the policy annotation include the corresponding enumeration value.
[0006] According to an embodiment of the present disclosure, the generation of at least one policy implementation class based on the enumeration value of the enumeration class using the policy component template includes: parsing the enumeration class to extract the multiple enumeration values defined by the enumeration class; traversing each of the enumeration values and generating a policy implementation class framework based on the policy component template; in the policy implementation class framework, filling in the implementation logic of the policy interface according to the enumeration value to obtain the policy implementation class corresponding to each of the enumeration values.
[0007] According to an embodiment of the present disclosure, the method also includes: creating a policy factory class based on the policy component template, and configuring the policy factory class so that the policy factory class references a preset container; through the policy factory class, retrieving all corresponding policy implementation classes from the preset container according to different types of the policy interface; creating corresponding policy class instances according to different types of policy implementation classes and storing them according to the type of the policy interface, wherein the policy class instance is used to represent the instantiated object of the policy implementation class.
[0008] According to an embodiment of the present disclosure, creating corresponding policy class instances according to different types of policy implementation classes and storing them according to the type of the policy interface also includes: traversing all the retrieved policy implementation classes and determining whether each of the policy implementation classes is marked with the policy annotation; when the policy implementation class is not marked with the policy annotation, filtering the policy implementation class and storing the policy class instances corresponding to the filtered policy implementation class.
[0009] According to an embodiment of the present disclosure, the method also includes: in response to a call request to the policy interface, determining an enumeration value of the call request according to parameters carried by the call request; based on the enumeration value and the policy annotation, obtaining the corresponding policy class instance, and executing a preset method in the policy class instance.
[0010] According to an embodiment of the present disclosure, the policy component template includes at least: an enumeration class template, a policy annotation class template, a policy interface template, a policy implementation class template and a policy factory template.
[0011] The second aspect of the present disclosure provides a device for automatic code generation, which includes: an acquisition module, which is used to respond to a request for code generation initiated by a target user and acquire configuration parameters input by the target user; a first generation module, which is used 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 generation module, which is used to generate at least one policy implementation class based on the enumeration value of the enumeration class and using the policy component template, wherein the policy implementation class is used to implement the policy interface; an integration module, which is used to integrate the enumeration class, the policy interface and the policy implementation class to form a policy pattern code set.
[0012] A third aspect of the present 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 above method.
[0013] The fourth aspect of the present disclosure further provides a computer-readable storage medium having a computer program or instructions stored thereon, which implements the steps of the above method when the above computer program or instructions are executed by a processor.
[0014] The fifth aspect of the present disclosure further provides a computer program product, including a computer program or instructions, which implement the steps of the above method when the above computer program or instructions are executed by a processor.
[0015] According to the code automatic generation method, device, equipment, medium and program product provided by the embodiments of the present disclosure, the policy pattern code set is automatically generated through customized configuration parameters and using the policy component template, so that it can face the scenario where the policy pattern needs to be implemented, avoiding the need to repeatedly create different classes during the development process, and simplifying the development process. At the same time, the style of the automatically generated policy pattern code set is unified, and the code quality has been significantly improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] The above contents and other objects, features and advantages of the present disclosure will become more apparent through the following description of the embodiments of the present disclosure with reference to the accompanying drawings, in which:
[0017] Figure 1 The application scenario diagram of the automatic code generation method, apparatus, device, medium and program product according to the embodiment of the present disclosure is schematically shown;
[0018] Figure 2 The flowchart of the method for automatically generating codes according to the embodiment of the present disclosure is schematically shown;
[0019] Figure 3 Another flow chart of the method for automatically generating codes according to an embodiment of the present disclosure is schematically shown;
[0020] Figure 4 A flowchart of a storage policy class instance according to an embodiment of the present disclosure is schematically shown;
[0021] Figure 5 A schematic diagram showing a structural block diagram of a device for automatically generating codes according to an embodiment of the present disclosure; and
[0022] Figure 6 A block diagram of an electronic device suitable for implementing a method for automatically generating codes according to an embodiment of the present disclosure is schematically shown. DETAILED DESCRIPTION
[0023] Hereinafter, embodiments of the present disclosure will 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 present disclosure. In the following detailed description, for ease of explanation, many specific details are set forth to provide a comprehensive understanding of the embodiments of the present disclosure. However, it is apparent that one or more embodiments may also be implemented without these specific details. In addition, in the following description, descriptions of known structures and technologies are omitted to avoid unnecessary confusion of the concepts of the present disclosure.
[0024] The terms used herein are only for describing specific embodiments and are not intended to limit the present disclosure. The terms "comprise", "include", etc. used herein indicate the existence of the features, steps, operations and / or components, but do not exclude the existence or addition of one or more other features, steps, operations or components.
[0025] All terms (including technical and scientific terms) used herein have the meanings commonly understood by those skilled in the art unless otherwise defined. It should be noted that the terms used herein should be interpreted as having a meaning consistent with the context of this specification and should not be interpreted in an idealized or overly rigid manner.
[0026] When using expressions such as "at least one of A, B, and C, etc.", they should generally be interpreted according to the meaning of the expression commonly understood by those skilled in the art (for example, "a system having at least one of A, B, and C" should include but is not limited to a system having A alone, B alone, C alone, A and B, A and C, B and C, and / or A, B, C, etc.).
[0027] It should be noted that the automatic code generation method and device disclosed in the present invention can be used in the financial field and the artificial intelligence field, and can also be used in any field other than the financial field. The application field of the automatic code generation method and device disclosed in the present invention is not limited.
[0028] It should be noted that in the embodiments of the present application, certain software, components, models and other existing solutions in the industry may be mentioned, and they should be regarded as exemplary. Their purpose is only to illustrate the feasibility of implementing the technical solution of the present application, but it does not mean that the applicant has or will necessarily use the solution.
[0029] In the technical solution of the present disclosure, the user information (including but not limited to user personal information, user image information, user device information, such as location information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved are all information and data authorized by the user or fully authorized by all parties, and the collection, storage, use, processing, transmission, provision, disclosure and application of the relevant data comply with relevant laws, regulations and standards, take necessary confidentiality measures, do not violate public order and good morals, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0030] In the scenario of using personal information for automated decision-making, the methods, devices, and systems provided by the embodiments of the present disclosure provide users with corresponding operation portals for users to choose to agree or reject the automated decision-making results; if the user chooses to reject, the expert decision-making process will be entered. The expression "automated decision-making" here refers to the activity of automatically analyzing and evaluating a person's behavioral habits, interests and hobbies, or economic, health, credit status, etc. through computer programs, and making decisions. The expression "expert decision-making" here refers to the activity of making decisions by people who specialize in a certain field, have specialized experience, knowledge and skills, and have reached a certain level of professionalism.
[0031] Embodiments of the present disclosure provide a method, apparatus, device, medium, and program product for automatic code generation.
[0032] Figure 1 The application scenario diagram of the automatic code generation method, apparatus, device, medium and program product according to the embodiments of the present disclosure is schematically shown.
[0033] like Figure 1 As shown, the application scenario 100 according to this embodiment may include terminal devices 101, 102, 103, a network 104, and a server 105. The network 104 is used to provide a medium for communication links between the first terminal device 101, the second terminal device 102, the third terminal device 103, and the server 105. The network 104 may include various connection types, such as wired, wireless communication links, or optical fiber cables, etc.
[0034] The user can use the first terminal device 101, the second terminal device 102, and the third terminal device 103 to interact with the server 105 through 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 platform software, etc. (only for example).
[0035] The first terminal device 101, the second terminal device 102, and the third terminal device 103 may be various electronic devices having display screens and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, desktop computers, and the like.
[0036] The server 105 may be a server that provides various services, such as a background management server (only as an example) that provides support for websites browsed by users using the first terminal device 101, the second terminal device 102, and the third terminal device 103. The background management server may analyze and process the received data such as 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 device.
[0037] It should be noted that the automatic code generation method provided in the embodiment of the present disclosure can generally be executed by the server 105. Accordingly, the automatic code generation device provided in the embodiment of the present disclosure can generally be set in the server 105. The automatic code generation method provided in the embodiment of the present disclosure can also be executed by a server or server cluster that is different from the server 105 and can communicate with the first terminal device 101, the second terminal device 102, the third terminal device 103 and / or the server 105. Correspondingly, the automatic code generation device provided in the embodiment of the present disclosure can also be set in a server or server cluster that is different from the server 105 and can communicate with the first terminal device 101, the second terminal device 102, the third terminal device 103 and / or the server 105.
[0038] It should be understood that Figure 1 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.
[0039] The following will be based on Figure 1 The scene described by Figures 2 to 3 The code automatic generation method of the embodiment of the present disclosure is described in detail.
[0040] Figure 2 The flowchart of the method for automatic code generation according to an embodiment of the present disclosure is schematically shown.
[0041] like Figure 2 As shown, the code automatic generation method of this embodiment includes operations S210 to S240.
[0042] In operation S210 , in response to a request from a target user to initiate code generation, configuration parameters input by the target user are obtained.
[0043] In an embodiment of the present disclosure, before obtaining the configuration parameters input by the target user, the user's consent or authorization may be obtained. For example, before operation S210, a request to obtain the configuration parameters input by the target user may be issued to the user. When the user agrees or authorizes that the configuration parameters input by the target user may be obtained, operation S210 is performed.
[0044] In operation S220, a pre-configured policy component template is loaded, and an enumeration class and a policy interface are generated according to the configuration parameters and the policy component template.
[0045] In operation S230, at least one policy implementation class is generated using a policy component template based on the enumeration value of the enumeration class, wherein the policy implementation class is used to implement a policy interface.
[0046] In the embodiments of the present disclosure, a corresponding operation entry may be provided for the user to choose to agree or reject the automated decision result. That is, before generating at least one policy implementation class process / decision using the policy component template, an instruction of the user to agree or reject the process / decision input through the corresponding operation entry may be obtained. If the user agrees to process / decision, at least one policy implementation class process / decision is generated using the policy component template, and step S230 is executed. If the user refuses to process / decision, the expert decision process is entered.
[0047] In operation S240, the enumeration class, the policy interface, and the policy implementation class are integrated to form a policy pattern code set.
[0048] Figure 3 Another flowchart of the method for automatic code generation according to an embodiment of the present disclosure is schematically shown.
[0049] In some exemplary embodiments, Figure 3As shown, the target user can initiate a code generation request through a graphical user interface or a command line interface. The system first receives and parses the request, and then prompts the user to enter the necessary configuration parameters, which include but are not limited to enumeration type values, names, method parameters, and basic package names. In addition, a set of policy component templates are preset in the system, including the basic framework of enumeration classes, policy interfaces, and policy implementation classes. After receiving the user's configuration parameters, the system loads the policy component template that matches the request and generates an enumeration class. The name of the class, the enumeration object, and the name of the enumeration object can all be dynamically generated through the values customized by the front-end page. At the same time, the system can generate a policy interface, which defines the methods that all policy implementation classes must implement. Among them, the interface name can be dynamically generated, and the interface method can be customized by the front-end, which is usually related to the specific execution logic of the policy. Based on the enumeration value of the enumeration class, one or more policy implementation classes are generated for each policy type to implement the methods defined in the policy interface, and the specific policy execution logic can be defined according to the configuration parameters or default logic provided by the user. In the process of generating the policy implementation class, the system can also further customize the implementation class according to the user's additional needs, such as adding logging, exception handling, etc. Finally, the generated enumeration class, strategy interface and all strategy implementation classes are integrated to form a complete strategy pattern code set, which can be directly exported to the file format specified by the user and saved to the location specified by the user.
[0050] For example, when an e-commerce platform needs to generate corresponding strategy codes for different promotional activities, in this process, the target user first initiates a code generation request through the graphical user interface and enters the following configuration parameters: promotion strategy (strategy name), full reduction, discount and gift (strategy type), order amount and product category (execution condition). Based on these configuration parameters, the system loads the preset strategy component template and generates the following code set: an enumeration class, namely PromotionStrategyEnum, which contains three enumeration values: FULL_REDUCTION, DISCOUNT, and GIFT; a strategy interface, namely PromotionStrategyInterface, which defines an execute() method for executing specific promotion strategies. Strategy implementation class, in which FullReductionStrategy is used to implement the specific logic of the full reduction strategy, DiscountStrategy is used to implement the specific logic of the discount strategy, and GiftStrategy is used to implement the specific logic of the gift strategy.
[0051] It is understandable that, during the development process, when encountering the need to process different specific logics according to different types, the method provided by the embodiment of the present disclosure can be used for development, replacing the complicated conditional logic judgments in the development process, optimizing the code structure, facilitating future expansion, complying with the open-closed principle, and improving the maintainability and extensibility of the code. Specifically, according to the different types of input, the strategy pattern code is automatically generated to avoid the need to repeatedly create different classes during the development process, thereby simplifying the development process. At the same time, the style of the automatically generated strategy pattern code is unified to achieve reuse and integration.
[0052] In an embodiment of the present disclosure, a policy component template includes at least: an enumeration class template, a policy annotation class template, a policy interface template, a policy implementation class template, and a policy factory template.
[0053] In order to better implement the strategy pattern, you can use the template engine 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. Among them, the enumeration class can be generated according to the enumeration class template to define the policy type, and each enumeration value represents a specific strategy. At the same time, the corresponding policy annotation can be generated according to the policy annotation class template, and the policy implementation class can be marked with annotations so that the policy factory can find them through reflection. The strategy interface template defines the strategy interface to standardize the strategy behavior, implements the specific strategy class to execute different algorithms, the strategy implementation class is generated through the strategy implementation class template, and the strategy factory template is used to generate the strategy factory to create and manage policy objects.
[0054] It is understandable that the policy component template can be regarded as a code reuse mechanism. By defining a template, the common parts can be extracted and reused when generating specific classes, which helps to reduce code redundancy and improve code readability and maintainability.
[0055] Furthermore, based on the enumeration value of the enumeration class, generating at least one policy implementation class using a policy component template includes: parsing the enumeration class and extracting multiple enumeration values defined by the enumeration class; traversing each enumeration value and generating a policy implementation class framework according to the policy component template; in the policy implementation class framework, filling in the implementation logic of the policy interface according to the enumeration value to obtain a policy implementation class corresponding to each enumeration value.
[0056] Specifically, parse the enumeration class containing multiple enumeration values. The enumeration class defines all possible policy types or behaviors. The enumeration value can represent a specific policy or behavior and can contain some additional information (such as description, priority, etc.). Then, extract and traverse all defined enumeration values from the enumeration class. At the same time, generate the framework of the policy implementation class according to the policy implementation class template in the policy component template. The framework can include class declarations, necessary fields, constructors, and implementation method placeholders of the policy interface. In the policy implementation class framework, you can write corresponding algorithms or behavior codes according to the specific meaning of the enumeration value, fill in the implementation logic of the policy interface, such as conditional judgment, data processing, and external system calls, so as to generate multiple policy implementation classes.
[0057] It should be noted that the name of the strategy implementation class can be dynamically generated based on the enumeration value. For example, the name of the enumeration value can be converted to uppercase letters and separated by underscores (if applicable), and then used as part of the class name.
[0058] It is understandable that when a new enumeration value is added to the enumeration class, the policy implementation class corresponding to the new enumeration value can be automatically created by simply rerunning the generation process, so that the system can easily cope with the emergence of new requirements and improve the flexibility and scalability of the system. At the same time, by associating the policy implementation class with the enumeration value, it is easier to manage and maintain the policy.
[0059] In an embodiment of the present disclosure, the method further includes: generating a policy annotation based on a policy component template; and automatically adding the policy annotation to the policy implementation class, wherein the attributes of the policy annotation include corresponding enumeration values.
[0060] According to the definition of the policy annotation class template in the policy component template, a policy annotation class can be generated, and the attribute value of the annotation can automatically include the corresponding enumeration value and its additional information (if the enumeration value contains such information). Then, the previously generated policy annotation can be automatically added to the generated policy implementation class. The attribute value of the annotation can be filled according to the enumeration value and its additional information (if the enumeration value contains such information) to identify the type, description, priority and other attributes of the policy through the policy annotation.
[0061] For example, the type attribute of the policy 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 details not included in this embodiment, please refer to the implementation details of the aforementioned embodiment.
[0063] It can be understood that by adding policy annotations to the policy implementation class, different policy implementation classes can be obtained by enumeration values, and the corresponding policy implementation classes can be easily identified and processed through the reflection mechanism at runtime or compile time, so that the policy selection is more flexible and extensible.
[0064] Figure 4 The flowchart of the storage policy class instance according to an embodiment of the present disclosure is schematically shown.
[0065] Based on the above embodiments, in this embodiment, Figure 4 As shown, based on the policy component template, a policy factory class is created, and the policy factory class is configured so that the policy factory class references a preset container; through the policy factory class, all corresponding policy implementation classes are retrieved from the preset container according to different types of policy interfaces; according to different types of policy implementation classes, corresponding policy class instances are created and stored according to the type of policy interface, wherein the policy class instance is used to represent the instantiated object of the policy implementation class.
[0066] In some exemplary embodiments, a policy factory class can be created according to a policy factory template in an existing policy component template to retrieve and create a corresponding policy class instance from a preset container (such as a Spring container in an open source Spring framework) according to the type of the policy interface. Then, the policy factory class is configured. For example, the policy factory class can be configured as an object (such as a bean) in a Spring container to utilize Spring's dependency injection and lifecycle management functions. At the same time, the policy factory class can also 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 policy factory class can receive a reference to the Spring container based on the ApplicationContextAware mechanism provided by Spring. After the policy factory class has a reference to the container, the bean search function of Spring (such as the getBean method or the getBeansOfType method) can be used to retrieve all corresponding policy implementation classes from the container according to the type of the policy interface. For each policy implementation class, the policy factory class creates a policy class instance and stores it according to the type of the policy interface. For example, this can be implemented by using a Map data structure, in which the key is the type of the policy interface (or the name of the type) and the value is the corresponding policy class instance set.
[0067] It is understandable that the introduction of the policy factory class decouples the creation and management of the policy implementation class from the specific business logic code, that is, the business logic code only needs to obtain the required policy class instance through the policy factory class, while ignoring the specific implementation and registration process of the policy implementation class. At the same time, it is easy to add new policy implementation classes and simplify the configuration process of the policy implementation class.
[0068] Furthermore, according to different types of policy implementation classes, corresponding policy class instances are created and stored according to the type of policy interface, which also includes: traversing all retrieved policy implementation classes and determining whether each policy implementation class is marked with a policy annotation; when the policy implementation class is not marked with a policy annotation, filtering the policy implementation class and storing the policy class instances corresponding to the filtered policy implementation class.
[0069] Based on the above embodiment, after obtaining references to all strategy implementation classes in the Spring container, the strategy factory class will traverse these implementation classes and check them to determine whether they are marked with specific strategy annotations. If the strategy implementation class is not marked with a strategy annotation, it is regarded as an invalid strategy implementation class and excluded from subsequent processing. For strategy implementation classes marked with strategy annotations, the strategy factory class will create corresponding instances and store these instances according to the type of the strategy interface.
[0070] It should be noted that when storing policy class instances, the policy implementation classes can be further subdivided and stored according to the attributes in the policy annotation (if any). For example, if an annotation contains an attribute indicating the policy priority, the policy implementation classes can be sorted and stored according to the priority based on this attribute.
[0071] It can be understood that by introducing the filtering mechanism of policy annotations and policy factory classes, it can be ensured that only policy implementation classes that truly meet the requirements will be created and stored, which not only improves the flexibility and scalability of the system, but also enhances the readability and maintainability of the code.
[0072] In an embodiment of the present disclosure, the method also includes: in response to a call request to a policy interface, determining an enumeration value of the call request according to parameters carried in the call request; based on the enumeration value and policy annotation, obtaining a corresponding policy class instance, and executing a preset method in the policy class instance.
[0073] In some exemplary embodiments, when an external system or component needs to execute a certain policy, it sends a call request to the policy factory, which usually contains some key information about the required policy, such as the type or name of the policy, and the parameters that may be required. After receiving the call request, the policy factory will first parse the parameters carried in the request, such as the enumeration value, to identify the policy type pointed to by the call request. Then, the policy factory can use the enumeration value and the previously stored policy annotation information to retrieve the corresponding policy class instance. For example, you can find the key that matches the enumeration value in the Map data structure (or other similar data structures) and obtain the set of policy class instances corresponding to the key. Then, you can select the most suitable instance from these instances based on other conditions (such as the priority attribute in the policy annotation) to execute the preset method in the instance, and return the execution result to the caller.
[0074] It is understandable that the embodiments of the present disclosure can directly obtain the corresponding policy implementation class according to the enumeration type, thereby simplifying the policy selection and execution process, so that the caller can more flexibly select the appropriate policy according to actual needs.
[0075] Based on the above code automatic generation method, the present disclosure also provides a code automatic generation device. Figure 5 The device is described in detail.
[0076] Figure 5 The structural block diagram of the automatic code generation device according to the embodiment of the present disclosure is schematically shown.
[0077] like Figure 5 As shown, the automatic code 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 respond to the code generation request initiated by the target user and acquire the configuration parameters input 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 described in detail here.
[0079] The first generation module 520 is used 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. In one embodiment, the first generation module 520 can be used to perform the operation S220 described above, which will not be described in detail here.
[0080] The second generation module 530 is used to generate at least one policy implementation class based on the enumeration value of the enumeration class using the policy component template, wherein the policy implementation class is used to implement the policy 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, the policy interface and the policy implementation class to form a policy pattern code set. In one embodiment, the integration module 540 can be used to perform the operation S240 described above, which will not be described in detail here.
[0082] According to an embodiment of the present disclosure, a policy component template includes at least: an enumeration class template, a policy annotation class template, a policy interface template, a policy implementation class template and a policy factory template.
[0083] According to an embodiment of the present disclosure, the first generation module 520 may also be used to: generate a policy annotation based on a policy component template; and automatically add a policy annotation to a policy implementation class, wherein the attributes of the policy annotation include corresponding enumeration values.
[0084] According to an embodiment of the present disclosure, the second generation module 530 is specifically used to: parse the enumeration class and extract multiple enumeration values defined by the enumeration class; traverse each enumeration value and generate a policy implementation class framework based on the policy component template; in the policy implementation class framework, fill in the implementation logic of the policy interface according to the enumeration value to obtain the policy implementation class corresponding to each enumeration value.
[0085] According to an embodiment of the present disclosure, the integration module 540 can also be used to: create a policy factory class based on a policy component template, and configure the policy factory class so that the policy factory class references a preset container; through the policy factory class, retrieve all corresponding policy implementation classes from the preset container according to different types of policy interfaces; create corresponding policy class instances according to different types of policy implementation classes and store them according to the type of policy interface, wherein the policy class instance is used to represent the instantiated object of the policy implementation class.
[0086] According to an embodiment of the present disclosure, the integration module 540 can also be used to: traverse all retrieved policy implementation classes and determine whether each policy implementation class is marked with a policy annotation; when the policy implementation class is not marked with a policy annotation, filter the policy implementation class and store the policy class instance corresponding to the filtered policy implementation class.
[0087] According to an embodiment of the present disclosure, the integration module 540 can also be used to: respond to a call request to a policy interface, determine the enumeration value of the call request according to the parameters carried by the call request; based on the enumeration value and policy annotation, obtain the corresponding policy class instance, and execute the preset method in the policy class instance.
[0088] According to an embodiment of the present disclosure, any multiple 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 for implementation, or any one of the modules can be split into multiple modules. Alternatively, at least part of the functions of one or more of these modules can be combined with at least part of the functions of other modules and implemented in one module. According to an embodiment of the present 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 a hardware circuit, 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 a package, an application specific integrated circuit (ASIC), or can be implemented by hardware or firmware such as any other reasonable way of integrating or packaging the circuit, or implemented in any one of the three implementation methods of software, hardware and firmware or in a suitable combination of any of them. 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 at least partially implemented as a computer program module, and when the computer program module is executed, a corresponding function may be executed.
[0089] Figure 6 A block diagram of an electronic device suitable for implementing a method for automatically generating codes according to an embodiment of the present disclosure is schematically shown.
[0090] like Figure 6 As shown, the electronic device 600 according to an embodiment of the present 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 part 608 to 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 a related chipset and / or a dedicated microprocessor (e.g., an application-specific integrated circuit (ASIC)), etc. The processor 601 may also include an 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 the present disclosure.
[0091] In RAM 603, various programs and data required for the operation of electronic device 600 are stored. Processor 601, ROM 602 and RAM 603 are connected to each other via bus 604. Processor 601 performs various operations of the method flow according to the embodiment of the present disclosure by executing the program in ROM 602 and / or RAM 603. It should be noted that the program can also be stored in one or more memories other than ROM 602 and RAM 603. Processor 601 can also perform various operations of the method flow according to the embodiment of the present disclosure by executing the program stored in the one or more memories.
[0092] According to an embodiment of the present disclosure, the electronic device 600 may further include an input / output (I / O) interface 605, which is also connected to the bus 604. The electronic device 600 may further include one or more of the following components connected to the input / output (I / O) interface 605: an input portion 606 including a keyboard, a mouse, etc.; an output portion 607 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage portion 608 including a hard disk, etc.; and a communication portion 609 including a network interface card such as a LAN card, a modem, etc. The communication portion 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 magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 610 as needed, so that a computer program read therefrom is installed into the storage portion 608 as needed.
[0093] The present disclosure also provides a computer-readable storage medium, which may be included in the device / apparatus / system described in the above embodiments; or may exist independently without being assembled into the device / apparatus / system. The above computer-readable storage medium carries one or more programs, and when the above one or more programs are executed, the method according to the embodiment of the present disclosure is implemented.
[0094] According to an embodiment of the present disclosure, the computer-readable storage medium may be a non-volatile computer-readable storage medium, for example, it may include but is not limited to: a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program, which may be used by or in combination with an instruction execution system, an apparatus or a device. For example, according to an embodiment of the present disclosure, the computer-readable storage medium may include the ROM 602 and / or RAM 603 described above and / or one or more memories other than ROM 602 and RAM 603.
[0095] The embodiment of the present disclosure also includes a computer program product, which includes a computer program, and the computer program contains program code for executing the method shown in the flowchart. When the computer program product is run in a computer system, the program code is used to enable the computer system to implement the code automatic generation method provided by the embodiment of the present disclosure.
[0096] The above functions defined in the system / device of the embodiment of the present disclosure are performed when the computer program is executed by the processor 601. According to the embodiment of the present disclosure, the system, device, module, unit, etc. described above can be implemented by a computer program module.
[0097] In one embodiment, the computer program may rely on tangible storage media such as optical storage devices, magnetic storage devices, etc. In another embodiment, the computer program may also be transmitted and distributed in the form of signals on a network medium, and downloaded and installed through the communication part 609, and / or installed from a removable medium 611. The program code contained in the computer program may be transmitted using any appropriate network medium, including but not limited to: wireless, wired, etc., or any suitable combination of the above.
[0098] In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 609, and / or installed from the removable medium 611. When the computer program is executed by the processor 601, the above functions defined in the system of the embodiment of the present disclosure are performed. According to the embodiment of the present disclosure, the system, device, apparatus, module, unit, etc. described above can be implemented by a computer program module.
[0099] According to an embodiment of the present disclosure, the program code for executing the computer program provided by the embodiment of the present disclosure can be written in any combination of one or more programming languages. Specifically, these computing programs can be implemented using high-level process and / or object-oriented programming languages, and / or assembly / machine languages. Programming languages include, but are not limited to, Java, C++, python, "C" language or similar programming languages. The program code can be executed entirely on the user computing device, partially on the user device, partially on the remote computing device, or entirely on the remote computing device or server. In the case of a remote computing device, the remote computing device can be connected to the user computing device through any type of network, including a local area network (LAN) or a wide area network (WAN), or can be connected to an external computing device (for example, using an Internet service provider to connect through the Internet).
[0100] The flow charts and block diagrams in the accompanying drawings illustrate the possible architecture, functions and operations of the systems, methods and computer program products according to various embodiments of the present disclosure. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0101] It will be appreciated by those skilled in the art that the features described in the various embodiments of the present disclosure may be combined and / or combined in a variety of ways, even if such combinations or combinations are not explicitly described in the present disclosure. In particular, without departing from the spirit and teachings of the present disclosure, the features described in the various embodiments of the present disclosure may be combined and / or combined in a variety of ways. All of these combinations and / or combinations fall within the scope of the present disclosure.
[0102] The embodiments of the present disclosure are described above. However, these embodiments are only for illustrative purposes and are not intended to limit the scope of the present disclosure. Although the embodiments are described above, this does not mean that the measures in the various embodiments cannot be used in combination to advantage. Without departing from the scope of the present disclosure, those skilled in the art may make a variety of substitutions and modifications, which should all fall within the scope of the present disclosure.
Claims
1. A method for automatic code generation, characterized in that: 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; Based on the enumeration value of the enumeration class, generating at least one policy implementation class using the policy component template, wherein the policy implementation class is used to implement the policy interface; The enumeration class, the strategy interface and the strategy implementation class are integrated to form a strategy pattern code set.
2. The method according to claim 1, characterized in that The method further comprises: Generate a policy annotation based on the policy component template; The policy annotation is automatically added to the policy implementation class, wherein the attributes of the policy annotation include the corresponding enumeration value.
3. The method according to claim 1, characterized in that The step of generating at least one policy implementation class based on the enumeration value of the enumeration class and using the policy component template comprises: Parsing the enumeration class, and extracting a plurality of the enumeration values defined by the enumeration class; Traversing each of the enumeration values, and generating a strategy implementation class framework according to the strategy component template; In the policy implementation class framework, the implementation logic of the policy interface is filled according to the enumeration value to obtain the policy implementation class corresponding to each enumeration value.
4. The method according to claim 2, characterized in that: The method further comprises: Based on the policy component template, create a policy factory class, and configure the policy factory class so that the policy factory class references a preset container; Through the strategy factory class, according to different types of the strategy interface, all corresponding strategy implementation classes are retrieved from the preset container; According to different types of the policy implementation classes, corresponding policy class instances are created and stored according to the type of the policy interface, wherein the policy class instances are used to represent instantiated objects of the policy implementation class.
5. The method according to claim 4, characterized in that The creating corresponding strategy class instances according to different types of strategy implementation classes and storing them according to the type of strategy interface also includes: Traversing all the retrieved policy implementation classes, and determining whether each policy implementation class is marked with the policy annotation; When the policy implementation class is not marked with the policy annotation, the policy implementation class is filtered, and the policy class instance corresponding to the filtered policy implementation class is stored.
6. The method according to claim 4 or 5, characterized in that: The method further comprises: In response to a call request to the policy interface, determining an enumeration value of the call request according to a parameter carried in the call request; Based on the enumeration value and the policy annotation, the corresponding policy class instance is obtained, and the preset method in the policy class instance is executed.
7. The method according to claim 1, characterized in that The policy component template includes at least: an enumeration class template, a policy annotation class template, a policy interface template, a policy implementation class template and a policy factory template.
8. A code automatic generation device, characterized in that: The device comprises: An acquisition module, configured to acquire configuration parameters input by a target user in response to a request for code generation initiated by the target user; A first generation module, used 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 generate at least one policy implementation class based on the enumeration value of the enumeration class and using the policy component template, wherein the policy implementation class is used to implement the policy interface; The integration module is used to integrate the enumeration class, the strategy interface and the strategy implementation class to form a strategy pattern code set.
9. An electronic device, comprising: one or more processors; a storage device for storing one or more computer programs, It is characterized in that when the one or more programs are executed by the one or more processors, the one or more processors execute the method according to any one of claims 1 to 7.
10. A computer-readable storage medium having executable instructions stored thereon, characterized in that: When the instruction is executed by a processor, the processor executes the method according to any one of claims 1 to 7.
11. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
State enumeration class generation method and device, server and storage medium
CN111399901A
Program development method and device
CN113778388A
Code generation method and device, equipment and storage medium
CN116880856A