Skip to content

Commit

Permalink
Support for java.nio.file.Files from 1.8 JDK (OS_OPEN_STREAM) and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveeclipse committed Jul 15, 2015
1 parent 1d82e81 commit c41242f
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ public final class FindOpenStream extends ResourceTrackingDetector<Stream, Strea
streamFactoryCollection.add(new MethodReturnValueStreamFactory("java.nio.file.Files", "newBufferedWriter",
"(Ljava/nio/file/Path;Ljava/nio/charset/Charset;[Ljava/nio/file/OpenOption;)Ljava/io/BufferedWriter;", "OS_OPEN_STREAM"));

// java 8
streamFactoryCollection.add(new MethodReturnValueStreamFactory("java.nio.file.Files", "newBufferedReader",
"(Ljava/nio/file/Path;)Ljava/io/BufferedReader;", "OS_OPEN_STREAM"));
streamFactoryCollection.add(new MethodReturnValueStreamFactory("java.nio.file.Files", "newBufferedWriter",
"(Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/BufferedWriter;", "OS_OPEN_STREAM"));

// Ignore socket input and output streams
streamFactoryCollection.add(new MethodReturnValueStreamFactory("java.net.Socket", "getInputStream",
"()Ljava/io/InputStream;"));
Expand Down
78 changes: 78 additions & 0 deletions findbugsTestCases/src/java/sfBugsNew/Bug1399.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package sfBugsNew;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.SeekableByteChannel;
import java.nio.charset.Charset;
import java.nio.file.DirectoryStream;
import java.nio.file.DirectoryStream.Filter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Collection;
import java.util.Collections;

import edu.umd.cs.findbugs.annotations.ExpectWarning;

public class Bug1399 {

@ExpectWarning("OS_OPEN_STREAM")
void test1(File f, byte b) throws IOException {
InputStream stream = Files.newInputStream(Paths.get(""));
stream.read();
}

@ExpectWarning("OS_OPEN_STREAM")
void test2(File f, byte b) throws IOException {
OutputStream stream = Files.newOutputStream(Paths.get(""));
stream.write(0);
}

@ExpectWarning("OS_OPEN_STREAM")
void test3(File f, byte b) throws IOException {
SeekableByteChannel c = Files.newByteChannel(Paths.get(""));
c.position();
}

@ExpectWarning("OS_OPEN_STREAM")
void test4(File f, byte b) throws IOException {
SeekableByteChannel c = Files.newByteChannel(Paths.get(""), StandardOpenOption.APPEND);
c.position();
}

@ExpectWarning("OS_OPEN_STREAM")
void test5(File f, byte b) throws IOException {
DirectoryStream<Path> c = Files.newDirectoryStream(Paths.get(""));
c.hashCode();
}

@ExpectWarning("OS_OPEN_STREAM")
void test6(File f, byte b) throws IOException {
DirectoryStream<Path> c = Files.newDirectoryStream(Paths.get(""), (Filter)null);
c.hashCode();
}

@ExpectWarning("OS_OPEN_STREAM")
void test7(File f, byte b) throws IOException {
DirectoryStream<Path> c = Files.newDirectoryStream(Paths.get(""), "");
c.hashCode();
}

@ExpectWarning("OS_OPEN_STREAM")
void test8(File f, byte b) throws IOException {
BufferedReader c = Files.newBufferedReader(Paths.get(""), Charset.defaultCharset());
c.hashCode();
}

@ExpectWarning("OS_OPEN_STREAM")
void test9(File f, byte b) throws IOException {
BufferedWriter c = Files.newBufferedWriter(Paths.get(""), Charset.defaultCharset());
c.hashCode();
}
}
35 changes: 35 additions & 0 deletions findbugsTestCases/src/java/sfBugsNew/Bug1399jdk8.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package sfBugsNew;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.SeekableByteChannel;
import java.nio.charset.Charset;
import java.nio.file.DirectoryStream;
import java.nio.file.DirectoryStream.Filter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;

import edu.umd.cs.findbugs.annotations.ExpectWarning;

public class Bug1399jdk8 {

@ExpectWarning("OS_OPEN_STREAM")
void test1(File f, byte b) throws IOException {
BufferedReader c = Files.newBufferedReader(Paths.get(""));
c.hashCode();
}

@ExpectWarning("OS_OPEN_STREAM")
void test2(File f, byte b) throws IOException {
BufferedWriter c = Files.newBufferedWriter(Paths.get(""));
c.hashCode();
}
}

0 comments on commit c41242f

Please sign in to comment.