2020import static com .google .errorprone .dataflow .nullnesspropagation .Nullness .NULL ;
2121import static com .google .errorprone .matchers .Matchers .instanceEqualsInvocation ;
2222import static com .google .errorprone .matchers .Matchers .staticEqualsInvocation ;
23- import static java .util .Arrays .stream ;
2423
2524import com .google .errorprone .VisitorState ;
2625import com .google .errorprone .bugpatterns .BugChecker .BinaryTreeMatcher ;
2726import com .google .errorprone .dataflow .nullnesspropagation .Nullness ;
2827import com .google .errorprone .fixes .Fix ;
2928import com .google .errorprone .fixes .SuggestedFix ;
29+ import com .google .errorprone .fixes .SuggestedFixes ;
3030import com .google .errorprone .matchers .Description ;
3131import com .google .errorprone .matchers .Matcher ;
3232import com .google .errorprone .util .ASTHelpers ;
33- import com .google .errorprone .util .FindIdentifiers ;
3433import com .sun .source .tree .BinaryTree ;
3534import com .sun .source .tree .ExpressionTree ;
3635import com .sun .source .tree .MethodInvocationTree ;
3736import com .sun .source .tree .Tree ;
3837import com .sun .source .tree .Tree .Kind ;
3938import com .sun .source .util .TreePath ;
40- import com .sun .tools .javac .code .Kinds .KindSelector ;
41- import com .sun .tools .javac .code .Symbol ;
42- import com .sun .tools .javac .code .Symbol .TypeSymbol ;
43- import com .sun .tools .javac .code .Type ;
44- import com .sun .tools .javac .util .Name ;
4539import java .util .List ;
4640import java .util .Optional ;
4741
@@ -84,26 +78,6 @@ public final Description matchBinary(BinaryTree tree, VisitorState state) {
8478 return builder .build ();
8579 }
8680
87- private static boolean symbolsTypeHasName (Symbol sym , String name ) {
88- if (sym == null ) {
89- return false ;
90- }
91- Type type = sym .type ;
92- if (type == null ) {
93- return false ;
94- }
95- TypeSymbol tsym = type .tsym ;
96- if (tsym == null ) {
97- return false ;
98- }
99- Name typeName = tsym .getQualifiedName ();
100- if (typeName == null ) {
101- // Probably shouldn't happen, but might as well check
102- return false ;
103- }
104- return typeName .contentEquals (name );
105- }
106-
10781 protected void addFixes (Description .Builder builder , BinaryTree tree , VisitorState state ) {
10882 ExpressionTree lhs = tree .getLeftOperand ();
10983 ExpressionTree rhs = tree .getRightOperand ();
@@ -129,13 +103,12 @@ protected void addFixes(Description.Builder builder, BinaryTree tree, VisitorSta
129103
130104 // If the lhs is possibly-null, provide both options.
131105 if (nullness != NONNULL ) {
132- Symbol existingObjects = FindIdentifiers .findIdent ("Objects" , state , KindSelector .TYP );
133- ObjectsFix preferredFix =
134- stream (ObjectsFix .values ())
135- .filter (f -> symbolsTypeHasName (existingObjects , f .className ))
136- .findFirst ()
137- .orElse (ObjectsFix .JAVA_UTIL );
138- builder .addFix (preferredFix .fix (tree , prefix , lhsSource , rhsSource ));
106+ SuggestedFix .Builder fix = SuggestedFix .builder ();
107+ String objects = SuggestedFixes .qualifyType (state , fix , "java.util.Objects" );
108+ builder .addFix (
109+ fix .replace (
110+ tree , String .format ("%s%s.equals(%s, %s)" , prefix , objects , lhsSource , rhsSource ))
111+ .build ());
139112 }
140113 if (nullness != NULL ) {
141114 builder .addFix (
@@ -149,26 +122,6 @@ protected void addFixes(Description.Builder builder, BinaryTree tree, VisitorSta
149122 }
150123 }
151124
152- private enum ObjectsFix {
153- JAVA_UTIL ("java.util.Objects" , "Objects.equals" ),
154- GUAVA ("com.google.common.base.Objects" , "Objects.equal" );
155-
156- private final String className ;
157- private final String methodName ;
158-
159- ObjectsFix (String className , String methodName ) {
160- this .className = className ;
161- this .methodName = methodName ;
162- }
163-
164- SuggestedFix fix (BinaryTree tree , String prefix , String lhsSource , String rhsSource ) {
165- return SuggestedFix .builder ()
166- .replace (tree , String .format ("%s%s(%s, %s)" , prefix , methodName , lhsSource , rhsSource ))
167- .addImport (className )
168- .build ();
169- }
170- }
171-
172125 private static Optional <Fix > inOrStatementWithEqualsCheck (VisitorState state , BinaryTree tree ) {
173126 // Only attempt to handle a == b || a.equals(b);
174127 if (tree .getKind () == Kind .NOT_EQUAL_TO ) {
0 commit comments