Skip to content

Commit

Permalink
Improve handling of type parameter names that start with _
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 351412076
  • Loading branch information
cushon authored and Error Prone Team committed Jan 12, 2021
1 parent 99968c5 commit f7c5e6c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import static com.google.common.collect.ImmutableList.toImmutableList;

import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand All @@ -32,8 +32,9 @@ public class NamingConventions {
private static final String CASE_TRANSITION = "(?<=[a-z0-9])(?=[A-Z])";
private static final String TRAILING_DIGITS = "(?<![0-9_])(?=[0-9]+$)";

private static final Pattern TERM_SPLITTER =
Pattern.compile(String.format("%s|%s|%s", UNDERSCORE, CASE_TRANSITION, TRAILING_DIGITS));
private static final Splitter TERM_SPLITTER =
Splitter.onPattern(String.format("%s|%s|%s", UNDERSCORE, CASE_TRANSITION, TRAILING_DIGITS))
.omitEmptyStrings();

/**
* Split a Java name into terms based on either Camel Case or Underscores. We also split digits at
Expand All @@ -47,7 +48,8 @@ public static ImmutableList<String> splitToLowercaseTerms(String identifierName)
// Degenerate case of names which contain only underscore
return ImmutableList.of(identifierName);
}
return Arrays.stream(TERM_SPLITTER.split(identifierName))
return TERM_SPLITTER
.splitToStream(identifierName)
.map(String::toLowerCase)
.collect(toImmutableList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ private static List<TypeVariableSymbol> typeVariablesEnclosing(Symbol sym) {
}

private static String suggestedSingleLetter(String id, Tree tree) {
char firstLetter = id.charAt(0);
char firstLetter =
Ascii.toUpperCase(NamingConventions.splitToLowercaseTerms(id).get(0).charAt(0));
Symbol sym = ASTHelpers.getSymbol(tree);
List<TypeVariableSymbol> enclosingTypeSymbols = typeVariablesEnclosing(sym);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,25 @@ public void negativeCases_manyNumberedTypes() {
.doTest();
}

@Test
public void refactoring_underscore() {
refactoring
.addInputLines(
"in/Test.java", //
"class Test {",
" public <_T> void method(_T t) {",
" }",
"}")
.addOutputLines(
"in/Test.java", //
"class Test {",
" public <T> void method(T t) {",
" }",
"}")
.setFixChooser(FixChoosers.FIRST)
.doTest(TestMode.TEXT_MATCH);
}

@Test
public void classifyTypeName_singleLetter() {
assertKindOfName("T").isEqualTo(LETTER_WITH_MAYBE_NUMERAL);
Expand Down

0 comments on commit f7c5e6c

Please sign in to comment.