Skip to content

Commit 19dddd8

Browse files
klueverError Prone Team
authored andcommitted
PUBLIC: Flag obsolete Scanner constructors in JdkObsolete.
RELNOTES=n/a PiperOrigin-RevId: 864840303
1 parent ab4e7bd commit 19dddd8

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

core/src/main/java/com/google/errorprone/bugpatterns/JdkObsolete.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,26 @@ private record ObsoleteApi(Matcher<ExpressionTree> matcher, String message) {}
199199

200200
private static final ImmutableList<ObsoleteApi> OBSOLETE_CONSTRUCTORS =
201201
ImmutableList.of(
202+
new ObsoleteApi(
203+
constructor()
204+
.forClass("java.util.Scanner")
205+
.withParameters("java.io.InputStream", "java.lang.String"),
206+
"Use new Scanner(InputStream, Charset) instead."),
207+
new ObsoleteApi(
208+
constructor()
209+
.forClass("java.util.Scanner")
210+
.withParameters("java.io.File", "java.lang.String"),
211+
"Use new Scanner(File, Charset) instead."),
212+
new ObsoleteApi(
213+
constructor()
214+
.forClass("java.util.Scanner")
215+
.withParameters("java.nio.file.Path", "java.lang.String"),
216+
"Use new Scanner(Path, Charset) instead."),
217+
new ObsoleteApi(
218+
constructor()
219+
.forClass("java.util.Scanner")
220+
.withParameters("java.nio.channels.ReadableByteChannel", "java.lang.String"),
221+
"Use new Scanner(ReadableByteChannel, Charset) instead."),
202222
new ObsoleteApi(
203223
constructor()
204224
.forClass("java.lang.String")

core/src/test/java/com/google/errorprone/bugpatterns/JdkObsoleteTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ public void preferCharsetAcceptingApis() {
411411
import java.io.*;
412412
import java.net.*;
413413
import java.nio.channels.*;
414+
import java.nio.file.Path;
414415
import java.util.*;
415416
416417
class Test {
@@ -495,6 +496,18 @@ void properties(OutputStream os) throws Exception {
495496
// BUG: Diagnostic contains: Properties.storeToXML(OutputStream, String, Charset)
496497
new Properties().storeToXML(os, "comment", UTF8_NAME);
497498
}
499+
500+
void scanner(InputStream is, String fileName, File file, Path path, ReadableByteChannel rbc)
501+
throws Exception {
502+
// BUG: Diagnostic contains: new Scanner(InputStream, Charset)
503+
new Scanner(is, UTF8_NAME);
504+
// BUG: Diagnostic contains: new Scanner(File, Charset)
505+
new Scanner(file, UTF8_NAME);
506+
// BUG: Diagnostic contains: new Scanner(Path, Charset)
507+
new Scanner(path, UTF8_NAME);
508+
// BUG: Diagnostic contains: new Scanner(ReadableByteChannel, Charset)
509+
new Scanner(rbc, UTF8_NAME);
510+
}
498511
}
499512
""")
500513
.doTest();

0 commit comments

Comments
 (0)