gnodet commented on code in PR #13118:
URL: https://github.com/apache/maven/pull/13118#discussion_r4027353445
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSettingsBuilder.java:
##########
@@ -245,6 +246,47 @@ private Server serverAlias(Server server, String id) {
return Server.newBuilder(server,
true).id(id).aliases(List.of()).build();
}
+ @Nullable
+ private SettingsParser selectParser(Source source,
ProblemCollector<BuilderProblem> problems) {
+ List<Map.Entry<String, SettingsParser>> matches = new ArrayList<>();
+ for (Map.Entry<String, SettingsParser> entry :
settingsParsers.entrySet()) {
+ boolean supported;
+ try {
+ supported = entry.getValue().supports(source);
+ } catch (RuntimeException e) {
+ problems.reportProblem(new DefaultBuilderProblem(
+ source.getLocation(),
+ -1,
+ -1,
+ e,
+ "Settings parser '" + (entry.getKey() != null ?
entry.getKey() : "<unnamed>")
+ + "' failed to determine support for this
source",
+ BuilderProblem.Severity.FATAL));
+ return null;
+ }
+ if (supported) {
+ matches.add(entry);
+ }
+ }
+ if (matches.size() > 1) {
+ problems.reportProblem(new DefaultBuilderProblem(
+ source.getLocation(),
+ -1,
+ -1,
+ null,
+ "Multiple settings parsers support this source: "
+ + String.join(
+ ", ",
+ matches.stream()
+ .map(entry -> entry.getKey() !=
null ? entry.getKey() : "<unnamed>")
+ .sorted()
+ .toList()),
+ BuilderProblem.Severity.FATAL));
+ return null;
+ }
+ return matches.isEmpty() ? xmlSettingsParser :
matches.get(0).getValue();
+ }
Review Comment:
`selectParser` can be made more compact by:
1. Extracting the repeated `entry.getKey() != null ? entry.getKey() :
"<unnamed>"` into a small `parserName` helper
2. Building the message in a local variable before `reportProblem`, so the
6-arg constructor fits on one line instead of being spread across 7
Spotless-verified (compiled and reformatted cleanly).
```suggestion
@Nullable
private SettingsParser selectParser(Source source,
ProblemCollector<BuilderProblem> problems) {
List<Map.Entry<String, SettingsParser>> matches = new ArrayList<>();
for (Map.Entry<String, SettingsParser> entry :
settingsParsers.entrySet()) {
boolean supported;
try {
supported = entry.getValue().supports(source);
} catch (RuntimeException e) {
String msg = "Settings parser '" + parserName(entry.getKey())
+ "' failed to determine support for this source";
problems.reportProblem(
new DefaultBuilderProblem(source.getLocation(), -1,
-1, e, msg, BuilderProblem.Severity.FATAL));
return null;
}
if (supported) {
matches.add(entry);
}
}
if (matches.size() > 1) {
String parsers =
matches.stream().map(e ->
parserName(e.getKey())).sorted().collect(Collectors.joining(", "));
String msg = "Multiple settings parsers support this source: " +
parsers;
problems.reportProblem(
new DefaultBuilderProblem(source.getLocation(), -1, -1,
null, msg, BuilderProblem.Severity.FATAL));
return null;
}
return matches.isEmpty() ? xmlSettingsParser :
matches.get(0).getValue();
}
private static String parserName(@Nullable String key) {
return key != null ? key : "<unnamed>";
}
```
Also needs `import java.util.stream.Collectors;` added to the imports.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]