Fix for warning related to stream api call chains in java.base
Vipin Sharma
vipinsharma85 at gmail.com
Sun May 3 15:10:16 UTC 2020
Hi All,
I have fixed some warnings in java.base module, following are 3 type of
code changes in this patch:
! stream().filter(...).findFirst().isPresent() replaced with
stream().noneMatch(...))
stream.filter(...).findFirst().isPresent() replaced with
stream.anyMatch(...)
stream.collect(Collectors.joining(",")) replaced with String.join(",",
stringList)
This patch improves performance as well, jmh results available at
https://gist.github.com/Vipin-Sharma/00f1d6da6b8e0e0bc7bec2e33bce85ed, end
of gist file you can see results. Need a sponsor for this patch.
Patch:
--- old/src/java.base/share/classes/java/lang/invoke/MethodHandles.java
2020-05-03 19:44:09.519289354 +0530
+++ new/src/java.base/share/classes/java/lang/invoke/MethodHandles.java
2020-05-03 19:44:09.087294954 +0530
@@ -6349,7 +6349,7 @@
loopReturnType + ")");
}
- if
(!pred.stream().filter(Objects::nonNull).findFirst().isPresent()) {
+ if (pred.stream().noneMatch(Objects::nonNull)) {
throw newIllegalArgumentException("no predicate found", pred);
}
if
(pred.stream().filter(Objects::nonNull).map(MethodHandle::type).map(MethodType::returnType).
--- old/src/java.base/share/classes/java/util/Locale.java 2020-05-03
19:44:10.915271183 +0530
+++ new/src/java.base/share/classes/java/util/Locale.java 2020-05-03
19:44:10.487276766 +0530
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights
reserved.
+ * Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights
reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -2250,7 +2250,7 @@
// If we have no list patterns, compose the list in a simple,
// non-localized way.
if (pattern == null) {
- return
Arrays.stream(stringList).collect(Collectors.joining(","));
+ return String.join(",", stringList);
}
switch (stringList.length) {
--- old/src/java.base/share/classes/jdk/internal/module/DefaultRoots.java
2020-05-03 19:44:11.911258115 +0530
+++ new/src/java.base/share/classes/jdk/internal/module/DefaultRoots.java
2020-05-03 19:44:11.479263786 +0530
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights
reserved.
+ * Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights
reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -74,8 +74,6 @@
private static boolean exportsAPI(ModuleDescriptor descriptor) {
return descriptor.exports()
.stream()
- .filter(e -> !e.isQualified())
- .findAny()
- .isPresent();
+ .anyMatch(e -> !e.isQualified());
}
}
--- old/src/java.base/share/classes/sun/launcher/LauncherHelper.java
2020-05-03 19:44:12.863245549 +0530
+++ new/src/java.base/share/classes/sun/launcher/LauncherHelper.java
2020-05-03 19:44:12.427251308 +0530
@@ -1142,7 +1142,7 @@
ostream.format("uses %s%n", s);
}
for (Provides ps : md.provides()) {
- String names =
ps.providers().stream().collect(Collectors.joining(" "));
+ String names = String.join(" ", ps.providers());
ostream.format("provides %s with %s%n", ps.service(), names);
}
@@ -1150,7 +1150,7 @@
// qualified exports
for (Exports e : md.exports()) {
if (e.isQualified()) {
- String who =
e.targets().stream().collect(Collectors.joining(" "));
+ String who = String.join(" ", e.targets());
ostream.format("qualified exports %s to %s%n", e.source(),
who);
}
}
@@ -1164,7 +1164,7 @@
.collect(Collectors.joining(" "));
ostream.format("opens %s", sourceAndMods);
if (opens.isQualified()) {
- String who =
opens.targets().stream().collect(Collectors.joining(" "));
+ String who = String.join(" ", opens.targets());
ostream.format(" to %s", who);
}
ostream.println();
---
old/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java
2020-05-03 19:44:14.143228540 +0530
+++
new/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java
2020-05-03 19:44:13.711234292 +0530
@@ -380,7 +380,7 @@
return parse(oset);
} catch (OptionException ex) {
if (ex.options().isEmpty()) {
- msg("jshell.err.opt.invalid",
stream(args).collect(joining(", ")));
+ msg("jshell.err.opt.invalid", String.join(", ", args));
} else {
boolean isKnown =
parser.recognizedOptions().containsKey(ex.options().iterator().next());
msg(isKnown
@@ -2041,16 +2041,14 @@
if (matches.length == 0) {
// There are no matching sub-commands
errormsg("jshell.err.arg", cmd, sub);
- fluffmsg("jshell.msg.use.one.of", Arrays.stream(subs)
- .collect(Collectors.joining(", "))
+ fluffmsg("jshell.msg.use.one.of", String.join(", ", subs)
);
return null;
}
if (matches.length > 1) {
// More than one sub-command matches the initial characters
provided
errormsg("jshell.err.sub.ambiguous", cmd, sub);
- fluffmsg("jshell.msg.use.one.of", Arrays.stream(matches)
- .collect(Collectors.joining(", "))
+ fluffmsg("jshell.msg.use.one.of", String.join(", ", matches)
);
return null;
}
Regards,
Vipin
More information about the core-libs-dev
mailing list