Skip to content

Commit 509395d

Browse files
committed
Fix SpotBugs error: Class
org.apache.commons.io.filefilter.RegexFileFilter defines non-transient non-serializable instance field pathToString [org.apache.commons.io.filefilter.RegexFileFilter] In RegexFileFilter.java SE_BAD_FIELD
1 parent 94d36ef commit 509395d

3 files changed

Lines changed: 13 additions & 13 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ The <action> type attribute can be add,update,fix,remove.
5555
<action dev="ggregory" type="fix" due-to="Gary Gregory">Fix some Javadoc issues in LineIterator and IOUtils.</action>
5656
<action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in RegexFileFilter.RegexFileFilter(Pattern).</action>
5757
<action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in RegexFileFilter.accept(Path, BasicFileAttributes).</action>
58+
<action dev="ggregory" type="fix" due-to="Gary Gregory">Fix SpotBugs error: Class org.apache.commons.io.filefilter.RegexFileFilter defines non-transient non-serializable instance field pathToString [org.apache.commons.io.filefilter.RegexFileFilter] In RegexFileFilter.java SE_BAD_FIELD.</action>
5859
<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>
5960
<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>
6061
<!-- UPDATE -->

src/main/java/org/apache/commons/io/filefilter/RegexFileFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private static int toFlags(final IOCase ioCase) {
101101
private final Pattern pattern;
102102

103103
/** How convert a path to a string. */
104-
private final Function<Path, String> pathToString;
104+
private transient final Function<Path, String> pathToString;
105105

106106
/**
107107
* Constructs a new regular expression filter for a compiled regular expression

src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTest.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,16 @@ public class RegexFileFilterTest {
4242

4343
public void assertFiltering(final IOFileFilter filter, final File file, final boolean expected) {
4444
// Note. This only tests the (File, String) version if the parent of
45-
// the File passed in is not null
46-
assertEquals(expected, filter.accept(file),
47-
"Filter(File) " + filter.getClass().getName() + " not " + expected + " for " + file);
45+
// the File passed in is not null
46+
assertEquals(expected, filter.accept(file), "Filter(File) " + filter.getClass().getName() + " not " + expected + " for " + file);
4847

4948
if (file != null && file.getParentFile() != null) {
5049
assertEquals(expected, filter.accept(file.getParentFile(), file.getName()),
5150
"Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for " + file);
52-
assertEquals(expected, filter.matches(file.toPath()),
53-
"Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for " + file);
51+
assertEquals(expected, filter.matches(file.toPath()), "Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for " + file);
5452
} else if (file == null) {
55-
assertEquals(expected, filter.accept(file),
56-
"Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for null");
57-
assertEquals(expected, filter.matches(null),
58-
"Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for null");
53+
assertEquals(expected, filter.accept(file), "Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for null");
54+
assertEquals(expected, filter.matches(null), "Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for null");
5955
}
6056
// Just don't blow up
6157
assertNotNull(filter.toString());
@@ -72,11 +68,10 @@ public void assertFiltering(final IOFileFilter filter, final Path path, final bo
7268

7369
if (path != null && path.getParent() != null) {
7470
assertEquals(expectedFileVisitResult, filter.accept(path, null),
75-
"Filter(Path, Path) " + filter.getClass().getName() + " not " + expectedFileVisitResult + " for "
76-
+ path);
71+
"Filter(Path, Path) " + filter.getClass().getName() + " not " + expectedFileVisitResult + " for " + path);
7772
} else if (path == null) {
7873
assertEquals(expectedFileVisitResult, filter.accept(path, null),
79-
"Filter(Path, Path) " + filter.getClass().getName() + " not " + expectedFileVisitResult + " for null");
74+
"Filter(Path, Path) " + filter.getClass().getName() + " not " + expectedFileVisitResult + " for null");
8075
}
8176
// Just don't blow up
8277
assertNotNull(filter.toString());
@@ -167,8 +162,12 @@ public void testRegexFileNameOnly() throws IOException {
167162
assertFiltering(assertSerializable(new RegexFileFilter(patternStr)), path, true);
168163
assertFiltering(assertSerializable(new RegexFileFilter(Pattern.compile(patternStr), (Function<Path, String> & Serializable) Path::toString)), path,
169164
false);
165+
//
170166
assertFiltering(new RegexFileFilter(Pattern.compile(patternStr), (Function<Path, String> & Serializable) null), path, false);
171167
assertFiltering(new RegexFileFilter(Pattern.compile(patternStr), (Function<Path, String> & Serializable) p -> null), path, false);
168+
//
169+
assertFiltering(assertSerializable(new RegexFileFilter(Pattern.compile(patternStr), (Function<Path, String> & Serializable) null)), path, false);
170+
assertFiltering(assertSerializable(new RegexFileFilter(Pattern.compile(patternStr), (Function<Path, String> & Serializable) p -> null)), path, false);
172171
}
173172

174173
}

0 commit comments

Comments
 (0)