Skip to content

8362429: AssertionError in File.listFiles(FileFilter | FilenameFilter) #26353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/java.base/share/classes/java/io/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -1175,9 +1175,15 @@ public File[] listFiles(FilenameFilter filter) {
String[] ss = normalizedList();
if (ss == null) return null;
ArrayList<File> files = new ArrayList<>();
for (String s : ss)
if ((filter == null) || filter.accept(this, s))
files.add(new File(s, this));
if (path.isEmpty()) {
for (String s : ss)
if ((filter == null) || filter.accept(this, s))
files.add(new File(s));
} else {
for (String s : ss)
if ((filter == null) || filter.accept(this, s))
files.add(new File(s, this));
}
return files.toArray(new File[files.size()]);
}

Expand Down Expand Up @@ -1208,10 +1214,18 @@ public File[] listFiles(FileFilter filter) {
String[] ss = normalizedList();
if (ss == null) return null;
ArrayList<File> files = new ArrayList<>();
for (String s : ss) {
File f = new File(s, this);
if ((filter == null) || filter.accept(f))
files.add(f);
if (path.isEmpty()) {
for (String s : ss) {
File f = new File(s);
if ((filter == null) || filter.accept(f))
files.add(f);
}
} else {
for (String s : ss) {
File f = new File(s, this);
if ((filter == null) || filter.accept(f))
files.add(f);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than duplicating the loop, it could be changed to introduce boolean isEmpty = path.isEmpty and then the f can be created with File f = isEmpty ? new File(s) : new File(this, s); It should be a bit cleaner.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had rejected that in the case of the parameter-less listFiles to avoid a ternary operator in each loop iteration but it is cleaner. Will update.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So changed in cfd494f.

}
}
return files.toArray(new File[files.size()]);
}
Expand Down
40 changes: 36 additions & 4 deletions test/jdk/java/io/File/EmptyPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,26 @@
*/

/* @test
* @bug 4842706 8024695 8361587
* @bug 4842706 8024695 8361587 8362429
* @summary Test some file operations with empty path
* @run junit EmptyPath
*/

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.FileStore;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -211,7 +215,15 @@ public void length() throws IOException {

@Test
public void list() throws IOException {
String[] files = f.list();
list(f.list());
}

@Test
public void listFilenameFilter() throws IOException {
list(f.list((FilenameFilter)null));
}

private void list(String[] files) throws IOException {
assertNotNull(files);
Set<String> ioSet = new HashSet(Arrays.asList(files));
Set<String> nioSet = new HashSet();
Expand All @@ -221,11 +233,26 @@ public void list() throws IOException {

@Test
public void listFiles() throws IOException {
File child = new File(f.getAbsoluteFile(), "child");
listFiles(x -> x.listFiles());
}

@Test
public void listFilesFileFilter() throws IOException {
listFiles(x -> x.listFiles((FileFilter)null));
}

@Test
public void listFilesFilenameFilter() throws IOException {
listFiles(x -> x.listFiles((FilenameFilter)null));
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you think about test a non-null filter too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you think about test a non-null filter too?

Okay.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add listFiles tests for non-null FileFilter and FilenameFilter in cfd494f.

private void listFiles(Function<File,File[]> func) throws IOException {
String childName = "child" + System.nanoTime();
File child = new File(f.getAbsoluteFile(), childName);
assertTrue(child.createNewFile());
child.deleteOnExit();

File[] files = f.listFiles();
File[] files = func.apply(f);
for (File file : files)
assertEquals(-1, f.toString().indexOf(File.separatorChar));

Expand Down Expand Up @@ -348,4 +375,9 @@ public String toString() {
public void toURI() {
assertEquals(f.toPath().toUri(), f.toURI());
}

@Test
public void toURL() throws MalformedURLException {
assertEquals(f.toPath().toUri().toURL(), f.toURL());
}
}