Skip to content

Commit 649c2f5

Browse files
committed
Accept only ClassPathResource in ResourceHints#registerResource()
This commit throws an exception in registerResource() if the supplied resource is not a ClassPathResource. See gh-29083
1 parent 7605ed7 commit 649c2f5

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

spring-core/src/main/java/org/springframework/aot/hint/ResourceHints.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.core.io.ClassPathResource;
2828
import org.springframework.core.io.Resource;
2929
import org.springframework.lang.Nullable;
30-
import org.springframework.util.Assert;
3130

3231
/**
3332
* Gather the need for resources available at runtime.
@@ -115,19 +114,19 @@ public ResourceHints registerPattern(String include) {
115114

116115
/**
117116
* Register that the supplied resource should be made available at runtime.
118-
* <p>If the supplied resource is not a {@link ClassPathResource}, it will
119-
* not be registered.
120117
* @param resource the resource to register
121-
* @throws IllegalArgumentException if the supplied class path resource does
122-
* not {@linkplain Resource#exists() exist}
118+
* @throws IllegalArgumentException if the supplied resource is not a
119+
* {@link ClassPathResource} or does not {@linkplain Resource#exists() exist}
123120
* @see #registerPattern(String)
124121
* @see ClassPathResource#getAbsolutePath()
125122
*/
126123
public void registerResource(Resource resource) {
127-
if (resource instanceof ClassPathResource classPathResource) {
128-
Assert.isTrue(classPathResource.exists(), () -> "Resource does not exist: " + classPathResource);
124+
if (resource instanceof ClassPathResource classPathResource && classPathResource.exists()) {
129125
registerPattern(classPathResource.getAbsolutePath());
130126
}
127+
else {
128+
throw new IllegalArgumentException("Resource must be a ClassPathResource that exists: " + resource);
129+
}
131130
}
132131

133132
/**

spring-core/src/test/java/org/springframework/aot/hint/ResourceHintsTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,17 @@ void registerIfPresentIgnoreMissingLocation() {
117117
@Test
118118
void registerResourceWithUnsupportedResourceType() {
119119
DescriptiveResource resource = new DescriptiveResource("bogus");
120-
this.resourceHints.registerResource(resource);
121-
assertThat(this.resourceHints.resourcePatterns()).isEmpty();
120+
assertThatIllegalArgumentException()
121+
.isThrownBy(() -> this.resourceHints.registerResource(resource))
122+
.withMessage("Resource must be a ClassPathResource that exists: %s", resource);
122123
}
123124

124125
@Test
125126
void registerResourceWithNonexistentClassPathResource() {
126127
ClassPathResource resource = new ClassPathResource("bogus", getClass());
127128
assertThatIllegalArgumentException()
128129
.isThrownBy(() -> this.resourceHints.registerResource(resource))
129-
.withMessage("Resource does not exist: %s", resource);
130+
.withMessage("Resource must be a ClassPathResource that exists: %s", resource);
130131
}
131132

132133
@Test

0 commit comments

Comments
 (0)