Skip to content

Commit

Permalink
Make it an error to initialise AutoValue.Builder defaults in construc…
Browse files Browse the repository at this point in the history
…tors

The automatic fix isn't perfect, but Flume: unknown commit

PiperOrigin-RevId: 422385003
  • Loading branch information
graememorgan authored and Error Prone Team committed Jan 17, 2022
1 parent 5e6691c commit 7821f59
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 0 deletions.
@@ -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());
}
}
Expand Up @@ -39,6 +39,7 @@
import com.google.errorprone.bugpatterns.AssertionFailureIgnored;
import com.google.errorprone.bugpatterns.AsyncCallableReturnsNull;
import com.google.errorprone.bugpatterns.AsyncFunctionReturnsNull;
import com.google.errorprone.bugpatterns.AutoValueBuilderDefaultsInConstructor;
import com.google.errorprone.bugpatterns.AutoValueFinalMethods;
import com.google.errorprone.bugpatterns.AutoValueImmutableFields;
import com.google.errorprone.bugpatterns.AutoValueSubclassLeaked;
Expand Down Expand Up @@ -596,6 +597,7 @@ public static ScannerSupplier errorChecks() {
AssistedParameters.class,
AsyncCallableReturnsNull.class,
AsyncFunctionReturnsNull.class,
AutoValueBuilderDefaultsInConstructor.class,
AutoValueConstructorOrderChecker.class,
BadAnnotationImplementation.class,
BadShiftAmount.class,
Expand Down
@@ -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();
}
}

0 comments on commit 7821f59

Please sign in to comment.