diff --git a/spring-core-test/src/test/java/org/springframework/aot/agent/InstrumentedMethodTests.java b/spring-core-test/src/test/java/org/springframework/aot/agent/InstrumentedMethodTests.java index 60f100ea8a..e2839c46ad 100644 --- a/spring-core-test/src/test/java/org/springframework/aot/agent/InstrumentedMethodTests.java +++ b/spring-core-test/src/test/java/org/springframework/aot/agent/InstrumentedMethodTests.java @@ -36,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link InstrumentedMethod}. * * @author Brian Clozel + * @author Sam Brannen */ class InstrumentedMethodTests { @@ -556,7 +557,7 @@ class InstrumentedMethodTests { void classGetResourceShouldMatchResourcePatternWhenAbsolute() { RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETRESOURCE) .onInstance(InstrumentedMethodTests.class).withArgument("/some/path/resource.txt").build(); - hints.resources().registerPattern("/some/*"); + hints.resources().registerPattern("some/*"); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETRESOURCE, invocation); } @@ -564,7 +565,7 @@ class InstrumentedMethodTests { void classGetResourceShouldMatchResourcePatternWhenRelative() { RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETRESOURCE) .onInstance(InstrumentedMethodTests.class).withArgument("resource.txt").build(); - hints.resources().registerPattern("/org/springframework/aot/agent/*"); + hints.resources().registerPattern("org/springframework/aot/agent/*"); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETRESOURCE, invocation); } @@ -572,7 +573,7 @@ class InstrumentedMethodTests { void classGetResourceShouldNotMatchResourcePatternWhenInvalid() { RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETRESOURCE) .onInstance(InstrumentedMethodTests.class).withArgument("/some/path/resource.txt").build(); - hints.resources().registerPattern("/other/*"); + hints.resources().registerPattern("other/*"); assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETRESOURCE, invocation); } @@ -580,7 +581,7 @@ class InstrumentedMethodTests { void classGetResourceShouldNotMatchResourcePatternWhenExcluded() { RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETRESOURCE) .onInstance(InstrumentedMethodTests.class).withArgument("/some/path/resource.txt").build(); - hints.resources().registerPattern(resourceHint -> resourceHint.includes("/some/*").excludes("/some/path/*")); + hints.resources().registerPattern(resourceHint -> resourceHint.includes("some/*").excludes("some/path/*")); assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETRESOURCE, invocation); } diff --git a/spring-core/src/main/java/org/springframework/aot/hint/predicate/ResourceHintsPredicates.java b/spring-core/src/main/java/org/springframework/aot/hint/predicate/ResourceHintsPredicates.java index df93b6a058..8aa0060662 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/predicate/ResourceHintsPredicates.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/predicate/ResourceHintsPredicates.java @@ -34,6 +34,7 @@ import org.springframework.util.ConcurrentLruCache; * * @author Brian Clozel * @author Stephane Nicoll + * @author Sam Brannen * @since 6.0 */ public class ResourceHintsPredicates { @@ -58,7 +59,11 @@ public class ResourceHintsPredicates { * Return a predicate that checks whether a resource hint is registered for the given * resource name, located in the given type's package. *
For example, {@code forResource(org.example.MyClass, "myResource.txt")} - * will match for {@code "/org/example/myResource.txt"}. + * will match against {@code "org/example/myResource.txt"}. + *
If the given resource name is an absolute path (i.e., starts with a + * leading slash), the supplied type will be ignored. For example, + * {@code forResource(org.example.MyClass, "/myResource.txt")} will match against + * {@code "myResource.txt"}. * @param type the type's package where to look for the resource * @param resourceName the resource name * @return the {@link RuntimeHints} predicate @@ -69,32 +74,39 @@ public class ResourceHintsPredicates { } private String resolveAbsoluteResourceName(TypeReference type, String resourceName) { + // absolute path if (resourceName.startsWith("/")) { + return resourceName.substring(1); + } + // default package + else if (type.getPackageName().isEmpty()) { return resourceName; } + // relative path else { - return "/" + type.getPackageName().replace('.', '/') - + "/" + resourceName; + return type.getPackageName().replace('.', '/') + "/" + resourceName; } } /** * Return a predicate that checks whether a resource hint is registered for * the given resource name. - * @param resourceName the full resource name + *
A leading slash will be removed.
+ * @param resourceName the absolute resource name
* @return the {@link RuntimeHints} predicate
*/
public Predicate