diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceof.java b/core/src/main/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceof.java index aae89f12d4b..529199e5fa2 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceof.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceof.java @@ -16,7 +16,6 @@ package com.google.errorprone.bugpatterns; -import static com.google.common.base.Ascii.toLowerCase; import static com.google.common.collect.Streams.stream; import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; import static com.google.errorprone.fixes.SuggestedFix.mergeFixes; @@ -53,6 +52,7 @@ import com.sun.source.util.TreePathScanner; import com.sun.tools.javac.code.Type; import com.sun.tools.javac.code.TypeTag; + import java.util.HashSet; import java.util.stream.Stream; import javax.inject.Inject; @@ -162,11 +162,20 @@ public Description matchInstanceOf(InstanceOfTree instanceOfTree, VisitorState s private static String generateVariableName(Type targetType, VisitorState state) { Type unboxed = state.getTypes().unboxedType(targetType); String simpleName = targetType.tsym.getSimpleName().toString(); - String lowerFirstLetter = toLowerCase(String.valueOf(simpleName.charAt(0))); - String camelCased = lowerFirstLetter + simpleName.substring(1); + char[] chars = simpleName.toCharArray(); + boolean priorCharWasUpper = Character.isUpperCase(chars[0]);; + chars[0] = Character.toLowerCase(chars[0]); + for (int i = 1; i < chars.length - 1; i++) { + boolean currentCharIsUpper = Character.isUpperCase(chars[i]); + if (currentCharIsUpper && priorCharWasUpper && Character.isUpperCase(chars[i + 1])) { + chars[i] = Character.toLowerCase(chars[i]); + } + priorCharWasUpper = currentCharIsUpper; + } + String camelCased = new String(chars); if (SourceVersion.isKeyword(camelCased) || (unboxed != null && unboxed.getTag() != TypeTag.NONE)) { - return lowerFirstLetter; + return Character.toString(Character.toLowerCase(simpleName.charAt(0))); } return camelCased; } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceofTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceofTest.java index c03cec68cfc..14f4f8a156a 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceofTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceofTest.java @@ -500,6 +500,44 @@ void test(Object o) { .doTest(); } + @Test + public void checkStyleCompliantName() { + helper + .addInputLines( + "Class.java", + """ + import java.io.InterruptedIOException; + import java.sql.SQLException; + + class Class { + void test(Object e) { + if (e instanceof InterruptedIOException) { + test((InterruptedIOException) e); + } else if (e instanceof SQLException) { + test((SQLException) e); + } + } + } + """) + .addOutputLines( + "Class.java", + """ + import java.io.InterruptedIOException; + import java.sql.SQLException; + + class Class { + void test(Object e) { + if (e instanceof InterruptedIOException interruptedIoException) { + test(interruptedIoException); + } else if (e instanceof SQLException sqlException) { + test(sqlException); + } + } + } + """) + .doTest(); + } + @Test public void recordPatternMatching() { assume().that(Runtime.version().feature()).isAtLeast(21);