Polishing
This commit is contained in:
@@ -73,8 +73,7 @@ public final class GeneratedClass {
|
||||
TypeSpec.Builder typeSpecBuilder = TypeSpec.classBuilder(this.name);
|
||||
this.typeSpecCustomizer.accept(typeSpecBuilder);
|
||||
this.methods.doWithMethodSpecs(typeSpecBuilder::addMethod);
|
||||
return JavaFile.builder(this.name.packageName(), typeSpecBuilder.build())
|
||||
.build();
|
||||
return JavaFile.builder(this.name.packageName(), typeSpecBuilder.build()).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -142,8 +142,7 @@ public class GeneratedClasses {
|
||||
* @param typeSpecCustomizer a customizer for the {@link TypeSpec.Builder}
|
||||
* @return a {@link GeneratedClass} instance
|
||||
*/
|
||||
public GeneratedClass getOrGenerate(String id,
|
||||
Consumer<TypeSpec.Builder> typeSpecCustomizer) {
|
||||
public GeneratedClass getOrGenerate(String id, Consumer<TypeSpec.Builder> typeSpecCustomizer) {
|
||||
Assert.hasLength(id, "'id' must not be empty");
|
||||
Assert.notNull(typeSpecCustomizer, "'typeSpecCustomizer' must not be null");
|
||||
Owner owner = new Owner(id, GeneratedClasses.this.classNameGenerator
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.springframework.util.function.ThrowingConsumer;
|
||||
|
||||
/**
|
||||
* Interface that can be used to add {@link Kind#SOURCE source},
|
||||
* {@link Kind#RESOURCE resource} or {@link Kind#CLASS class} files generated
|
||||
* {@link Kind#RESOURCE resource}, or {@link Kind#CLASS class} files generated
|
||||
* during ahead-of-time processing. Source and resource files are written using
|
||||
* UTF-8 encoding.
|
||||
*
|
||||
@@ -191,14 +191,14 @@ public interface GeneratedFiles {
|
||||
SOURCE,
|
||||
|
||||
/**
|
||||
* A resource file that should be directly added to final application.
|
||||
* A resource file that should be directly added to the final application.
|
||||
* For example, a {@code .properties} file.
|
||||
*/
|
||||
RESOURCE,
|
||||
|
||||
/**
|
||||
* A class file containing bytecode. For example, the result of a proxy
|
||||
* generated using cglib.
|
||||
* generated using CGLIB.
|
||||
*/
|
||||
CLASS
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ public final class GeneratedMethod {
|
||||
|
||||
/**
|
||||
* Create a new {@link GeneratedMethod} instance with the given name. This
|
||||
* constructor is package-private since names should only be generated via a
|
||||
* constructor is package-private since names should only be generated via
|
||||
* {@link GeneratedMethods}.
|
||||
* @param name the generated name
|
||||
*/
|
||||
@@ -66,7 +66,7 @@ public final class GeneratedMethod {
|
||||
*/
|
||||
public MethodSpec getSpec() {
|
||||
Assert.state(this.spec != null,
|
||||
() -> String.format("Method '%s' has no method spec defined", this.name));
|
||||
() -> "Method '%s' has no method spec defined".formatted(this.name));
|
||||
return this.spec;
|
||||
}
|
||||
|
||||
@@ -86,8 +86,8 @@ public final class GeneratedMethod {
|
||||
}
|
||||
|
||||
private void assertNameHasNotBeenChanged(MethodSpec spec) {
|
||||
Assert.isTrue(this.name.equals(spec.name), () -> String
|
||||
.format("'spec' must use the generated name \"%s\"", this.name));
|
||||
Assert.isTrue(this.name.equals(spec.name),
|
||||
() -> "'spec' must use the generated name '%s'".formatted(this.name));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -45,8 +45,7 @@ public class InMemoryGeneratedFiles implements GeneratedFiles {
|
||||
Assert.notNull(content, "'content' must not be null");
|
||||
Map<String, InputStreamSource> paths = this.files.computeIfAbsent(kind,
|
||||
key -> new LinkedHashMap<>());
|
||||
Assert.state(!paths.containsKey(path),
|
||||
() -> "Path '" + path + "' already in use");
|
||||
Assert.state(!paths.containsKey(path), () -> "Path '" + path + "' already in use");
|
||||
paths.put(path, content);
|
||||
}
|
||||
|
||||
@@ -57,8 +56,7 @@ public class InMemoryGeneratedFiles implements GeneratedFiles {
|
||||
*/
|
||||
public Map<String, InputStreamSource> getGeneratedFiles(Kind kind) {
|
||||
Assert.notNull(kind, "'kind' must not be null");
|
||||
return Collections
|
||||
.unmodifiableMap(this.files.getOrDefault(kind, Collections.emptyMap()));
|
||||
return Collections.unmodifiableMap(this.files.getOrDefault(kind, Collections.emptyMap()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,8 +70,7 @@ public class InMemoryGeneratedFiles implements GeneratedFiles {
|
||||
public String getGeneratedFileContent(Kind kind, String path) throws IOException {
|
||||
InputStreamSource source = getGeneratedFile(kind, path);
|
||||
if (source != null) {
|
||||
return new String(source.getInputStream().readAllBytes(),
|
||||
StandardCharsets.UTF_8);
|
||||
return new String(source.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -95,8 +95,7 @@ public class MethodNameGenerator {
|
||||
public static String join(Object... parts) {
|
||||
Stream<String> capitalizedPartNames = Arrays.stream(parts)
|
||||
.map(MethodNameGenerator::getPartName).map(StringUtils::capitalize);
|
||||
return StringUtils
|
||||
.uncapitalize(capitalizedPartNames.collect(Collectors.joining()));
|
||||
return StringUtils.uncapitalize(capitalizedPartNames.collect(Collectors.joining()));
|
||||
}
|
||||
|
||||
private static String getPartName(@Nullable Object part) {
|
||||
|
||||
@@ -144,15 +144,14 @@ public final class MethodReference {
|
||||
*/
|
||||
public CodeBlock toCodeBlock(@Nullable String instanceVariable) {
|
||||
return switch (this.kind) {
|
||||
case INSTANCE -> toCodeBlockForInstance(instanceVariable);
|
||||
case STATIC -> toCodeBlockForStatic(instanceVariable);
|
||||
case INSTANCE -> toCodeBlockForInstance(instanceVariable);
|
||||
case STATIC -> toCodeBlockForStatic(instanceVariable);
|
||||
};
|
||||
}
|
||||
|
||||
private CodeBlock toCodeBlockForInstance(@Nullable String instanceVariable) {
|
||||
instanceVariable = (instanceVariable != null) ? instanceVariable : "this";
|
||||
return CodeBlock.of("$L::$L", instanceVariable, this.methodName);
|
||||
|
||||
}
|
||||
|
||||
private CodeBlock toCodeBlockForStatic(@Nullable String instanceVariable) {
|
||||
@@ -180,8 +179,8 @@ public final class MethodReference {
|
||||
CodeBlock... arguments) {
|
||||
|
||||
return switch (this.kind) {
|
||||
case INSTANCE -> toInvokeCodeBlockForInstance(instanceVariable, arguments);
|
||||
case STATIC -> toInvokeCodeBlockForStatic(instanceVariable, arguments);
|
||||
case INSTANCE -> toInvokeCodeBlockForInstance(instanceVariable, arguments);
|
||||
case STATIC -> toInvokeCodeBlockForStatic(instanceVariable, arguments);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -225,9 +224,9 @@ public final class MethodReference {
|
||||
@Override
|
||||
public String toString() {
|
||||
return switch (this.kind) {
|
||||
case INSTANCE -> ((this.declaringClass != null) ? "<" + this.declaringClass + ">"
|
||||
: "<instance>") + "::" + this.methodName;
|
||||
case STATIC -> this.declaringClass + "::" + this.methodName;
|
||||
case INSTANCE -> ((this.declaringClass != null) ? "<" + this.declaringClass + ">"
|
||||
: "<instance>") + "::" + this.methodName;
|
||||
case STATIC -> this.declaringClass + "::" + this.methodName;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ class GeneratedMethodTests {
|
||||
void getSpecReturnsSpec() {
|
||||
GeneratedMethod method = new GeneratedMethod(NAME);
|
||||
method.using(builder -> builder.addJavadoc("Test"));
|
||||
assertThat(method.getSpec().javadoc.toString()).contains("Test");
|
||||
assertThat(method.getSpec().javadoc).asString().contains("Test");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -57,7 +57,7 @@ class GeneratedMethodTests {
|
||||
void usingAddsSpec() {
|
||||
GeneratedMethod method = new GeneratedMethod(NAME);
|
||||
method.using(builder -> builder.addModifiers(Modifier.PUBLIC));
|
||||
assertThat(method.getSpec().toString())
|
||||
assertThat(method.getSpec()).asString()
|
||||
.isEqualToIgnoringNewLines("public void spring() {}");
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ class GeneratedMethodTests {
|
||||
GeneratedMethod method = new GeneratedMethod(NAME);
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> method.using(builder -> builder.setName("badname")))
|
||||
.withMessage("'spec' must use the generated name \"spring\"");
|
||||
.withMessage("'spec' must use the generated name 'spring'");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user