其他
用 ChatGPT 写代码,效率杠杠的!
关注 AI 实战,开启智能生活!
推荐阅读:
1、单测用例生成
/**
* 配置文件数据服务
*/
public static ISepI18nDataService dataService;
/**
* 构建数据服务
*
* @param dataType
* @return
*/
public static ISepI18nDataService buildDataService(SepI18nDataType dataType, SepI18nLanguageType languageType) {
//按照数据类型判断
switch (dataType) {
//从ducc中构建多语言服务
case DUCC:
return getInstance(languageType, x -> new SepI18nDuccDataService(x));
//从mysql中构建多语言服务
case MYSQL:
return getInstance(languageType, x -> new SepI18nMysqlDataService(x));
//从本地配置中构建多语言服务
case CONFIG:
return getInstance(languageType, x -> new SepI18nConfigDataService(x));
//空数据服务,兜底逻辑
default:
return getInstance(languageType, x -> new SepI18nEmptyDataService(x));
}
}
/**
* 高并发下实例创建管控
*
* @param languageType
* @param callback
* @return
*/
private static ISepI18nDataService getInstance(SepI18nLanguageType languageType, Function<SepI18nLanguageType, ISepI18nDataService> callback) {
if (dataService == null) {
synchronized (SepI18nDataFactory.class) {
if (dataService == null) {
dataService = callback.apply(languageType);
}
}
}
return dataService;
}
}
ChatGPT问题如下(要求:为类中的所有方法创建单测并包含所有的场景):
public class SepI18nDataFactory {
/** * 配置文件数据服务 */
public static SepI18nConfigDataService configDataService;
/** * 数据库数据服务 */
public static SepI18nMysqlDataService mysqlDataService;
/** * ducc数据服务 */
public static SepI18nDuccDataService duccDataService;
/** * 空数据服务 - 兜底服务 */
public static SepI18nEmptyDataService emptyDataService;
/** * 构建数据服务
* @param dataType
* @return
*/
public static ISepI18nDataService buildDataService(SepI18nDataType dataType, SepI18nLanguageType languageType) {
//区域设定 Locale locale = null;
//按照数据类型判断
switch (dataType) {
//从ducc中构建多语言服务
case DUCC:
if (duccDataService == null) {
duccDataService = new SepI18nDuccDataService(languageType);
}
return duccDataService;
//从mysql中构建多语言服务
case MYSQL:
if (mysqlDataService == null) {
mysqlDataService = new SepI18nMysqlDataService(languageType);
}
return mysqlDataService;
//从本地配置中构建多语言服务
case CONFIG:
if (configDataService == null) {
configDataService = new SepI18nConfigDataService(languageType);
} return configDataService;
//空数据服务,兜底逻辑
default:
if (emptyDataService == null) {
emptyDataService = new SepI18nEmptyDataService(languageType);
}
return emptyDataService;
}
}
}
ChatGPT回答如下:
public class SepI18nDataFactoryTest {
@Test
public void testBuildDataService_DUCC() {
SepI18nDataType dataType = SepI18nDataType.DUCC;
SepI18nLanguageType languageType = SepI18nLanguageType.EN;
ISepI18nDataService dataService = SepI18nDataFactory.buildDataService(dataType, languageType);
assertEquals(SepI18nDuccDataService.class, dataService.getClass());
}
@Test
public void testBuildDataService_MYSQL() {
SepI18nDataType dataType = SepI18nDataType.MYSQL;
SepI18nLanguageType languageType = SepI18nLanguageType.EN;
ISepI18nDataService dataService = SepI18nDataFactory.buildDataService(dataType, languageType);
assertEquals(SepI18nMysqlDataService.class, dataService.getClass());
}
@Test
public void testBuildDataService_CONFIG() {
SepI18nDataType dataType = SepI18nDataType.CONFIG;
SepI18nLanguageType languageType = SepI18nLanguageType.EN;
ISepI18nDataService dataService = SepI18nDataFactory.buildDataService(dataType, languageType);
assertEquals(SepI18nConfigDataService.class, dataService.getClass());
}
@Test
public void testBuildDataService_Default() {
SepI18nDataType dataType = SepI18nDataType.OTHER;
SepI18nLanguageType languageType = SepI18nLanguageType.EN;
ISepI18nDataService dataService = SepI18nDataFactory.buildDataService(dataType, languageType);
assertEquals(SepI18nEmptyDataService.class, dataService.getClass());}}
2、编写小工具
ChatGPT回答如下:
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class EnumFinder extends Application {
private TextField directoryPathTextField;
private TextField enumClassNameTextField;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
directoryPathTextField = new TextField();
Button directoryPathButton = new Button("Select Directory");
directoryPathButton.setOnAction(e -> {
DirectoryChooser directoryChooser = new DirectoryChooser();
File selectedDirectory = directoryChooser.showDialog(primaryStage);
if (selectedDirectory != null) {
directoryPathTextField.setText(selectedDirectory.getAbsolutePath());
}
});
Button findEnumButton = new Button("Find Enum");
findEnumButton.setOnAction(e -> {
String directoryPath = directoryPathTextField.getText();
try (Stream<Path> paths =Files.walk(Paths.get(directoryPath))){
paths.filter(Files::isRegularFile).filter(p -> p.toString().endsWith(".class")).filter(p ->isEnumClass(p)).forEach(p -> enumClassNameTextField.setText(p.getFileName().toString().replace(".class","")));}catch(IOException ex){
ex.printStackTrace();}});
enumClassNameTextField =newTextField();
enumClassNameTextField.setEditable(false);HBox hBox =newHBox(directoryPathTextField, directoryPathButton, findEnumButton, enumClassNameTextField);Scene scene =newScene(hBox);
primaryStage.setScene(scene);
primaryStage.show();}privatebooleanisEnumClass(Path path){try{Class clazz =Class.forName(path.toString().replace(".class",""));return clazz.isEnum();}catch(ClassNotFoundException e){
e.printStackTrace();returnfalse;}}}
3. 修复代码bug
Sample1:高并发bug
/**
* 配置文件数据服务
*/
public static ISepI18nDataService dataService;
/**
* 构建数据服务
*
* @param dataType
* @return
*/
public static ISepI18nDataService buildDataService(SepI18nDataType dataType, SepI18nLanguageType languageType) {
//按照数据类型判断
switch (dataType) {
//从ducc中构建多语言服务
case DUCC:
return getInstance(languageType, x -> new SepI18nDuccDataService(x));
//从mysql中构建多语言服务
case MYSQL:
return getInstance(languageType, x -> new SepI18nMysqlDataService(x));
//从本地配置中构建多语言服务
case CONFIG:
return getInstance(languageType, x -> new SepI18nConfigDataService(x));
//空数据服务,兜底逻辑
default:
return getInstance(languageType, x -> new SepI18nEmptyDataService(x));
}
}
/**
* 高并发下实例创建管控
*
* @param languageType
* @param callback
* @return
*/
private static ISepI18nDataService getInstance(SepI18nLanguageType languageType, Function<SepI18nLanguageType, ISepI18nDataService> callback) {
if (dataService == null) {
dataService = callback.apply(languageType);
}
return dataService;
}
}
ChatGPT训练如下(指出代码在高并发下工作不太正常,让其修复):
public class SepI18nDataFactory {
/** 配置文件数据服务 */
public static ISepI18nDataService dataService;
/** 构建数据服务
* @param dataType
* @return
*/
public static ISepI18nDataService buildDataService(SepI18nDataType dataType, SepI18nLanguageType languageType) {
//按照数据类型判断
switch (dataType) {
//从ducc中构建多语言服务 case DUCC: return getInstance(languageType, x -> new SepI18nDuccDataService(x));
//从mysql中构建多语言服务 case MYSQL: return getInstance(languageType, x -> new SepI18nMysqlDataService(x));
//从本地配置中构建多语言服务 case CONFIG: return getInstance(languageType, x -> new SepI18nConfigDataService(x));
//空数据服务,兜底逻辑 default: return getInstance(languageType, x -> new SepI18nEmptyDataService(x));
}
}
/** 高并发下实例创建管控
* @param languageType
* @param callback
* @return
*/
private static ISepI18nDataService getInstance(SepI18nLanguageType languageType, Function callback) {
if (dataService == null) {
synchronized (SepI18nDataFactory.class) {
if (dataService == null) {
dataService = callback.apply(languageType);
}
}
}
return dataService;
}
}
ChatGPT回答如下:
Sample2: 自定义锁bug
@Override
protected boolean tryAcquire(int arg) {
if (compareAndSetState(0, 1)) {
setExclusiveOwnerThread(Thread.currentThread());
return true;
}
return false;
}
@Override
protected boolean tryRelease(int arg) {
if (getState() == 0) {
throw new IllegalMonitorStateException();
}
setExclusiveOwnerThread(null);
setState(0);
return true;
}
public void lock() {
acquire(1);
}
public void unlock() {
release(1);
}
}
ChatGPT问题如下(大意是帮忙看下锁写的有没有问题):
class SepSync extends AbstractQueuedSynchronizer {
@Override protected boolean tryAcquire(int arg) {
if (compareAndSetState(0, 1)) {
setExclusiveOwnerThread(Thread.currentThread());
return true;
}
return false;
}
@Override
protected boolean tryRelease(int arg) {
if (getState() == 0) {
throw new IllegalMonitorStateException();
}
setExclusiveOwnerThread(null);
setState(0);
return true;
}
public void lock() {
acquire(1);
}
public void unlock() {
release(1);
}
}
ChatGPT回答如下:
To fix this bug, the tryRelease() method should be modified to check if the current thread is the exclusive owner thread before releasing the lock. The modified code should look like this:
@Override protected boolean tryRelease(int arg){
if(Thread.currentThread() != getExclusiveOwnerThread()){
throw new IllegalMonitorStateException();
}
setExclusiveOwnerThread(null);
setState(0);
return true;
}