Browse Source

Polishing

pull/28427/head
Sam Brannen 3 years ago
parent
commit
8627bef8d9
  1. 8
      spring-core/src/main/java/org/springframework/aot/generate/FileSystemGeneratedFiles.java
  2. 28
      spring-core/src/main/java/org/springframework/util/ObjectUtils.java
  3. 29
      spring-core/src/test/java/org/springframework/aot/generate/FileSystemGeneratedFilesTests.java

8
spring-core/src/main/java/org/springframework/aot/generate/FileSystemGeneratedFiles.java

@ -73,9 +73,9 @@ public class FileSystemGeneratedFiles implements GeneratedFiles { @@ -73,9 +73,9 @@ public class FileSystemGeneratedFiles implements GeneratedFiles {
private static Function<Kind, Path> conventionRoots(Path root) {
Assert.notNull(root, "'root' must not be null");
return kind -> switch (kind) {
case SOURCE -> root.resolve("sources");
case RESOURCE -> root.resolve("resources");
case CLASS -> root.resolve("classes");
case SOURCE -> root.resolve("sources");
case RESOURCE -> root.resolve("resources");
case CLASS -> root.resolve("classes");
};
}
@ -83,7 +83,7 @@ public class FileSystemGeneratedFiles implements GeneratedFiles { @@ -83,7 +83,7 @@ public class FileSystemGeneratedFiles implements GeneratedFiles {
public void addFile(Kind kind, String path, InputStreamSource content) {
Assert.notNull(kind, "'kind' must not be null");
Assert.hasLength(path, "'path' must not be empty");
Assert.notNull(content, "'kind' must not be null");
Assert.notNull(content, "'content' must not be null");
Path root = this.roots.apply(kind).toAbsolutePath().normalize();
Path relativePath = root.resolve(path).toAbsolutePath().normalize();
Assert.isTrue(relativePath.startsWith(root), () -> "'path' must be relative");

28
spring-core/src/main/java/org/springframework/util/ObjectUtils.java

@ -250,33 +250,35 @@ public abstract class ObjectUtils { @@ -250,33 +250,35 @@ public abstract class ObjectUtils {
* @return the new array (of the same component type; never {@code null})
*/
public static <A, O extends A> A[] addObjectToArray(@Nullable A[] array, @Nullable O obj) {
return addObjectToArray(array, obj, (array != null) ? array.length : 0);
return addObjectToArray(array, obj, (array != null ? array.length : 0));
}
/**
* Append the given object to the given array, returning a new array
* consisting of the input array contents plus the given object.
* @param array the array to append to (can be {@code null})
* Add the given object to the given array at the specified position, returning
* a new array consisting of the input array contents plus the given object.
* @param array the array to add to (can be {@code null})
* @param obj the object to append
* @param position the position at which to add the object
* @return the new array (of the same component type; never {@code null})
* @since 6.0
*/
public static <A, O extends A> A[] addObjectToArray(@Nullable A[] array, @Nullable O obj, int position) {
Class<?> compType = Object.class;
Class<?> componentType = Object.class;
if (array != null) {
compType = array.getClass().getComponentType();
componentType = array.getClass().getComponentType();
}
else if (obj != null) {
compType = obj.getClass();
componentType = obj.getClass();
}
int newArrLength = (array != null ? array.length + 1 : 1);
int newArrayLength = (array != null ? array.length + 1 : 1);
@SuppressWarnings("unchecked")
A[] newArr = (A[]) Array.newInstance(compType, newArrLength);
A[] newArray = (A[]) Array.newInstance(componentType, newArrayLength);
if (array != null) {
System.arraycopy(array, 0, newArr, 0, position);
System.arraycopy(array, position, newArr, position + 1, array.length - position);
System.arraycopy(array, 0, newArray, 0, position);
System.arraycopy(array, position, newArray, position + 1, array.length - position);
}
newArr[position] = obj;
return newArr;
newArray[position] = obj;
return newArray;
}
/**

29
spring-core/src/test/java/org/springframework/aot/generate/FileSystemGeneratedFilesTests.java

@ -46,12 +46,9 @@ class FileSystemGeneratedFilesTests { @@ -46,12 +46,9 @@ class FileSystemGeneratedFilesTests {
generatedFiles.addResourceFile("META-INF/test", "test");
generatedFiles.addClassFile("com/example/TestProxy.class",
new ByteArrayResource("!".getBytes(StandardCharsets.UTF_8)));
assertThat(this.root.resolve("sources/com/example/Test.java")).content()
.isEqualTo("{}");
assertThat(this.root.resolve("resources/META-INF/test")).content()
.isEqualTo("test");
assertThat(this.root.resolve("classes/com/example/TestProxy.class")).content()
.isEqualTo("!");
assertThat(this.root.resolve("sources/com/example/Test.java")).content().isEqualTo("{}");
assertThat(this.root.resolve("resources/META-INF/test")).content().isEqualTo("test");
assertThat(this.root.resolve("classes/com/example/TestProxy.class")).content().isEqualTo("!");
}
@Test
@ -62,12 +59,9 @@ class FileSystemGeneratedFilesTests { @@ -62,12 +59,9 @@ class FileSystemGeneratedFilesTests {
generatedFiles.addResourceFile("META-INF/test", "test");
generatedFiles.addClassFile("com/example/TestProxy.class",
new ByteArrayResource("!".getBytes(StandardCharsets.UTF_8)));
assertThat(this.root.resolve("the-SOURCE/com/example/Test.java")).content()
.isEqualTo("{}");
assertThat(this.root.resolve("the-RESOURCE/META-INF/test")).content()
.isEqualTo("test");
assertThat(this.root.resolve("the-CLASS/com/example/TestProxy.class")).content()
.isEqualTo("!");
assertThat(this.root.resolve("the-SOURCE/com/example/Test.java")).content().isEqualTo("{}");
assertThat(this.root.resolve("the-RESOURCE/META-INF/test")).content().isEqualTo("test");
assertThat(this.root.resolve("the-CLASS/com/example/TestProxy.class")).content().isEqualTo("!");
}
@Test
@ -80,17 +74,15 @@ class FileSystemGeneratedFilesTests { @@ -80,17 +74,15 @@ class FileSystemGeneratedFilesTests {
@Test
void createWhenRootsIsNullThrowsException() {
assertThatIllegalArgumentException()
.isThrownBy(
() -> new FileSystemGeneratedFiles((Function<Kind, Path>) null))
.isThrownBy(() -> new FileSystemGeneratedFiles((Function<Kind, Path>) null))
.withMessage("'roots' must not be null");
}
@Test
void createWhenRootsResultsInNullThrowsException() {
assertThatIllegalArgumentException()
.isThrownBy(
() -> new FileSystemGeneratedFiles(kind -> (kind != Kind.CLASS)
? this.root.resolve(kind.toString()) : null))
.isThrownBy(() -> new FileSystemGeneratedFiles(kind -> (kind != Kind.CLASS) ?
this.root.resolve(kind.toString()) : null))
.withMessage("'roots' must return a value for all file kinds");
}
@ -102,8 +94,7 @@ class FileSystemGeneratedFilesTests { @@ -102,8 +94,7 @@ class FileSystemGeneratedFilesTests {
assertPathMustBeRelative(generatedFiles, "test/../../test");
}
private void assertPathMustBeRelative(FileSystemGeneratedFiles generatedFiles,
String path) {
private void assertPathMustBeRelative(FileSystemGeneratedFiles generatedFiles, String path) {
assertThatIllegalArgumentException()
.isThrownBy(() -> generatedFiles.addResourceFile(path, "test"))
.withMessage("'path' must be relative");

Loading…
Cancel
Save