A stream kept in a variable is operated on a second time, which throws
What it finds
A stream can be operated on once: after an operation has been applied, the stream is used up, and a second operation throws an IllegalStateException, as the java.util.stream documentation says. Keeping a stream in a variable and calling it twice, as in long n = s.count(); s.forEach(...), fails at the second call.
Reported is the second method call on a local variable declared as a Stream, IntStream, LongStream or DoubleStream, or with var from a call of stream(), that is never assigned again, when both calls run on the same path: they are not in different branches of one if, switch, conditional expression or try. close, parallel, sequential, isParallel and onClose do not use a stream up.
From the rule's source, reused_stream.rs.
import java.util.List;import java.util.stream.Stream; class Report { long show(List<String> lines, boolean flag) { Stream<String> words = lines.stream(); long n = words.count(); words.forEach(System.out::println);tx0 reports: this stream was already operated on var each = lines.stream(); each.filter(String::isEmpty); long m = each.count();tx0 reports: already operated on Stream<String> once = lines.stream().parallel(); once.parallel(); long k = once.count(); Stream<String> branch = lines.stream(); if (flag) { branch.forEach(System.out::println); } else { k += branch.count(); } return n + m + k; }} Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/java:reused-stream" = "off"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:reused-stream] reason="..."tx0 explain tx0/java:reused-stream shows the same in the terminal. Found a false positive? Report it.