Skip to content

Commit c235ad0

Browse files
committed
Introduce ResourcePatternHint#toRegex
This change is done for several reasons: - Move the logic where it is documented. - Test it with ResourcePatternHintTests. - Allow RuntimeHintsPredicates to leverage this logic. Closes gh-28620
1 parent 77ad4a1 commit c235ad0

File tree

3 files changed

+88
-14
lines changed

3 files changed

+88
-14
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
package org.springframework.aot.hint;
1818

19+
import java.util.Arrays;
1920
import java.util.Objects;
21+
import java.util.regex.Pattern;
22+
import java.util.stream.Collectors;
2023

2124
import org.springframework.lang.Nullable;
2225

@@ -58,12 +61,26 @@ public final class ResourcePatternHint implements ConditionalHint {
5861

5962
/**
6063
* Return the pattern to use for identifying the resources to match.
61-
* @return the patterns
64+
* @return the pattern
6265
*/
6366
public String getPattern() {
6467
return this.pattern;
6568
}
6669

70+
/**
71+
* Return the regex {@link Pattern} to use for identifying the resources to match.
72+
* @return the regex pattern
73+
*/
74+
public Pattern toRegex() {
75+
String prefix = (this.pattern.startsWith("*") ? ".*" : "");
76+
String suffix = (this.pattern.endsWith("*") ? ".*" : "");
77+
String regex = Arrays.stream(this.pattern.split("\\*"))
78+
.filter(s -> !s.isEmpty())
79+
.map(Pattern::quote)
80+
.collect(Collectors.joining(".*", prefix, suffix));
81+
return Pattern.compile(regex);
82+
}
83+
6784
@Nullable
6885
@Override
6986
public TypeReference getReachableType() {

spring-core/src/main/java/org/springframework/aot/nativex/ResourceHintsWriter.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616

1717
package org.springframework.aot.nativex;
1818

19-
import java.util.Arrays;
2019
import java.util.Collection;
2120
import java.util.LinkedHashMap;
2221
import java.util.List;
2322
import java.util.Map;
24-
import java.util.regex.Pattern;
25-
import java.util.stream.Collectors;
2623
import java.util.stream.Stream;
2724

2825
import org.springframework.aot.hint.ConditionalHint;
@@ -77,19 +74,10 @@ private Map<String, Object> toAttributes(ResourceBundleHint hint) {
7774
private Map<String, Object> toAttributes(ResourcePatternHint hint) {
7875
Map<String, Object> attributes = new LinkedHashMap<>();
7976
handleCondition(attributes, hint);
80-
attributes.put("pattern", patternToRegexp(hint.getPattern()));
77+
attributes.put("pattern", hint.toRegex().toString());
8178
return attributes;
8279
}
8380

84-
private String patternToRegexp(String pattern) {
85-
String prefix = (pattern.startsWith("*") ? ".*" : "");
86-
String suffix = (pattern.endsWith("*") ? ".*" : "");
87-
return Arrays.stream(pattern.split("\\*"))
88-
.filter(s -> !s.isEmpty())
89-
.map(Pattern::quote)
90-
.collect(Collectors.joining(".*", prefix, suffix));
91-
}
92-
9381
private void addIfNotEmpty(Map<String, Object> attributes, String name, @Nullable Object value) {
9482
if (value instanceof Collection<?> collection) {
9583
if (!collection.isEmpty()) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.aot.hint;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
/**
24+
* Tests for {@link ResourcePatternHint}.
25+
*
26+
* @author Sebastien Deleuze
27+
*/
28+
public class ResourcePatternHintTests {
29+
30+
@Test
31+
void fileAtRoot() {
32+
ResourcePatternHint hint = new ResourcePatternHint("file.properties", null);
33+
assertThat(hint.toRegex().asMatchPredicate())
34+
.accepts("file.properties")
35+
.rejects("com/example/file.properties", "file.prop", "another-file.properties");
36+
}
37+
38+
@Test
39+
void fileInDirectory() {
40+
ResourcePatternHint hint = new ResourcePatternHint("com/example/file.properties", null);
41+
assertThat(hint.toRegex().asMatchPredicate())
42+
.accepts("com/example/file.properties")
43+
.rejects("file.properties", "com/file.properties", "com/example/another-file.properties");
44+
}
45+
46+
@Test
47+
void extension() {
48+
ResourcePatternHint hint = new ResourcePatternHint("*.properties", null);
49+
assertThat(hint.toRegex().asMatchPredicate())
50+
.accepts("file.properties", "com/example/file.properties")
51+
.rejects("file.prop", "com/example/file.prop");
52+
}
53+
54+
@Test
55+
void extensionInDirectoryAtAnyDepth() {
56+
ResourcePatternHint hint = new ResourcePatternHint("com/example/*.properties", null);
57+
assertThat(hint.toRegex().asMatchPredicate())
58+
.accepts("com/example/file.properties", "com/example/another/file.properties")
59+
.rejects("file.properties", "com/file.properties");
60+
}
61+
62+
@Test
63+
void anyFileInDirectoryAtAnyDepth() {
64+
ResourcePatternHint hint = new ResourcePatternHint("com/example/*", null);
65+
assertThat(hint.toRegex().asMatchPredicate())
66+
.accepts("com/example/file.properties", "com/example/another/file.properties", "com/example/another")
67+
.rejects("file.properties", "com/file.properties");
68+
}
69+
}

0 commit comments

Comments
 (0)