From be0161a8e63096f3a21ce6ea1e055ee1c4ed63ad Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Thu, 17 Jul 2025 06:31:34 +0000 Subject: [PATCH] 8362429: AssertionError in File.listFiles(FileFilter | FilenameFilter) Reviewed-by: alanb --- src/java.base/share/classes/java/io/File.java | 18 +++--- test/jdk/java/io/File/EmptyPath.java | 56 +++++++++++++++++-- 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/java.base/share/classes/java/io/File.java b/src/java.base/share/classes/java/io/File.java index 468d138657b..bb216366344 100644 --- a/src/java.base/share/classes/java/io/File.java +++ b/src/java.base/share/classes/java/io/File.java @@ -1134,15 +1134,9 @@ public class File if (ss == null) return null; int n = ss.length; File[] fs = new File[n]; - if (path.isEmpty()) { - for (int i = 0; i < n; i++) { - fs[i] = new File(ss[i]); - } - } else { - for (int i = 0; i < n; i++) { - fs[i] = new File(ss[i], this); - } - } + boolean isEmpty = path.isEmpty(); + for (int i = 0; i < n; i++) + fs[i] = isEmpty ? new File(ss[i]) : new File(ss[i], this); return fs; } @@ -1175,9 +1169,10 @@ public class File String[] ss = normalizedList(); if (ss == null) return null; ArrayList files = new ArrayList<>(); + boolean isEmpty = path.isEmpty(); for (String s : ss) if ((filter == null) || filter.accept(this, s)) - files.add(new File(s, this)); + files.add(isEmpty ? new File(s) : new File(s, this)); return files.toArray(new File[files.size()]); } @@ -1208,8 +1203,9 @@ public class File String[] ss = normalizedList(); if (ss == null) return null; ArrayList files = new ArrayList<>(); + boolean isEmpty = path.isEmpty(); for (String s : ss) { - File f = new File(s, this); + File f = isEmpty ? new File(s) : new File(s, this); if ((filter == null) || filter.accept(f)) files.add(f); } diff --git a/test/jdk/java/io/File/EmptyPath.java b/test/jdk/java/io/File/EmptyPath.java index d0c9beddc08..55c4af96fe3 100644 --- a/test/jdk/java/io/File/EmptyPath.java +++ b/test/jdk/java/io/File/EmptyPath.java @@ -22,15 +22,18 @@ */ /* @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; @@ -38,6 +41,7 @@ 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; @@ -211,7 +215,15 @@ public class EmptyPath { @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 ioSet = new HashSet(Arrays.asList(files)); Set nioSet = new HashSet(); @@ -221,11 +233,42 @@ public class EmptyPath { @Test public void listFiles() throws IOException { - File child = new File(f.getAbsoluteFile(), "child"); + listFiles(x -> x.listFiles()); + } + + @Test + public void listFilesFileFilter() throws IOException { + FileFilter ff = new FileFilter() { + public boolean accept(File pathname) { return true; } + }; + listFiles(x -> x.listFiles(ff)); + } + + @Test + public void listFilesNullFileFilter() throws IOException { + listFiles(x -> x.listFiles((FileFilter)null)); + } + + @Test + public void listFilesFilenameFilter() throws IOException { + FilenameFilter fnf = new FilenameFilter() { + public boolean accept(File dir, String name) { return true; } + }; + listFiles(x -> x.listFiles(fnf)); + } + + @Test + public void listFilesNullFilenameFilter() throws IOException { + listFiles(x -> x.listFiles((FilenameFilter)null)); + } + + private void listFiles(Function 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)); @@ -348,4 +391,9 @@ public class EmptyPath { public void toURI() { assertEquals(f.toPath().toUri(), f.toURI()); } + + @Test + public void toURL() throws MalformedURLException { + assertEquals(f.toPath().toUri().toURL(), f.toURL()); + } }