Skip to content

Commit 47dd2a8

Browse files
cushonError Prone Team
authored andcommitted
Suggest Splitter.on(Pattern.compile(...)) instead of Splitter.onPattern
onPattern is now deprecated. PiperOrigin-RevId: 678868087
1 parent 50d0983 commit 47dd2a8

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,25 +226,26 @@ public Boolean visitMemberSelect(MemberSelectTree tree, Void unused) {
226226
return Optional.of(fix.build());
227227
}
228228

229-
private static String getMethodAndArgument(Tree origArg, VisitorState state) {
229+
private static String getMethodAndArgument(
230+
SuggestedFix.Builder fix, Tree origArg, VisitorState state) {
230231
String argSource = state.getSourceForNode(origArg);
231232
Tree arg = ASTHelpers.stripParentheses(origArg);
232233
if (arg.getKind() != Tree.Kind.STRING_LITERAL) {
233234
// Even if the regex is a constant, it still needs to be treated as a regex, since the
234235
// value comes from the symbol and/or a concatenation; the values of the subexpressions may be
235236
// changed subsequently.
236-
return String.format("onPattern(%s)", argSource);
237+
return onPattern(fix, argSource);
237238
}
238239
String constValue = ASTHelpers.constValue(arg, String.class);
239240
if (constValue == null) {
240241
// Not a constant value, so we can't assume anything about pattern: have to treat it as a
241242
// regex.
242-
return String.format("onPattern(%s)", argSource);
243+
return onPattern(fix, argSource);
243244
}
244245
Optional<String> regexAsLiteral = convertRegexToLiteral(constValue);
245246
if (!regexAsLiteral.isPresent()) {
246247
// Can't convert the regex to a literal string: have to treat it as a regex.
247-
return String.format("onPattern(%s)", argSource);
248+
return onPattern(fix, argSource);
248249
}
249250
String escaped = SourceCodeEscapers.javaCharEscaper().escape(regexAsLiteral.get());
250251
if (regexAsLiteral.get().length() == 1) {
@@ -253,6 +254,11 @@ private static String getMethodAndArgument(Tree origArg, VisitorState state) {
253254
return String.format("on(\"%s\")", escaped);
254255
}
255256

257+
private static String onPattern(SuggestedFix.Builder fix, String argSource) {
258+
fix.addImport("java.util.regex.Pattern");
259+
return String.format("on(Pattern.compile(%s))", argSource);
260+
}
261+
256262
private static SuggestedFix.Builder replaceWithSplitter(
257263
SuggestedFix.Builder fix,
258264
MethodInvocationTree tree,
@@ -267,7 +273,7 @@ private static SuggestedFix.Builder replaceWithSplitter(
267273
fix.addImport("com.google.common.base.Splitter");
268274
Type receiverType = getType(receiver);
269275
if (isSubtype(receiverType, state.getSymtab().stringType, state)) {
270-
String methodAndArgument = getMethodAndArgument(arg, state);
276+
String methodAndArgument = getMethodAndArgument(fix, arg, state);
271277
return fix.prefixWith(
272278
receiver,
273279
String.format(

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

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,24 @@ void f() {
105105
.addOutputLines(
106106
"Test.java",
107107
"""
108-
import com.google.common.base.Splitter;
108+
import com.google.common.base.Splitter;
109+
import java.util.regex.Pattern;
109110
110-
class Test {
111-
static final String NON_REGEX_PATTERN_STRING = ":";
112-
static final String REGEX_PATTERN_STRING = ".*";
113-
static final String CONVERTIBLE_PATTERN_STRING = "\\\\Q\\\\E:";
111+
class Test {
112+
static final String NON_REGEX_PATTERN_STRING = ":";
113+
static final String REGEX_PATTERN_STRING = ".*";
114+
static final String CONVERTIBLE_PATTERN_STRING = "\\\\Q\\\\E:";
114115
115-
void f() {
116-
for (String s : Splitter.onPattern(NON_REGEX_PATTERN_STRING).split("")) {}
117-
for (String s : Splitter.onPattern(REGEX_PATTERN_STRING).split("")) {}
118-
for (String s : Splitter.onPattern(CONVERTIBLE_PATTERN_STRING).split("")) {}
119-
for (String s : Splitter.onPattern((CONVERTIBLE_PATTERN_STRING)).split("")) {}
120-
}
121-
}
122-
""")
116+
void f() {
117+
for (String s : Splitter.on(Pattern.compile(NON_REGEX_PATTERN_STRING)).split("")) {}
118+
for (String s : Splitter.on(Pattern.compile(REGEX_PATTERN_STRING)).split("")) {}
119+
for (String s :
120+
Splitter.on(Pattern.compile(CONVERTIBLE_PATTERN_STRING)).split("")) {}
121+
for (String s :
122+
Splitter.on(Pattern.compile((CONVERTIBLE_PATTERN_STRING))).split("")) {}
123+
}
124+
}
125+
""")
123126
.doTest(TestMode.TEXT_MATCH);
124127
}
125128

@@ -139,10 +142,11 @@ void f() {
139142
"Test.java",
140143
"""
141144
import com.google.common.base.Splitter;
145+
import java.util.regex.Pattern;
142146
143147
class Test {
144148
void f() {
145-
for (String s : Splitter.onPattern(":" + 0).split("")) {}
149+
for (String s : Splitter.on(Pattern.compile(":" + 0)).split("")) {}
146150
}
147151
}
148152
""")
@@ -166,11 +170,12 @@ void f() {
166170
"Test.java",
167171
"""
168172
import com.google.common.base.Splitter;
173+
import java.util.regex.Pattern;
169174
170175
class Test {
171176
void f() {
172177
String pattern = ":";
173-
for (String s : Splitter.onPattern(pattern).split("")) {}
178+
for (String s : Splitter.on(Pattern.compile(pattern)).split("")) {}
174179
}
175180
}
176181
""")
@@ -307,10 +312,11 @@ void f() {
307312
"Test.java",
308313
"""
309314
import com.google.common.base.Splitter;
315+
import java.util.regex.Pattern;
310316
311317
class Test {
312318
void f() {
313-
for (String s : Splitter.onPattern(".*foo\\\\t").split("")) {}
319+
for (String s : Splitter.on(Pattern.compile(".*foo\\\\t")).split("")) {}
314320
}
315321
}
316322
""")
@@ -408,16 +414,17 @@ void f(String input) {
408414
.addOutputLines(
409415
"Test.java",
410416
"""
411-
import com.google.common.base.Splitter;
412-
import java.util.List;
417+
import com.google.common.base.Splitter;
418+
import java.util.List;
419+
import java.util.regex.Pattern;
413420
414-
class Test {
415-
void f(String input) {
416-
List<String> lines = Splitter.onPattern("\\\\r?\\\\n").splitToList(input);
417-
System.err.println(lines.get(0));
418-
}
419-
}
420-
""")
421+
class Test {
422+
void f(String input) {
423+
List<String> lines = Splitter.on(Pattern.compile("\\\\r?\\\\n")).splitToList(input);
424+
System.err.println(lines.get(0));
425+
}
426+
}
427+
""")
421428
.doTest();
422429
}
423430

0 commit comments

Comments
 (0)