Skip to content
Open
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 @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down