mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
完善代码生成, 添加格式化
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -317,7 +317,11 @@
|
||||
<!-- <version>2.15.0</version>-->
|
||||
|
||||
<!-- </dependency>-->
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.googlejavaformat</groupId>
|
||||
<artifactId>google-java-format</artifactId>
|
||||
<version>1.18.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
|
||||
@@ -14,6 +14,8 @@ import cn.lihongjie.coal.base.mapper.CommonMapper;
|
||||
import cn.lihongjie.coal.base.service.BaseService;
|
||||
|
||||
import com.google.common.base.CaseFormat;
|
||||
import com.google.googlejavaformat.java.Formatter;
|
||||
import com.google.googlejavaformat.java.JavaFormatterOptions;
|
||||
import com.squareup.javapoet.*;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -38,12 +40,30 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
|
||||
import javax.lang.model.element.Modifier;
|
||||
|
||||
/**
|
||||
* google-java-format uses internal javac APIs for parsing Java source. The following JVM flags are
|
||||
* required when running on JDK 16 and newer, due to JEP 396: Strongly Encapsulate JDK Internals by
|
||||
* Default:
|
||||
*
|
||||
* <p>--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
|
||||
* --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
|
||||
* --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
|
||||
* --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
|
||||
* --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
|
||||
* --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
|
||||
*/
|
||||
public class Codegen {
|
||||
|
||||
public static final Path DIRECTORY = Path.of("src/main/java/");
|
||||
@@ -80,7 +100,7 @@ public class Codegen {
|
||||
.addAnnotation(Entity.class)
|
||||
.build();
|
||||
|
||||
JavaFile.builder(entityPackage, entity).build().writeTo(DIRECTORY);
|
||||
saveFile(entityPackage, entity);
|
||||
|
||||
// 生成dto
|
||||
TypeSpec dto =
|
||||
@@ -90,7 +110,7 @@ public class Codegen {
|
||||
.addAnnotation(Data.class)
|
||||
.build();
|
||||
|
||||
JavaFile.builder(dtoPackage, dto).build().writeTo(DIRECTORY);
|
||||
saveFile(dtoPackage, dto);
|
||||
|
||||
// 生成createDto
|
||||
TypeSpec createDto =
|
||||
@@ -100,7 +120,7 @@ public class Codegen {
|
||||
.addAnnotation(Data.class)
|
||||
.build();
|
||||
|
||||
JavaFile.builder(dtoPackage, createDto).build().writeTo(DIRECTORY);
|
||||
saveFile(dtoPackage, createDto);
|
||||
|
||||
// 生成updateDto
|
||||
TypeSpec updateDto =
|
||||
@@ -110,7 +130,7 @@ public class Codegen {
|
||||
.addAnnotation(Data.class)
|
||||
.build();
|
||||
|
||||
JavaFile.builder(dtoPackage, updateDto).build().writeTo(DIRECTORY);
|
||||
saveFile(dtoPackage, updateDto);
|
||||
|
||||
// 生成repository
|
||||
TypeSpec repository =
|
||||
@@ -123,7 +143,7 @@ public class Codegen {
|
||||
ClassName.get(entityPackage, entity.name)))
|
||||
.build();
|
||||
|
||||
JavaFile.builder(repoPackage, repository).build().writeTo(DIRECTORY);
|
||||
saveFile(repoPackage, repository);
|
||||
|
||||
// 生成mapper
|
||||
TypeSpec mapper =
|
||||
@@ -147,7 +167,7 @@ public class Codegen {
|
||||
ClassName.get(dtoPackage, updateDto.name)))
|
||||
.build();
|
||||
|
||||
JavaFile.builder(mapperPackage, mapper).build().writeTo(DIRECTORY);
|
||||
saveFile(mapperPackage, mapper);
|
||||
|
||||
// 生成service
|
||||
TypeSpec service =
|
||||
@@ -263,7 +283,7 @@ public class Codegen {
|
||||
.build())
|
||||
.build();
|
||||
|
||||
JavaFile.builder(servicePackage, service).build().writeTo(DIRECTORY);
|
||||
saveFile(servicePackage, service);
|
||||
|
||||
// 生成controller
|
||||
TypeSpec.Builder controllerBuilder =
|
||||
@@ -395,6 +415,39 @@ public class Codegen {
|
||||
ClassName.get(Sort.class))
|
||||
.build());
|
||||
|
||||
JavaFile.builder(controllerPackage, controllerBuilder.build()).build().writeTo(DIRECTORY);
|
||||
saveFile(controllerPackage, controllerBuilder.build());
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
private static void saveFile(String pkg, TypeSpec entity) throws IOException {
|
||||
|
||||
JavaFile javaFile = JavaFile.builder(pkg, entity).build();
|
||||
|
||||
Path outputDirectory = DIRECTORY;
|
||||
if (!javaFile.packageName.isEmpty()) {
|
||||
for (String packageComponent : javaFile.packageName.split("\\.")) {
|
||||
outputDirectory = outputDirectory.resolve(packageComponent);
|
||||
}
|
||||
Files.createDirectories(outputDirectory);
|
||||
}
|
||||
|
||||
Path outputPath = outputDirectory.resolve(javaFile.typeSpec.name + ".java");
|
||||
try (Writer writer =
|
||||
new OutputStreamWriter(Files.newOutputStream(outputPath), StandardCharsets.UTF_8)) {
|
||||
|
||||
StringWriter out = new StringWriter();
|
||||
javaFile.writeTo(out);
|
||||
|
||||
String formattedSource =
|
||||
new Formatter(
|
||||
JavaFormatterOptions.builder()
|
||||
.style(JavaFormatterOptions.Style.AOSP)
|
||||
.formatJavadoc(true)
|
||||
.reorderModifiers(true)
|
||||
.build())
|
||||
.formatSource(out.toString());
|
||||
|
||||
writer.write(formattedSource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user