forked from google/error-prone
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it an error to initialise AutoValue.Builder defaults in construc…
…tors The automatic fix isn't perfect, but Flume: unknown commit PiperOrigin-RevId: 422385003
- Loading branch information
1 parent
5e6691c
commit 7821f59
Showing
3 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
...rc/main/java/com/google/errorprone/bugpatterns/AutoValueBuilderDefaultsInConstructor.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 2022 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.common.collect.ImmutableList.toImmutableList; | ||
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; | ||
import static com.google.errorprone.matchers.Description.NO_MATCH; | ||
import static com.google.errorprone.util.ASTHelpers.getSymbol; | ||
import static com.google.errorprone.util.ASTHelpers.getType; | ||
import static com.google.errorprone.util.ASTHelpers.hasAnnotation; | ||
import static com.google.errorprone.util.ASTHelpers.isSubtype; | ||
import static java.lang.String.join; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher; | ||
import com.google.errorprone.fixes.SuggestedFix; | ||
import com.google.errorprone.matchers.Description; | ||
import com.sun.source.tree.ExpressionStatementTree; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
import com.sun.source.tree.MethodTree; | ||
import com.sun.source.tree.NewClassTree; | ||
import com.sun.source.tree.StatementTree; | ||
import com.sun.source.util.TreePathScanner; | ||
import com.sun.tools.javac.code.Symbol; | ||
import com.sun.tools.javac.code.Symbol.MethodSymbol; | ||
import java.util.List; | ||
import javax.lang.model.element.Modifier; | ||
|
||
/** See summary for details. */ | ||
@BugPattern( | ||
name = "AutoValueBuilderDefaultsInConstructor", | ||
summary = | ||
"Defaults for AutoValue Builders should be set in the factory method returning Builder" | ||
+ " instances, not the constructor", | ||
severity = ERROR) | ||
public final class AutoValueBuilderDefaultsInConstructor extends BugChecker | ||
implements MethodTreeMatcher { | ||
|
||
@Override | ||
public Description matchMethod(MethodTree tree, VisitorState state) { | ||
MethodSymbol symbol = getSymbol(tree); | ||
if (!symbol.isConstructor()) { | ||
return NO_MATCH; | ||
} | ||
if (!hasAnnotation(symbol.owner, "com.google.auto.value.AutoValue.Builder", state)) { | ||
return NO_MATCH; | ||
} | ||
ImmutableList<String> invocations = | ||
extractInvocations(symbol, tree.getBody().getStatements(), state); | ||
if (invocations.isEmpty()) { | ||
return NO_MATCH; | ||
} | ||
|
||
return describeMatch(tree, appendDefaultsToConstructors(tree, symbol, invocations, state)); | ||
} | ||
|
||
private static SuggestedFix appendDefaultsToConstructors( | ||
MethodTree tree, MethodSymbol symbol, ImmutableList<String> invocations, VisitorState state) { | ||
SuggestedFix.Builder fix = SuggestedFix.builder().delete(tree); | ||
String defaultSetters = "." + join(".", invocations); | ||
|
||
new TreePathScanner<Void, Void>() { | ||
@Override | ||
public Void visitNewClass(NewClassTree tree, Void unused) { | ||
if (isSubtype(getType(tree), symbol.owner.type, state)) { | ||
fix.postfixWith(tree, defaultSetters); | ||
} | ||
return super.visitNewClass(tree, null); | ||
} | ||
}.scan(state.getPath().getCompilationUnit(), null); | ||
return fix.build(); | ||
} | ||
|
||
private static ImmutableList<String> extractInvocations( | ||
MethodSymbol symbol, List<? extends StatementTree> statements, VisitorState state) { | ||
return statements.stream() | ||
.filter(t -> t instanceof ExpressionStatementTree) | ||
.map(t -> (ExpressionStatementTree) t) | ||
.filter(t -> t.getExpression() instanceof MethodInvocationTree) | ||
.map(t -> (MethodInvocationTree) t.getExpression()) | ||
.filter( | ||
t -> { | ||
Symbol calledSymbol = getSymbol(t.getMethodSelect()); | ||
return calledSymbol.owner.equals(symbol.owner) | ||
&& calledSymbol.getModifiers().contains(Modifier.ABSTRACT); | ||
}) | ||
.map(t -> state.getSourceForNode(t).replaceFirst("^this\\.", "")) | ||
.collect(toImmutableList()); | ||
} | ||
} |
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
121 changes: 121 additions & 0 deletions
121
...est/java/com/google/errorprone/bugpatterns/AutoValueBuilderDefaultsInConstructorTest.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,121 @@ | ||
/* | ||
* Copyright 2022 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.auto.value.processor.AutoValueProcessor; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.errorprone.BugCheckerRefactoringTestHelper; | ||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Tests for {@link AutoValueBuilderDefaultsInConstructor}. */ | ||
@RunWith(JUnit4.class) | ||
public final class AutoValueBuilderDefaultsInConstructorTest { | ||
private final CompilationTestHelper helper = | ||
CompilationTestHelper.newInstance(AutoValueBuilderDefaultsInConstructor.class, getClass()); | ||
|
||
private final BugCheckerRefactoringTestHelper refactoringHelper = | ||
BugCheckerRefactoringTestHelper.newInstance( | ||
AutoValueBuilderDefaultsInConstructor.class, getClass()) | ||
.setArgs(ImmutableList.of("-processor", AutoValueProcessor.class.getName())); | ||
|
||
@Test | ||
public void negative() { | ||
helper | ||
.addSourceLines( | ||
"Test.java", | ||
"import com.google.auto.value.AutoValue;", | ||
"class Test {", | ||
" @AutoValue.Builder", | ||
" abstract class Builder {", | ||
" Builder() {}", | ||
" abstract void setFoo(int foo);", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void positive() { | ||
refactoringHelper | ||
.addInputLines( | ||
"Test.java", | ||
"package test;", | ||
"import com.google.auto.value.AutoValue;", | ||
"@AutoValue", | ||
"abstract class Test {", | ||
" abstract int foo();", | ||
" Builder builder() {", | ||
" return new AutoValue_Test.Builder();", | ||
" }", | ||
" @AutoValue.Builder", | ||
" abstract static class Builder {", | ||
" Builder() {", | ||
" this.setFoo(1);", | ||
" }", | ||
" abstract Builder setFoo(int foo);", | ||
" abstract Test build();", | ||
" }", | ||
"}") | ||
.addOutputLines( | ||
"Test.java", | ||
"package test;", | ||
"import com.google.auto.value.AutoValue;", | ||
"@AutoValue", | ||
"abstract class Test {", | ||
" abstract int foo();", | ||
" Builder builder() {", | ||
" return new AutoValue_Test.Builder().setFoo(1);", | ||
" }", | ||
" @AutoValue.Builder", | ||
" abstract static class Builder {", | ||
" abstract Builder setFoo(int foo);", | ||
" abstract Test build();", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void negative_nonAbstractMethodCalled() { | ||
refactoringHelper | ||
.addInputLines( | ||
"Test.java", | ||
"package test;", | ||
"import com.google.auto.value.AutoValue;", | ||
"@AutoValue", | ||
"abstract class Test {", | ||
" abstract int foo();", | ||
" Builder builder() {", | ||
" return new AutoValue_Test.Builder();", | ||
" }", | ||
" @AutoValue.Builder", | ||
" abstract static class Builder {", | ||
" Builder() {", | ||
" doSomethingOdd();", | ||
" }", | ||
" void doSomethingOdd() {}", | ||
" abstract Builder setFoo(int foo);", | ||
" abstract Test build();", | ||
" }", | ||
"}") | ||
.expectUnchanged() | ||
.doTest(); | ||
} | ||
} |