Allow registration of resource hint for root directory

Prior to this commit, it was not possible to register the root
directory as a native image resource; however, it is necessary to be
able to register the root directory to enable classpath scanning of the
root directory within a native image -- for example, to support
resource patterns such as `classpath*:/*.properties`.

This commit therefore relaxes the precondition check in the
ResourcePatternHint constructor to allow explicit registration of the
root directory.

Closes gh-29402
This commit is contained in:
Sam Brannen
2022-10-30 18:18:07 +01:00
parent 3c42363ba4
commit d03102edc3
3 changed files with 23 additions and 4 deletions

View File

@@ -43,7 +43,8 @@ import org.springframework.util.Assert;
* and its child directories at any depth.</li>
* </ul>
*
* <p>A resource pattern must not start with a slash ({@code /}).
* <p>A resource pattern must not start with a slash ({@code /}) unless it is the
* root directory.
*
* @author Stephane Nicoll
* @author Brian Clozel
@@ -58,13 +59,16 @@ public final class ResourcePatternHint implements ConditionalHint {
@Nullable
private final TypeReference reachableType;
ResourcePatternHint(String pattern, @Nullable TypeReference reachableType) {
Assert.isTrue(!pattern.startsWith("/"),
() -> "Resource pattern [%s] must not start with a '/'".formatted(pattern));
Assert.isTrue(("/".equals(pattern) || !pattern.startsWith("/")),
() -> "Resource pattern [%s] must not start with a '/' unless it is the root directory"
.formatted(pattern));
this.pattern = pattern;
this.reachableType = reachableType;
}
/**
* Return the pattern to use for identifying the resources to match.
* @return the pattern