Skip to content

Commit

Permalink
style: 添加spring javaformat
Browse files Browse the repository at this point in the history
  • Loading branch information
livk-cloud committed Nov 22, 2023
1 parent 21f6354 commit 38828d5
Show file tree
Hide file tree
Showing 727 changed files with 4,039 additions and 3,816 deletions.
1 change: 1 addition & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repositories {

dependencies {
implementation("org.springframework.boot:spring-boot-gradle-plugin:${libs.versions.spring.boot.get()}")
implementation("io.spring.javaformat:spring-javaformat-gradle-plugin:${libs.versions.spring.javaformat.get()}")
}

gradlePlugin {
Expand Down
2 changes: 2 additions & 0 deletions buildSrc/src/main/kotlin/com/livk/boot/ModulePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.livk.boot

import com.livk.boot.compile.CompileArgsPlugin
import io.spring.javaformat.gradle.SpringJavaFormatPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project

Expand All @@ -28,5 +29,6 @@ class ModulePlugin : Plugin<Project> {
override fun apply(project: Project) {
project.pluginManager.apply(CompileArgsPlugin::class.java)
project.pluginManager.apply(CorePlugin::class.java)
project.pluginManager.apply(SpringJavaFormatPlugin::class.java)
}
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jexl3 = "3.3"
mvel2 = "2.5.0.Final"
disruptor = "4.0.0"
spring-asciidoctor = "0.0.7"
spring-javaformat = "0.0.40"

[libraries]
spring-boot-admin-dependencies = { group = "de.codecentric", name = "spring-boot-admin-dependencies", version.ref = "spring-admin" }
Expand Down Expand Up @@ -116,3 +117,4 @@ spring-boot = { id = "org.springframework.boot", version.ref = "spring-boot" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
asciidoctor-jvm = { id = "org.asciidoctor.jvm.convert", version.ref = "asciidoctor-jvm" }
google-protobuf = { id = "com.google.protobuf", version.ref = "protobuf-plugins" }
spring-javaformat = { id = "io.spring.javaformat", version.ref = "spring-javaformat" }
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface SpringAutoService {

/**
* Annotation for automatic assembly, default org.springframework.boot.autoconfigure.AutoConfiguration
*
* Annotation for automatic assembly, default
* org.springframework.boot.autoconfigure.AutoConfiguration
* @return class
*/
Class<? extends Annotation> value() default Annotation.class;

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface SpringFactories {

/**
* 指定spring.factories文件生成接口
* 如当前类仅有一个接口,则可以自动推断
*
* 指定spring.factories文件生成接口 如当前类仅有一个接口,则可以自动推断
* @return class
*/
Class<?> value() default Void.class;

/**
* 将spring.factories文件调整至aot.factories
*
* @return the boolean
*/
boolean aot() default false;

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,11 @@ public SourceVersion getSupportedSourceVersion() {

@Override
public Set<String> getSupportedAnnotationTypes() {
return getSupportedAnnotation().stream()
.map(Class::getName)
.collect(Collectors.toSet());
return getSupportedAnnotation().stream().map(Class::getName).collect(Collectors.toSet());
}

/**
* Set supported annotations
*
* @return Set class
*/
protected abstract Set<Class<?>> getSupportedAnnotation();
Expand All @@ -104,7 +101,8 @@ public Set<String> getSupportedAnnotationTypes() {
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
generateConfigFiles();
} else {
}
else {
processAnnotations(annotations, roundEnv);
}
return false;
Expand All @@ -117,15 +115,13 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment

/**
* 处理注解
*
* @param annotations annotations
* @param roundEnv roundEnv
* @param roundEnv roundEnv
*/
protected abstract void processAnnotations(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv);

/**
* buffered reader.
*
* @param fileObject the file object
* @return the buffered reader
* @throws IOException the io exception
Expand All @@ -136,12 +132,12 @@ protected BufferedReader bufferedReader(FileObject fileObject) throws IOExceptio

/**
* buffered writer.
*
* @param fileObject the file object
* @return the buffered writer
* @throws IOException the io exception
*/
protected BufferedWriter bufferedWriter(FileObject fileObject) throws IOException {
return new BufferedWriter(fileObject.openWriter());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public class SpringAutoServiceProcessor extends CustomizeAbstractProcessor {

private static final String LOCATION = "META-INF/spring/%s.imports";

private final SetMultimap<String, String> importsMap = Multimaps.synchronizedSetMultimap(LinkedHashMultimap.create());
private final SetMultimap<String, String> importsMap = Multimaps
.synchronizedSetMultimap(LinkedHashMultimap.create());

@Override
protected Set<Class<?>> getSupportedAnnotation() {
Expand All @@ -71,11 +72,11 @@ protected void generateConfigFiles() {
Set<String> exitImports = this.read(resource);
Set<String> allImports = Stream.concat(exitImports.stream(), importsMap.get(providerInterface).stream())
.collect(Collectors.toSet());
FileObject fileObject =
filer.createResource(StandardLocation.CLASS_OUTPUT, "", resourceFile);
FileObject fileObject = filer.createResource(StandardLocation.CLASS_OUTPUT, "", resourceFile);

this.writeFile(allImports, fileObject);
} catch (IOException e) {
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
Expand All @@ -97,24 +98,21 @@ private TypeElement AutoConfigurationElement() {

/**
* 从文件读取配置
*
* @param fileObject 文件信息
* @return set className
*/
private Set<String> read(FileObject fileObject) {
try (BufferedReader reader = bufferedReader(fileObject)) {
return reader.lines()
.map(String::trim)
.collect(Collectors.toUnmodifiableSet());
} catch (Exception ignored) {
return reader.lines().map(String::trim).collect(Collectors.toUnmodifiableSet());
}
catch (Exception ignored) {
return Collections.emptySet();
}
}

/**
* 将配置信息写入到文件
*
* @param services 实现类信息
* @param services 实现类信息
* @param fileObject 文件信息
*/
private void writeFile(Collection<String> services, FileObject fileObject) throws IOException {
Expand All @@ -126,4 +124,5 @@ private void writeFile(Collection<String> services, FileObject fileObject) throw
writer.flush();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ public class SpringFactoriesProcessor extends CustomizeAbstractProcessor {

private static final String AOT_LOCATION = "META-INF/spring/aot.factories";

private final SetMultimap<String, String> springFactoriesMap = Multimaps.synchronizedSetMultimap(LinkedHashMultimap.create());
private final SetMultimap<String, String> springFactoriesMap = Multimaps
.synchronizedSetMultimap(LinkedHashMultimap.create());

private final SetMultimap<String, String> aotFactoriesMap = Multimaps.synchronizedSetMultimap(LinkedHashMultimap.create());
private final SetMultimap<String, String> aotFactoriesMap = Multimaps
.synchronizedSetMultimap(LinkedHashMultimap.create());

@Override
protected Set<Class<?>> getSupportedAnnotation() {
Expand All @@ -76,11 +78,11 @@ private void generateConfigFiles(Multimap<String, String> factoriesMap, String l
for (Map.Entry<String, String> entry : factoriesMap.entries()) {
allImportMap.put(entry.getKey(), entry.getValue());
}
FileObject fileObject =
filer.createResource(StandardLocation.CLASS_OUTPUT, "", location);
FileObject fileObject = filer.createResource(StandardLocation.CLASS_OUTPUT, "", location);

this.writeFile(allImportMap.asMap(), fileObject);
} catch (IOException e) {
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
Expand All @@ -99,7 +101,8 @@ protected void processAnnotations(Set<? extends TypeElement> annotations, RoundE
String serviceImpl = TypeElements.getBinaryName((TypeElement) element);
if (aot) {
aotFactoriesMap.put(provider, serviceImpl);
} else {
}
else {
springFactoriesMap.put(provider, serviceImpl);
}
}
Expand All @@ -120,7 +123,6 @@ private TypeElement fromInterface(Element element) {

/**
* 从文件读取某个接口的配置
*
* @param fileObject 文件信息
* @return set className
*/
Expand All @@ -135,18 +137,19 @@ private Multimap<String, String> read(FileObject fileObject) {
providers.putAll(factoryTypeName, Arrays.asList(factoryImplementationNames));
}
return providers;
} catch (Exception e) {
}
catch (Exception e) {
return LinkedHashMultimap.create();
}
}

/**
* 将配置信息写入到文件
*
* @param allImportMap 供应商接口及实现类信息
* @param fileObject 文件信息
* @param fileObject 文件信息
*/
private void writeFile(Map<String, ? extends Collection<String>> allImportMap, FileObject fileObject) throws IOException {
private void writeFile(Map<String, ? extends Collection<String>> allImportMap, FileObject fileObject)
throws IOException {
try (BufferedWriter writer = bufferedWriter(fileObject)) {
for (Map.Entry<String, ? extends Collection<String>> entry : allImportMap.entrySet()) {
String providerInterface = entry.getKey();
Expand All @@ -169,4 +172,5 @@ private void writeFile(Map<String, ? extends Collection<String>> allImportMap, F
writer.flush();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ class TypeElements {

/**
* Gets annotation attributes.
*
* @param <T> the type parameter
* @param element the element
* @param <T> the type parameter
* @param element the element
* @param targetClass the target class
* @param key the key
* @param key the key
* @return the annotation attributes
*/
public <T> Optional<TypeElement> getAnnotationAttributes(Element element, Class<T> targetClass, String key) {
Expand All @@ -51,21 +50,18 @@ public <T> Optional<TypeElement> getAnnotationAttributes(Element element, Class<
.entrySet()
.stream()
.filter(entry -> entry.getValue() != null)
.collect(Collectors.toMap(entry -> entry.getKey().getSimpleName().toString(),
Map.Entry::getValue));
return Optional.ofNullable(elementValues.get(key))
.map(annotationValue -> {
DeclaredType declaredType = (DeclaredType) annotationValue.getValue();
return (TypeElement) declaredType.asElement();
});
.collect(Collectors.toMap(entry -> entry.getKey().getSimpleName().toString(), Map.Entry::getValue));
return Optional.ofNullable(elementValues.get(key)).map(annotationValue -> {
DeclaredType declaredType = (DeclaredType) annotationValue.getValue();
return (TypeElement) declaredType.asElement();
});
}
}
throw new IllegalArgumentException(element + " not has annotation:" + targetClass);
}

/**
* Gets binary name.
*
* @param element the element
* @return the binary name
*/
Expand All @@ -84,4 +80,5 @@ private String getBinaryNameImpl(TypeElement element, String className) {
}
return getBinaryNameImpl((TypeElement) enclosingElement, enclosingElement.getSimpleName() + "$" + className);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface EnableAuto {

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
*/
@SpringAutoService(EnableAuto.class)
public class SpringAutoContext {

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public void testEnableAuto() {
private void compile(Class<?> type, String annotationName) {
SpringAutoServiceProcessor serviceProcessor = new SpringAutoServiceProcessor();
SourceFile sourceFile = SourceFile.forTestClass(type);
TestCompiler testCompiler = TestCompiler.forSystem()
.withProcessors(serviceProcessor)
.withSources(sourceFile);
TestCompiler testCompiler = TestCompiler.forSystem().withProcessors(serviceProcessor).withSources(sourceFile);
testCompiler.compile(compiled -> {
try {
Enumeration<URL> resources = compiled.getClassLoader()
Expand All @@ -61,11 +59,11 @@ private void compile(Class<?> type, String annotationName) {
configList.addAll(Arrays.stream(arr).map(String::trim).toList());
}
Assertions.assertTrue(configList.contains(type.getName()));
} catch (IOException e) {
}
catch (IOException e) {
throw new RuntimeException(e);
}
});
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
*/
@SpringAutoService
public class SpringContext {

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,16 @@ class SpringFactoriesProcessorTest {

@Test
public void test() {
compile(SpringFactoryServiceImpl.class, SpringFactoryService.class,
SpringFactoryServiceImpl.class.getName());
compile(SpringFactoryServiceImpl.class, SpringFactoryService.class, SpringFactoryServiceImpl.class.getName());
}

private void compile(Class<?> type, Class<?> factoryClass, String factoryClassImplName) {
SpringFactoriesProcessor serviceProcessor = new SpringFactoriesProcessor();
SourceFile sourceFile = SourceFile.forTestClass(type);
TestCompiler testCompiler = TestCompiler.forSystem()
.withProcessors(serviceProcessor)
.withSources(sourceFile);
TestCompiler testCompiler = TestCompiler.forSystem().withProcessors(serviceProcessor).withSources(sourceFile);
testCompiler.compile(compiled -> {
try {
Enumeration<URL> resources = compiled.getClassLoader()
.getResources("META-INF/spring.factories");
Enumeration<URL> resources = compiled.getClassLoader().getResources("META-INF/spring.factories");
Properties pro = new Properties();
for (URL url : Collections.list(resources)) {
InputStream inputStream = new UrlResource(url).getInputStream();
Expand All @@ -61,9 +57,11 @@ private void compile(Class<?> type, Class<?> factoryClass, String factoryClassIm
}
Assertions.assertTrue(pro.containsKey(factoryClass.getName()));
Assertions.assertEquals(factoryClassImplName, pro.get(factoryClass.getName()));
} catch (IOException e) {
}
catch (IOException e) {
throw new RuntimeException(e);
}
});
}

}
Loading

0 comments on commit 38828d5

Please sign in to comment.