Skip to content

Commit 7fd53e9

Browse files
committed
Fix SpotBugs error: org.apache.commons.io.function.IOStream$1.next()
cannot throw NoSuchElementException [org.apache.commons.io.function.IOStream$1] At IOStream.java:[line 98] IT_NO_SUCH_ELEMENT
1 parent 509395d commit 7fd53e9

3 files changed

Lines changed: 12 additions & 3 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ The <action> type attribute can be add,update,fix,remove.
5858
<action dev="ggregory" type="fix" due-to="Gary Gregory">Fix SpotBugs error: Class org.apache.commons.io.filefilter.RegexFileFilter defines non-transient non-serializable instance field pathToString [org.apache.commons.io.filefilter.RegexFileFilter] In RegexFileFilter.java SE_BAD_FIELD.</action>
5959
<action dev="ggregory" type="fix" due-to="Gary Gregory">Fix SpotBugs error: Class org.apache.commons.io.filefilter.DelegateFileFilter defines non-transient non-serializable instance field fileFilter [org.apache.commons.io.filefilter.DelegateFileFilter] In DelegateFileFilter.java SE_BAD_FIELD.</action>
6060
<action dev="ggregory" type="fix" due-to="Gary Gregory">Fix SpotBugs error: Class org.apache.commons.io.filefilter.DelegateFileFilter defines non-transient non-serializable instance field fileNameFilter [org.apache.commons.io.filefilter.DelegateFileFilter] In DelegateFileFilter.java SE_BAD_FIELD.</action>
61+
<action dev="ggregory" type="fix" due-to="Gary Gregory">Fix SpotBugs error: org.apache.commons.io.function.IOStream$1.next() cannot throw NoSuchElementException [org.apache.commons.io.function.IOStream$1] At IOStream.java:[line 98] IT_NO_SUCH_ELEMENT.</action>
6162
<!-- UPDATE -->
6263
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump org.codehaus.mojo:exec-maven-plugin from 3.1.0 to 3.1.1 #512.</action>
6364
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump commons-lang3 from 3.13.0 to 3.14.0.</action>

src/main/java/org/apache/commons/io/function/IOStream.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Arrays;
2323
import java.util.Iterator;
2424
import java.util.List;
25+
import java.util.NoSuchElementException;
2526
import java.util.Objects;
2627
import java.util.Optional;
2728
import java.util.Spliterator;
@@ -93,8 +94,14 @@ public boolean hasNext() {
9394
}
9495

9596
@Override
96-
public T next() {
97-
return t = t == IOStreams.NONE ? seed : Erase.apply(f, t);
97+
public T next() throws NoSuchElementException {
98+
try {
99+
return t = t == IOStreams.NONE ? seed : f.apply(t);
100+
} catch (IOException e) {
101+
final NoSuchElementException nsee = new NoSuchElementException();
102+
nsee.initCause(e);
103+
throw nsee;
104+
}
98105
}
99106
};
100107
return adapt(StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED | Spliterator.IMMUTABLE), false));

src/test/java/org/apache/commons/io/function/IOStreamTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.IOException;
3030
import java.util.Arrays;
3131
import java.util.Collections;
32+
import java.util.NoSuchElementException;
3233
import java.util.concurrent.atomic.AtomicInteger;
3334
import java.util.concurrent.atomic.AtomicReference;
3435
import java.util.stream.Collectors;
@@ -296,7 +297,7 @@ public void testIterateException() throws IOException {
296297
final IOStream<Long> stream = IOStream.iterate(1L, TestUtils.throwingIOUnaryOperator());
297298
final IOIterator<Long> iterator = stream.iterator();
298299
assertEquals(1L, iterator.next());
299-
assertThrows(IOException.class, () -> iterator.next());
300+
assertThrows(NoSuchElementException.class, () -> iterator.next());
300301
}
301302

302303
@SuppressWarnings("resource") // custom stream not recognized by compiler warning machinery

0 commit comments

Comments
 (0)