Free your hands! ChatGPT helps to write JAVA framework! | JD Cloud technical team

Dear Javaers, in the usual coding process, have you ever thought about writing a Java framework to improve development efficiency? But either I feel at a loss when writing a framework, and I don't know where to start. Either you don't understand the technical details of a certain function after you have an idea, and you can't realize it if you have an idea. If you encounter these problems, after reading this article, you can also use ChatGPT to write a simple JAVA framework.

clear idea

First of all, you need to clarify what problem your framework is going to solve and what features it has. This will help ChatGPT better understand your needs.

For example: I found that there are too many descriptions of enumeration classes that need to be returned to the front-end in a requirement for front-end and back-end separation. The traditional way is that the backend maps each enumeration value into a description based on the mapping of the enumeration class and returns it to the front end. But this time there are too many enumeration descriptions that need to be returned by the front end. This made me and my little friends feel helpless. So I wondered if I could write an annotation to automatically scan these enumeration classes for us, then generate a key and description map, and finally put it in the container?

Solve the problem : Solve the need to manually write code to map the description of the enumeration key to the front-end during the separation process of the front-end and back-end.

Features : The framework has an annotation, which has three fields name, key, and desc, which are decorated on the enumeration class. The framework needs to provide an interface for externally obtaining the enumeration map.

Communicate with ChatGPT

Start ChatGPT and tell it your vision and needs. It can generate initial code for you, suggest structures, and even handle some logic for you.

After thinking about the problems to be solved by the framework and the characteristics of the framework, the next step is to summarize the problems and characteristics to be solved by the framework into a text and send it to ChatGPT

Chatgpt probably generated a demo version of the framework according to my needs. It can be seen that there are still some differences from what I envisioned. I think it is to put the enumeration on the class. Then the enumerated attributes are name, key, and desc.

name: represents the key of the enumeration map in the outermost map.

key: represents the key or value field of the enumeration.

desc: represents the description field of the enumeration.



 



In-depth interaction

Have a deeper conversation with ChatGPT and ask for its advice on code optimization, exception handling, and more. It can help you find better solutions.

Next, you need to communicate further with ChatGPT to let ChatGPT optimize the previously generated code.

EnumInfo annotation before optimization:

Optimized:



Gradually improve

Guide chatgpt to improve the framework step by step

With the help of ChatGPT, gradually improve your framework. Get your hands dirty with coding and discuss every detail with ChatGPT.

Finally, you can communicate with ChatGPT step by step and let it help you build a complete framework.

In the end, after talking and iterating with ChatGPT slowly, the core classes of the framework are generated. The iteration process is omitted because it is too long.

Framework core class description

After ChatGPT gave the core code, I refer to the Spring module to design the final first version of the framework class as follows:

PackageScanner: A utility class to scan a given package for classes with the specified annotation.

PropertiesUtils: A tool class that provides utility methods for manipulating property files.

EnumInfo: It is used to mark the annotation of the enumeration class, specify the name of the enumeration item, key field and desc field information. By adding this annotation on the enumeration class, an index map can be established for the enumeration item, and the field name used to find the key and desc can be specified.

EnumContext: Enumeration context class, used to manage enumeration definition information and provide methods to obtain enumeration information.

EnumContextFactory: Enumeration context factory class, used to create and obtain singleton enumeration context objects.

EnumDefinition: Represents the class defined by the enumeration, which is used to store the information of the enumeration class.

EnumDefinitionRegistry: Enumeration definition registration interface, used to register, query and manage enumeration definitions.

DefaultEnumFactory: The default enumeration definition factory class, which implements the EnumDefinitionRegistry interface.

See this section on writing a framework using ChatGPT already done. Large pieces can use chatgpt to develop their own JAVA framework. But in order to actually apply the framework to production, some finishing processes need to be done.

Framework usage test

In the ChatGPT communication, after the framework is written, the framework needs to be applied to the actual project.

In the process of separating the front-end and back-end of the author's business system management end, the R&D staff found that there are many enumeration descriptions corresponding to enumeration classes that need to be returned to the front-end.

1) At the beginning, it was envisaged that each enumeration class would write code for the front-end package to return text. However, because the author has too many configuration items in the business system, it is too troublesome to write code for each configuration item.

2) So the R&D staff wondered whether they could use a unified interface to return the description corresponding to the enumeration class to the front end. The front end only needs to input the name of the enumeration class to obtain the mapping relationship between the corresponding enumeration key and the description.

