-
Notifications
You must be signed in to change notification settings - Fork 741
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suggest AutoValue classes to mark manually written equals, hashCode a…
…nd toString to be final RELNOTES: Suggest AutoValue classes to mark manually written equals, hashCode and toString to be final ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=204033438
- Loading branch information
1 parent
e8116bc
commit a5cb666
Showing
4 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
core/src/main/java/com/google/errorprone/bugpatterns/AutoValueFinalMethods.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,106 @@ | ||
/* | ||
* Copyright 2018 The Error Prone Authors. | ||
* | ||
* 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.google.errorprone.bugpatterns; | ||
|
||
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; | ||
import static com.google.errorprone.matchers.Description.NO_MATCH; | ||
import static com.google.errorprone.matchers.Matchers.allOf; | ||
import static com.google.errorprone.matchers.Matchers.anyOf; | ||
import static com.google.errorprone.matchers.Matchers.isSameType; | ||
import static com.google.errorprone.matchers.Matchers.methodHasParameters; | ||
import static com.google.errorprone.matchers.Matchers.methodIsNamed; | ||
import static com.google.errorprone.matchers.Matchers.methodReturns; | ||
import static com.google.errorprone.matchers.Matchers.not; | ||
import static com.google.errorprone.matchers.Matchers.variableType; | ||
import static com.google.errorprone.suppliers.Suppliers.BOOLEAN_TYPE; | ||
import static com.google.errorprone.suppliers.Suppliers.INT_TYPE; | ||
import static com.google.errorprone.suppliers.Suppliers.STRING_TYPE; | ||
|
||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.BugPattern.ProvidesFix; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher; | ||
import com.google.errorprone.fixes.SuggestedFix; | ||
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.util.ASTHelpers; | ||
import com.sun.source.tree.ClassTree; | ||
import com.sun.source.tree.MethodTree; | ||
import com.sun.source.tree.Tree; | ||
import java.util.Optional; | ||
import javax.lang.model.element.Modifier; | ||
|
||
/** | ||
* Checks the toString(), hashCode() and equals() methods are final in AutoValue classes. | ||
* | ||
* @author bhagwani@google.com (Sumit Bhagwani) | ||
*/ | ||
@BugPattern( | ||
name = "AutoValueFinalMethods", | ||
summary = | ||
"Make toString(), hashCode() and equals() final in AutoValue classes" | ||
+ ", so it is clear to readers that AutoValue is not overriding them", | ||
severity = WARNING, | ||
providesFix = ProvidesFix.REQUIRES_HUMAN_ATTENTION) | ||
public class AutoValueFinalMethods extends BugChecker implements ClassTreeMatcher { | ||
|
||
private static final Matcher<MethodTree> EQUALS_MATCHER = | ||
allOf( | ||
methodIsNamed("equals"), | ||
methodHasParameters(variableType(isSameType("java.lang.Object"))), | ||
methodReturns(BOOLEAN_TYPE)); | ||
|
||
private static final Matcher<MethodTree> TO_STRING_MATCHER = | ||
allOf(methodIsNamed("toString"), methodHasParameters(), methodReturns(STRING_TYPE)); | ||
|
||
private static final Matcher<MethodTree> HASH_CODE_MATCHER = | ||
allOf(methodIsNamed("hashCode"), methodHasParameters(), methodReturns(INT_TYPE)); | ||
|
||
// public non-final eq/ts/hs methods | ||
private static final Matcher<MethodTree> METHOD_MATCHER = | ||
allOf( | ||
Matchers.<MethodTree>hasModifier(Modifier.PUBLIC), | ||
not(Matchers.<MethodTree>hasModifier(Modifier.ABSTRACT)), | ||
not(Matchers.<MethodTree>hasModifier(Modifier.FINAL)), | ||
anyOf(EQUALS_MATCHER, TO_STRING_MATCHER, HASH_CODE_MATCHER)); | ||
|
||
@Override | ||
public Description matchClass(ClassTree tree, VisitorState state) { | ||
if (!ASTHelpers.hasAnnotation(tree, "com.google.auto.value.AutoValue", state)) { | ||
return NO_MATCH; | ||
} | ||
SuggestedFix.Builder fix = SuggestedFix.builder(); | ||
for (Tree memberTree : tree.getMembers()) { | ||
if (!(memberTree instanceof MethodTree)) { | ||
continue; | ||
} | ||
if (METHOD_MATCHER.matches((MethodTree) memberTree, state)) { | ||
Optional<SuggestedFix> optionalSuggestedFix = | ||
SuggestedFixes.addModifiers(memberTree, state, Modifier.FINAL); | ||
if (optionalSuggestedFix.isPresent()) { | ||
fix.merge(optionalSuggestedFix.get()); | ||
} | ||
} | ||
} | ||
if (!fix.isEmpty()) { | ||
return describeMatch(tree, fix.build()); | ||
} | ||
return NO_MATCH; | ||
} | ||
} |
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
125 changes: 125 additions & 0 deletions
125
core/src/test/java/com/google/errorprone/bugpatterns/AutoValueFinalMethodsTest.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,125 @@ | ||
/* | ||
* Copyright 2018 The Error Prone Authors. | ||
* | ||
* 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.google.errorprone.bugpatterns; | ||
|
||
import com.google.errorprone.BugCheckerRefactoringTestHelper; | ||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** | ||
* Unit tests for {@link AutoValueFinalMethods} bug pattern. | ||
* | ||
* @author bhagwani@google.com (Sumit Bhagwani) | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class AutoValueFinalMethodsTest { | ||
private final BugCheckerRefactoringTestHelper testHelper = | ||
BugCheckerRefactoringTestHelper.newInstance(new AutoValueFinalMethods(), getClass()); | ||
private final CompilationTestHelper compilationHelper = | ||
CompilationTestHelper.newInstance(AutoValueFinalMethods.class, getClass()); | ||
|
||
@Test | ||
public void testFinalAdditionToEqHcTs() throws Exception { | ||
testHelper | ||
.addInputLines( | ||
"in/Test.java", | ||
"import com.google.auto.value.AutoValue;", | ||
"@AutoValue", | ||
"abstract class Test {", | ||
" abstract String valueOne();", | ||
" abstract String valueTwo();", | ||
" static Test create(String valueOne, String valueTwo) {", | ||
" return null;", | ||
" }", | ||
" @Override", | ||
" public int hashCode() {", | ||
" return 1;", | ||
" }", | ||
" @Override", | ||
" public String toString() {", | ||
" return \"Hakuna Matata\";", | ||
" }", | ||
" @Override", | ||
" public boolean equals(Object obj) {", | ||
" return true;", | ||
" }", | ||
"}") | ||
.addOutputLines( | ||
"out/Test.java", | ||
"import com.google.auto.value.AutoValue;", | ||
"@AutoValue", | ||
"abstract class Test {", | ||
" abstract String valueOne();", | ||
" abstract String valueTwo();", | ||
" static Test create(String valueOne, String valueTwo) {", | ||
" return null;", | ||
" }", | ||
" @Override", | ||
" public final int hashCode() {", | ||
" return 1;", | ||
" }", | ||
" @Override", | ||
" public final String toString() {", | ||
" return \"Hakuna Matata\";", | ||
" }", | ||
" @Override", | ||
" public final boolean equals(Object obj) {", | ||
" return true;", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void testNegativeCases() throws Exception { | ||
compilationHelper | ||
.addSourceLines( | ||
"out/Test.java", | ||
"import com.google.auto.value.AutoValue;", | ||
"import com.google.auto.value.extension.memoized.Memoized;", | ||
"@AutoValue", | ||
"abstract class Test {", | ||
" abstract String valueOne();", | ||
" abstract String valueTwo();", | ||
" static Test create(String valueOne, String valueTwo) {", | ||
" return null;", | ||
" }", | ||
" @Override", | ||
" public abstract int hashCode(); ", | ||
" @Override", | ||
" public final String toString() {", | ||
" return \"Hakuna Matata\";", | ||
" }", | ||
" @Override", | ||
" public final boolean equals(Object obj) {", | ||
" return true;", | ||
" }", | ||
" private int privateNonEqTsHcMethod() {", | ||
" return 2;", | ||
" }", | ||
" public final String publicFinalNonEqTsHcMethod() {", | ||
" return \"Hakuna Matata\";", | ||
" }", | ||
" public boolean publicNonEqTsHcMethod(Object obj) {", | ||
" return true;", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
} |
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,7 @@ | ||
Consider that other developers will try to read and understand your value class | ||
while looking only at your hand-written class, not the actual (generated) | ||
implementation class. If you mark your concrete methods final, they won't have | ||
to wonder whether the generated subclass might be overriding them. This is | ||
especially helpful if you are underriding equals, hashCode or toString! | ||
|
||
Reference: https://github.com/google/auto/blob/master/value/userguide/practices.md#mark-all-concrete-methods-final |