提交 f01ebf86 编写于 作者: A alanb

6865748: (file) SimpleFileVisitor methods ignore null arguments

Reviewed-by: sherman
上级 695b4e15
...@@ -47,6 +47,14 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> { ...@@ -47,6 +47,14 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> {
protected SimpleFileVisitor() { protected SimpleFileVisitor() {
} }
/**
* Throws NullPointerException if obj is null.
*/
private static void checkNotNull(Object obj) {
if (obj == null)
throw new NullPointerException();
}
/** /**
* Invoked for a directory before entries in the directory are visited. * Invoked for a directory before entries in the directory are visited.
* *
...@@ -55,6 +63,7 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> { ...@@ -55,6 +63,7 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> {
*/ */
@Override @Override
public FileVisitResult preVisitDirectory(T dir) { public FileVisitResult preVisitDirectory(T dir) {
checkNotNull(dir);
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
...@@ -70,6 +79,8 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> { ...@@ -70,6 +79,8 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> {
*/ */
@Override @Override
public FileVisitResult preVisitDirectoryFailed(T dir, IOException exc) { public FileVisitResult preVisitDirectoryFailed(T dir, IOException exc) {
checkNotNull(dir);
checkNotNull(exc);
throw new IOError(exc); throw new IOError(exc);
} }
...@@ -81,6 +92,8 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> { ...@@ -81,6 +92,8 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> {
*/ */
@Override @Override
public FileVisitResult visitFile(T file, BasicFileAttributes attrs) { public FileVisitResult visitFile(T file, BasicFileAttributes attrs) {
checkNotNull(file);
checkNotNull(attrs);
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
...@@ -96,6 +109,8 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> { ...@@ -96,6 +109,8 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> {
*/ */
@Override @Override
public FileVisitResult visitFileFailed(T file, IOException exc) { public FileVisitResult visitFileFailed(T file, IOException exc) {
checkNotNull(file);
checkNotNull(exc);
throw new IOError(exc); throw new IOError(exc);
} }
...@@ -114,6 +129,7 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> { ...@@ -114,6 +129,7 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> {
*/ */
@Override @Override
public FileVisitResult postVisitDirectory(T dir, IOException exc) { public FileVisitResult postVisitDirectory(T dir, IOException exc) {
checkNotNull(dir);
if (exc != null) if (exc != null)
throw new IOError(exc); throw new IOError(exc);
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
......
...@@ -22,13 +22,14 @@ ...@@ -22,13 +22,14 @@
*/ */
/* @test /* @test
* @bug 4313887 6838333 * @bug 4313887 6838333 6865748
* @summary Unit test for java.nio.file.Files for miscellenous cases not * @summary Unit test for java.nio.file.Files for miscellenous cases not
* covered by other tests * covered by other tests
* @library .. * @library ..
*/ */
import java.nio.file.*; import java.nio.file.*;
import java.nio.file.attribute.Attributes;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
...@@ -113,5 +114,29 @@ public class Misc { ...@@ -113,5 +114,29 @@ public class Misc {
npeExpected(); npeExpected();
} catch (NullPointerException e) { } catch (NullPointerException e) {
} }
SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() { };
boolean ranTheGauntlet = false;
try { visitor.preVisitDirectory(null);
} catch (NullPointerException x0) {
try { visitor.preVisitDirectoryFailed(null, new IOException());
} catch (NullPointerException x1) {
try { visitor.preVisitDirectoryFailed(dir, null);
} catch (NullPointerException x2) {
try { visitor.visitFile(null, Attributes.readBasicFileAttributes(Paths.get(".")));
} catch (NullPointerException x3) {
try { visitor.visitFile(dir, null);
} catch (NullPointerException x4) {
try { visitor.visitFileFailed(null, new IOException());
} catch (NullPointerException x5) {
try { visitor.visitFileFailed(dir, null);
} catch (NullPointerException x6) {
try { visitor.postVisitDirectory(null, new IOException());
} catch (NullPointerException x7) {
// if we get here then all visit* methods threw NPE as expected
ranTheGauntlet = true;
}}}}}}}}
if (!ranTheGauntlet)
throw new RuntimeException("A visit method did not throw NPE");
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册