So we created an interface and defined a Map object to return the corresponding relationship between the key of the enumeration class and the description to the front end. However, because the channel configuration of the author's business system is still too much. In this way we need to initialize the Map. The initialization Map code is as follows:

public HashMap<String, Map<Integer, String>> initEnumMap() {
    enumMap = new HashMap<>();
    enumMap.put("前端获取枚举map的key", XXXEnum.getEnumMap());
    enumMap.put("前端获取枚举map的key", XXXEnum.getEnumMap());
    enumMap.put("前端获取枚举map的key", XXXEnum.getEnumMap());
    ...
    return enumMap;
}

It can be seen that each new enumeration class is added. We all need to put the mapping relationship into the map in the static code block. And the enumeration class needs to add a mapping relationship method for obtaining key and description. This is still too much trouble. And the code of this class must be changed for subsequent new mapping relationships.

Can the steps of map initialization and enumeration class creation steps be omitted?

3) So we imagine defining an annotation. Classes marked with this annotation are scanned by the framework. And generate a method to obtain the mapping relationship between the enumeration key and the description. Finally complete the process of initializing the Map. Only the method of obtaining the total enumeration Map is provided externally. Users don't need to care about how the Map is constructed. After using this framework, the code of the interface of the author's business system is as follows:

/**
 * 获取枚举
*
 * @param enumKey 枚举key
 * @return 返回值 Map<Integer,String>;code,描述
*/
@RequestMapping("/getEnum")
public Result<Map<String, Map<String, String>>> getEnum(String enumKey) {
    try {
        // 获取枚举上下文对象
        EnumContext enumContext = EnumContextFactory.getEnumContext();
        // 获取枚举map
        newEnumMap = enumContext.getEnumIndexMap();
        // buid映射从ducc中获取,所以需要手动设置
        newEnumMap.put(BUID.getKey(), getBuIdMap());
    } catch (Exception e) {
        log.error("获取枚举map出错!enumKey:{}", enumKey, e);
        return Result.createFail(e.getMessage());
    }
    // 如果枚举key为空则返回全部
    if (StringUtils.isBlank(enumKey)) {
        return Result.createWithSuc(newEnumMap);
    }
    // 如果枚举key不为空则返回指定值
    Map<String, Map<String, String>> resultMap = new HashMap<>();
    resultMap.put(enumKey, newEnumMap.get(enumKey));
    return Result.createWithSuc(resultMap);
}

4) The annotation class code is as follows:

Here is an example of a test enumeration class

@EnumInfo(name = "StatusEnum", key = "code", desc = "description")
public enum StatusEnum {
    SUCCESS(200, "Success"),
    ERROR(500, "Error");

    private final int code;
    private final String description;

    StatusEnum(int code, String description) {
        this.code = code;
        this.description = description;
    }

    public int getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }
}

To add an enumeration class in the future, you only need to mark @EnumInfo(name = "StatusEnum", key = "code", desc = "description"). Just specify the name of the enumeration class, the key field name and the description field name. The mapping relationship between the key of the enumeration and the description can be returned to the front end without modifying the code of the interface. Greatly reduced R&D joint debugging time and test regression time.

Framework performance pressure test

When the framework is applied to actual production projects, it is necessary to fully test and verify the framework assisted by ChatGPT. At the same time, it is also necessary to test the performance of the framework to know the bottleneck of the framework. Common interface pressure testing tools include LoadRunner and Apache JMeter . You can choose a pressure measurement tool to perform pressure measurement.

The author applied the framework to the project and exposed an interface to the outside world. Under the 4C4G machine configuration, the single machine can support up to 1000 QPS. Under 1000QPS, the CPU usage rate of the single machine reaches 30% , the system load is close to 0.9 , and the memory usage rate and pressure There was no significant change before the test.

 

Author: JD Retail Wang Fengxi

Source: Reprinted by JD Cloud developer community, please indicate the source

The country's first IDE that supports multi-environment development——CEC-IDE Microsoft has integrated Python into Excel, and Uncle Gui participated in the framework formulation. Chinese programmers refused to write gambling programs and were pulled out 14 teeth, with 88% body damage . Podman Desktop, an open-source imitation Song font, breaks through 500,000 downloads. Automatically skips opening screen advertisements. The application "Li Tiao Tiao" stops updating indefinitely. There is a remote code execution vulnerability Xiaomi filed mios.cn website domain name
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4090830/blog/10102758