Skip to content

Commit 1006af4

Browse files
committed
[GR-71061] Allow checking for warning messages in GlobUtils without writing them to console.
PullRequest: graal/22563
2 parents 8c98433 + e33d0df commit 1006af4

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/resources/CompressedGlobTrie/CompressedGlobTrie.java

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@
5151
* parameter represents list of glob patterns given in any order. At the very beginning, all given
5252
* globs will be validated using {@link GlobUtils#validatePattern(String)}. If all globs were
5353
* correct, they will be classified (with
54-
* {@link CompressedGlobTrieBuilder#classifyPatterns(List, List, List, List)}) and sorted (using
55-
* {@link CompressedGlobTrieBuilder#comparePatterns(GlobWithInfo, GlobWithInfo)} as the comparator
56-
* function). This preprocessing phase allows incremental structure build, going from the most
57-
* general glob pattern to the more specific ones. Furthermore, when trying to add a new glob, the
58-
* structure will first check if the given glob can be matched with some glob that already exists in
59-
* the structure (NOTE: possibly more than one glob pattern from the structure can match the new
60-
* pattern). When that is not the case, a new pattern will be added using
54+
* {@link CompressedGlobTrieBuilder#classifyPatterns(List, List, List, List, boolean)}) and sorted
55+
* (using {@link CompressedGlobTrieBuilder#comparePatterns(GlobWithInfo, GlobWithInfo)} as the
56+
* comparator function). This preprocessing phase allows incremental structure build, going from the
57+
* most general glob pattern to the more specific ones. Furthermore, when trying to add a new glob,
58+
* the structure will first check if the given glob can be matched with some glob that already
59+
* exists in the structure (NOTE: possibly more than one glob pattern from the structure can match
60+
* the new pattern). When that is not the case, a new pattern will be added using
6161
* {@link CompressedGlobTrieBuilder#addNewBranch}. This function will attempt identical matching
6262
* (where wildcards have no special semantics) to move as deep in the structure as possible. Once it
6363
* cannot proceed with the existing branches, the function will append rest of the pattern as the
@@ -100,13 +100,21 @@ public static class CompressedGlobTrieBuilder<C> {
100100
* with possibly additional context.
101101
*/
102102
public static <C> GlobTrieNode<C> build(List<GlobWithInfo<C>> patterns) {
103+
return build(patterns, true);
104+
}
105+
106+
/**
107+
* Builds an immutable CompressedGlobTrie structure from glob patterns given in any order
108+
* with possibly additional context. Validation of patterns can be turned off.
109+
*/
110+
public static <C> GlobTrieNode<C> build(List<GlobWithInfo<C>> patterns, boolean validatePatterns) {
103111
GlobTrieNode<C> root = new GlobTrieNode<>();
104112
/* classify patterns in groups */
105113
List<GlobWithInfo<C>> doubleStarPatterns = new ArrayList<>();
106114
List<GlobWithInfo<C>> starPatterns = new ArrayList<>();
107115
List<GlobWithInfo<C>> noStarPatterns = new ArrayList<>();
108116

109-
List<String> invalidPatternsErrors = classifyPatterns(patterns, doubleStarPatterns, starPatterns, noStarPatterns);
117+
List<String> invalidPatternsErrors = classifyPatterns(patterns, doubleStarPatterns, starPatterns, noStarPatterns, validatePatterns);
110118
if (!invalidPatternsErrors.isEmpty()) {
111119
invalidPatternsErrors.forEach(LogUtils::warning);
112120
}
@@ -213,19 +221,22 @@ private static <C> void addNewBranch(GlobTrieNode<C> root, List<GlobTrieNode<C>>
213221
private static <C> List<String> classifyPatterns(List<GlobWithInfo<C>> patterns,
214222
List<GlobWithInfo<C>> doubleStar,
215223
List<GlobWithInfo<C>> singleStar,
216-
List<GlobWithInfo<C>> noStar) {
224+
List<GlobWithInfo<C>> noStar,
225+
boolean validate) {
217226
List<String> invalidPatterns = new ArrayList<>();
218227
for (GlobWithInfo<C> patternWithInfo : patterns) {
219-
/* validate patterns */
220-
String error = GlobUtils.validatePattern(patternWithInfo.pattern());
221-
if (!error.isEmpty()) {
222-
if (patternWithInfo.additionalContent() instanceof ClassLoaderSupport.ConditionWithOrigin conditionWithOrigin) {
223-
invalidPatterns.add(error + "Pattern is from: " + conditionWithOrigin.origin());
224-
} else {
225-
invalidPatterns.add(error);
226-
}
228+
if (validate) {
229+
/* validate patterns */
230+
String error = GlobUtils.validatePattern(patternWithInfo.pattern());
231+
if (!error.isEmpty()) {
232+
if (patternWithInfo.additionalContent() instanceof ClassLoaderSupport.ConditionWithOrigin conditionWithOrigin) {
233+
invalidPatterns.add(error + "Pattern is from: " + conditionWithOrigin.origin());
234+
} else {
235+
invalidPatterns.add(error);
236+
}
227237

228-
continue;
238+
continue;
239+
}
229240
}
230241

231242
String pattern = patternWithInfo.pattern();

substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/GlobUtils.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.util.ArrayList;
2929
import java.util.List;
30+
import java.util.function.Consumer;
3031
import java.util.regex.Pattern;
3132

3233
public final class GlobUtils {
@@ -72,6 +73,10 @@ public static String transformToTriePath(String resource, String module) {
7273
}
7374

7475
public static String validatePattern(String pattern) {
76+
return validatePattern(pattern, LogUtils::warning);
77+
}
78+
79+
public static String validatePattern(String pattern, Consumer<String> warner) {
7580
StringBuilder sb = new StringBuilder();
7681

7782
if (pattern.isEmpty()) {
@@ -120,9 +125,10 @@ public static String validatePattern(String pattern) {
120125
break outer;
121126
} else if (token.kind == GlobToken.Kind.STAR_STAR) {
122127
String patternWithoutModule = pattern.substring(ALL_UNNAMED.length() + 1);
123-
LogUtils.warning("Pattern: " + patternWithoutModule + " contains ** without previous literal. " +
128+
String message = "Pattern: " + patternWithoutModule + " contains ** without previous literal. " +
124129
"This pattern is too generic and therefore can match many resources. " +
125-
"Please make the pattern more specific by adding non-generic level before ** level.");
130+
"Please make the pattern more specific by adding non-generic level before ** level.";
131+
warner.accept(message);
126132
}
127133
}
128134
}

0 commit comments

Comments
 (0)