From 8627bef8d90c67c57fb6bbf7df19027699652e4b Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 6 May 2022 14:12:30 +0200 Subject: [PATCH] Polishing --- .../generate/FileSystemGeneratedFiles.java | 8 ++--- .../org/springframework/util/ObjectUtils.java | 28 +++++++++--------- .../FileSystemGeneratedFilesTests.java | 29 +++++++------------ 3 files changed, 29 insertions(+), 36 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/aot/generate/FileSystemGeneratedFiles.java b/spring-core/src/main/java/org/springframework/aot/generate/FileSystemGeneratedFiles.java index 2839096707..33f62c5dbb 100644 --- a/spring-core/src/main/java/org/springframework/aot/generate/FileSystemGeneratedFiles.java +++ b/spring-core/src/main/java/org/springframework/aot/generate/FileSystemGeneratedFiles.java @@ -73,9 +73,9 @@ public class FileSystemGeneratedFiles implements GeneratedFiles { private static Function 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 { 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"); diff --git a/spring-core/src/main/java/org/springframework/util/ObjectUtils.java b/spring-core/src/main/java/org/springframework/util/ObjectUtils.java index 8e91923c9e..49f8d132c8 100644 --- a/spring-core/src/main/java/org/springframework/util/ObjectUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ObjectUtils.java @@ -250,33 +250,35 @@ public abstract class ObjectUtils { * @return the new array (of the same component type; never {@code null}) */ public static 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[] 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; } /** diff --git a/spring-core/src/test/java/org/springframework/aot/generate/FileSystemGeneratedFilesTests.java b/spring-core/src/test/java/org/springframework/aot/generate/FileSystemGeneratedFilesTests.java index c3cc52a864..d2698c0e53 100644 --- a/spring-core/src/test/java/org/springframework/aot/generate/FileSystemGeneratedFilesTests.java +++ b/spring-core/src/test/java/org/springframework/aot/generate/FileSystemGeneratedFilesTests.java @@ -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 { 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 { @Test void createWhenRootsIsNullThrowsException() { assertThatIllegalArgumentException() - .isThrownBy( - () -> new FileSystemGeneratedFiles((Function) null)) + .isThrownBy(() -> new FileSystemGeneratedFiles((Function) 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 { 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");