Skip to content

Commit

Permalink
Avoid NullPointerException in RegexFileFilter.RegexFileFilter(Pattern).
Browse files Browse the repository at this point in the history
Avoid NullPointerException in RegexFileFilter.accept(Path,
BasicFileAttributes)
  • Loading branch information
garydgregory committed Nov 23, 2023
1 parent 512c183 commit 94d36ef
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="fix" due-to="Stephan Markwalder, Gary Gregory">Add test for FileChannels.contentEquals() #509.</action>
<action dev="ggregory" type="fix" due-to="Gary Gregory">Fix FileChannels.contentEquals().</action>
<action dev="ggregory" type="fix" due-to="Gary Gregory">Fix some Javadoc issues in LineIterator and IOUtils.</action>
<action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in RegexFileFilter.RegexFileFilter(Pattern).</action>
<action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in RegexFileFilter.accept(Path, BasicFileAttributes).</action>
<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>
<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>
<!-- UPDATE -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private static int toFlags(final IOCase ioCase) {
*/
@SuppressWarnings("unchecked")
public RegexFileFilter(final Pattern pattern) {
this(pattern, (Function<Path, String> & Serializable) p -> p.getFileName().toString());
this(pattern, (Function<Path, String> & Serializable) p -> Objects.toString(p.getFileName(), null));
}

/**
Expand All @@ -125,7 +125,7 @@ public RegexFileFilter(final Pattern pattern) {
public RegexFileFilter(final Pattern pattern, final Function<Path, String> pathToString) {
Objects.requireNonNull(pattern, "pattern");
this.pattern = pattern;
this.pathToString = pathToString;
this.pathToString = pathToString != null ? pathToString : Objects::toString;
}

/**
Expand Down Expand Up @@ -181,7 +181,8 @@ public boolean accept(final File dir, final String name) {
*/
@Override
public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
return toFileVisitResult(pattern.matcher(pathToString.apply(path)).matches());
final String result = pathToString.apply(path);
return toFileVisitResult(result != null && pattern.matcher(result).matches());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ public void testRegexFileNameOnly() throws IOException {
final String patternStr = "Foo.*";
assertFiltering(assertSerializable(new RegexFileFilter(patternStr)), path, true);
assertFiltering(assertSerializable(new RegexFileFilter(Pattern.compile(patternStr), (Function<Path, String> & Serializable) Path::toString)), path,
false);
false);
assertFiltering(new RegexFileFilter(Pattern.compile(patternStr), (Function<Path, String> & Serializable) null), path, false);
assertFiltering(new RegexFileFilter(Pattern.compile(patternStr), (Function<Path, String> & Serializable) p -> null), path, false);
}

}

0 comments on commit 94d36ef

Please sign in to comment.