Skip to content

Commit b02fbe1

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Detect constructors by type instead of by name.
...as suggested by typetools/checker-framework#7705. RELNOTES=n/a PiperOrigin-RevId: 911968656
1 parent 9a97bed commit b02fbe1

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

refactorings/src/main/java/com/google/common/truth/refactorings/CorrespondenceSubclassToFactoryCall.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
import java.util.Objects;
8383
import java.util.Optional;
8484
import java.util.Set;
85+
import javax.lang.model.element.ElementKind;
8586
import javax.lang.model.element.Modifier;
8687
import org.jspecify.annotations.Nullable;
8788

@@ -91,7 +92,6 @@
9192
* and used.
9293
*/
9394
@BugPattern(
94-
name = "CorrespondenceSubclassToFactoryCall",
9595
summary = "Use the factory methods on Correspondence instead of defining a subclass.",
9696
severity = SUGGESTION)
9797
public final class CorrespondenceSubclassToFactoryCall extends BugChecker
@@ -274,7 +274,7 @@ private String visibilityModifierOnConstructor(ClassTree tree) {
274274
tree.getMembers().stream()
275275
.filter(t -> t instanceof MethodTree)
276276
.map(t -> (MethodTree) t)
277-
.filter(t -> t.getName().contentEquals("<init>"))
277+
.filter(t -> getSymbol(t).getKind() == ElementKind.CONSTRUCTOR)
278278
.findAny()
279279
.get();
280280
// We don't include the ModifiersTree directly in case it contains any annotations.
@@ -638,7 +638,7 @@ static MemberType from(Tree tree, VisitorState state) {
638638
}
639639
MethodSymbol methodSymbol = (MethodSymbol) symbol;
640640

641-
if (methodSymbol.getSimpleName().contentEquals("<init>")) {
641+
if (methodSymbol.getKind() == ElementKind.CONSTRUCTOR) {
642642
return CONSTRUCTOR;
643643
} else if (overrides(methodSymbol, CORRESPONDENCE_CLASS, "compare", state)) {
644644
return COMPARE_METHOD;

0 commit comments

Comments
 (0)