From 649c2f56fd9db576ef86475c2ca652ce8c62222e Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 11 Sep 2022 23:33:39 +0200 Subject: [PATCH] Accept only ClassPathResource in ResourceHints#registerResource() This commit throws an exception in registerResource() if the supplied resource is not a ClassPathResource. See gh-29083 --- .../org/springframework/aot/hint/ResourceHints.java | 13 ++++++------- .../aot/hint/ResourceHintsTests.java | 7 ++++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/aot/hint/ResourceHints.java b/spring-core/src/main/java/org/springframework/aot/hint/ResourceHints.java index fbca2547f0c..e17bd9934a2 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/ResourceHints.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/ResourceHints.java @@ -27,7 +27,6 @@ import java.util.stream.Stream; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.lang.Nullable; -import org.springframework.util.Assert; /** * Gather the need for resources available at runtime. @@ -115,19 +114,19 @@ public class ResourceHints { /** * Register that the supplied resource should be made available at runtime. - *

If the supplied resource is not a {@link ClassPathResource}, it will - * not be registered. * @param resource the resource to register - * @throws IllegalArgumentException if the supplied class path resource does - * not {@linkplain Resource#exists() exist} + * @throws IllegalArgumentException if the supplied resource is not a + * {@link ClassPathResource} or does not {@linkplain Resource#exists() exist} * @see #registerPattern(String) * @see ClassPathResource#getAbsolutePath() */ public void registerResource(Resource resource) { - if (resource instanceof ClassPathResource classPathResource) { - Assert.isTrue(classPathResource.exists(), () -> "Resource does not exist: " + classPathResource); + if (resource instanceof ClassPathResource classPathResource && classPathResource.exists()) { registerPattern(classPathResource.getAbsolutePath()); } + else { + throw new IllegalArgumentException("Resource must be a ClassPathResource that exists: " + resource); + } } /** diff --git a/spring-core/src/test/java/org/springframework/aot/hint/ResourceHintsTests.java b/spring-core/src/test/java/org/springframework/aot/hint/ResourceHintsTests.java index 616d2997395..7b9fe6feee0 100644 --- a/spring-core/src/test/java/org/springframework/aot/hint/ResourceHintsTests.java +++ b/spring-core/src/test/java/org/springframework/aot/hint/ResourceHintsTests.java @@ -117,8 +117,9 @@ class ResourceHintsTests { @Test void registerResourceWithUnsupportedResourceType() { DescriptiveResource resource = new DescriptiveResource("bogus"); - this.resourceHints.registerResource(resource); - assertThat(this.resourceHints.resourcePatterns()).isEmpty(); + assertThatIllegalArgumentException() + .isThrownBy(() -> this.resourceHints.registerResource(resource)) + .withMessage("Resource must be a ClassPathResource that exists: %s", resource); } @Test @@ -126,7 +127,7 @@ class ResourceHintsTests { ClassPathResource resource = new ClassPathResource("bogus", getClass()); assertThatIllegalArgumentException() .isThrownBy(() -> this.resourceHints.registerResource(resource)) - .withMessage("Resource does not exist: %s", resource); + .withMessage("Resource must be a ClassPathResource that exists: %s", resource); } @Test