Skip to content

Commit

Permalink
Retain camelCasing when translating from a field name to a method n…
Browse files Browse the repository at this point in the history
…ame in `UnnecessaryLambda`.

PiperOrigin-RevId: 713776818
  • Loading branch information
kluever authored and Error Prone Team committed Jan 9, 2025
1 parent cfd1f79 commit 921189c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import static com.google.common.base.CaseFormat.LOWER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
import static com.google.common.base.CharMatcher.inRange;
import static com.google.common.base.CharMatcher.is;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.fixes.SuggestedFixes.prettyType;
Expand All @@ -32,6 +34,7 @@
import static com.sun.tools.javac.util.Position.NOPOS;
import static java.util.stream.Collectors.joining;

import com.google.common.base.CharMatcher;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import com.google.errorprone.BugPattern;
Expand Down Expand Up @@ -126,6 +129,8 @@ public Void visitMethodInvocation(MethodInvocationTree node, Void unused) {
return describeMatch(tree, fix.build());
}

private static final CharMatcher UPPER_CASE = inRange('A', 'Z').or(is('_')).or(inRange('0', '9'));

@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
if (tree.getInitializer() == null) {
Expand All @@ -152,29 +157,32 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
return NO_MATCH;
}
SuggestedFix.Builder fix = SuggestedFix.builder();
String name =
isStatic(sym)
? UPPER_UNDERSCORE.converterTo(LOWER_CAMEL).convert(tree.getName().toString())
: tree.getName().toString();
String varName = tree.getName().toString();
// NOTE: if https://github.com/google/guava/issues/2212 gets resolved, we could use it here.
String methodName =
(isStatic(sym) && UPPER_CASE.matchesAllOf(varName))
? UPPER_UNDERSCORE.converterTo(LOWER_CAMEL).convert(varName)
: varName;

new TreePathScanner<Void, Void>() {
@Override
public Void visitMemberSelect(MemberSelectTree node, Void unused) {
if (Objects.equals(getSymbol(node), sym)) {
replaceUseWithMethodReference(fix, node, name, state.withPath(getCurrentPath()));
replaceUseWithMethodReference(fix, node, methodName, state.withPath(getCurrentPath()));
}
return super.visitMemberSelect(node, null);
}

@Override
public Void visitIdentifier(IdentifierTree node, Void unused) {
if (Objects.equals(getSymbol(node), sym)) {
replaceUseWithMethodReference(fix, node, name, state.withPath(getCurrentPath()));
replaceUseWithMethodReference(fix, node, methodName, state.withPath(getCurrentPath()));
}
return super.visitIdentifier(node, null);
}
}.scan(state.getPath().getCompilationUnit(), null);
SuggestedFixes.removeModifiers(tree, state, Modifier.FINAL).ifPresent(fix::merge);
lambdaToMethod(state, lambda, fix, name, type);
lambdaToMethod(state, lambda, fix, methodName, type);
return describeMatch(tree, fix.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,20 +277,19 @@ void g() {
}
}
""")
// TODO: b/388821905 - we should retain the camelCasing of the variable name
.addOutputLines(
"Test.java",
"""
import java.util.function.Function;
class Test {
private static String notuppercased(String x) {
private static String notUpperCased(String x) {
return "hello " + x;
}
void g() {
Function<String, String> l = Test::notuppercased;
System.err.println(notuppercased("world"));
Function<String, String> l = Test::notUpperCased;
System.err.println(notUpperCased("world"));
}
}
""")
Expand Down

0 comments on commit 921189c

Please sign in to comment.