Findbugs' bugs for java8
We know that findbugs is dedicated for code bugs check. It works on many common bug patterns. But today, I found a bug of findbugs, it ignores a bug on java8 codes.
1. The codes
Here we use jdk 1.8, we use streams to travese a string list like this.
List<String> titles = Arrays.asList("Java7","Java8","Java9");
Stream<String> s = titles.stream();
s.forEach(System.out::println);
s.forEach(System.out::println);/*should be a bug here */
As the comments shown, the second line of the s.forEach is a bug, because in java8, one stream object can only be traversed once, so ,if you run the program, you would get this:
Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
at java.util.stream.AbstractPipeline.sourceStageSpliterator(AbstractPipeline.java:274)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at streams.Main1.main(Main1.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
2. Let findbugs check it
Now we call findbugs to check it, just from the context menu, we got this result:
You can find that no bugs found by findbugs,but there is one!!!
I think findbugs should fix it ASAP.
You can find detail documents about the java8 here: