Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain camelCasing when translating from a field name to a method name in UnnecessaryLambda. #4761

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading