-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement error-prone ObjectsHashCodeUnnecessaryVarargs migration (#1522
- Loading branch information
1 parent
55df9b4
commit 3a3eedc
Showing
6 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...one/src/main/java/com/palantir/baseline/errorprone/ObjectsHashCodeUnnecessaryVarargs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.errorprone; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.BugPattern.LinkType; | ||
import com.google.errorprone.BugPattern.SeverityLevel; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.fixes.SuggestedFixes; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.google.errorprone.matchers.Matchers; | ||
import com.google.errorprone.matchers.method.MethodMatchers; | ||
import com.sun.source.tree.ExpressionTree; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
|
||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
name = "ObjectsHashCodeUnnecessaryVarargs", | ||
link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks", | ||
linkType = LinkType.CUSTOM, | ||
severity = SeverityLevel.WARNING, | ||
summary = "java.util.Objects.hash(non-varargs) should be replaced with java.util.Objects.hashCode(value) " | ||
+ "to avoid unnecessary varargs array allocations.") | ||
public final class ObjectsHashCodeUnnecessaryVarargs extends BugChecker | ||
implements BugChecker.MethodInvocationTreeMatcher { | ||
|
||
private static final Matcher<ExpressionTree> HASH_MATCHER = | ||
MethodMatchers.staticMethod().onClass("java.util.Objects").named("hash"); | ||
|
||
private static final Matcher<ExpressionTree> ARRAY_MATCHER = Matchers.isArrayType(); | ||
|
||
@Override | ||
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { | ||
if (HASH_MATCHER.matches(tree, state) && tree.getArguments().size() == 1) { | ||
ExpressionTree argument = tree.getArguments().get(0); | ||
if (!ARRAY_MATCHER.matches(argument, state)) { | ||
return buildDescription(tree) | ||
.addFix(SuggestedFixes.renameMethodInvocation(tree, "hashCode", state)) | ||
.build(); | ||
} | ||
} | ||
return Description.NO_MATCH; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...src/test/java/com/palantir/baseline/errorprone/ObjectsHashCodeUnnecessaryVarargsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.errorprone; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ObjectsHashCodeUnnecessaryVarargsTest { | ||
|
||
@Test | ||
public void test() { | ||
fix().addInputLines( | ||
"Test.java", | ||
"import java.util.Objects;", | ||
"public class Test {", | ||
" void f(Object a, Object[] b, String[] c, String d) {", | ||
" Objects.hash(a);", | ||
" Objects.hash(a, a);", | ||
" Objects.hash(b);", | ||
" Objects.hash(c);", | ||
" Objects.hash(d);", | ||
" }", | ||
"}") | ||
.addOutputLines( | ||
"Test.java", | ||
"import java.util.Objects;", | ||
"public class Test {", | ||
" void f(Object a, Object[] b, String[] c, String d) {", | ||
" Objects.hashCode(a);", | ||
" Objects.hash(a, a);", | ||
" Objects.hash(b);", | ||
" // BUG: Diagnostic contains: non-varargs call of varargs method", | ||
" Objects.hash(c);", | ||
" Objects.hashCode(d);", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
private RefactoringValidator fix() { | ||
return RefactoringValidator.of(new ObjectsHashCodeUnnecessaryVarargs(), getClass()); | ||
} | ||
|
||
public CompilationTestHelper helper() { | ||
return CompilationTestHelper.newInstance(ObjectsHashCodeUnnecessaryVarargs.class, getClass()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: improvement | ||
improvement: | ||
description: Implement error-prone ObjectsHashCodeUnnecessaryVarargs migration | ||
links: | ||
- https://github.com/palantir/gradle-baseline/pull/1522 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters