diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MissingImmutableSortedSetDefaultCheck.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MissingImmutableSortedSetDefaultCheck.java index 243632226ed..cc547f06755 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MissingImmutableSortedSetDefaultCheck.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MissingImmutableSortedSetDefaultCheck.java @@ -18,7 +18,6 @@ import com.google.errorprone.fixes.SuggestedFix; import com.google.errorprone.matchers.Description; import com.google.errorprone.matchers.Matcher; -import com.google.errorprone.util.ASTHelpers; import com.sun.source.tree.MethodTree; import com.sun.source.tree.Tree; import org.immutables.value.Value.Immutable; @@ -63,16 +62,11 @@ public Description matchMethod(MethodTree tree, VisitorState state) { // The ImmutableSortedSet has no empty default -> add the `@Value.NaturalOrder` annotation or // provide a default implementation. return buildDescription(tree) + .setMessage( + "Methods returning an `ImmutableSortedSet` within an @Value.Immutable or @Value.Modifiable class " + + "should include additional deserialization information for absent sets. " + + "Alternatively, specify a default implementation.") .addFix(SuggestedFix.builder().prefixWith(tree, "@Value.NaturalOrder ").build()) - .addFix( - SuggestedFix.builder() - .replace( - ASTHelpers.getStartPosition(tree), ASTHelpers.getStartPosition(tree) + 8, "") - .replace( - state.getEndPosition(tree) - 1, - state.getEndPosition(tree), - "{ return ImmutableSortedSet.of(); }") - .build()) .build(); } } diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MissingImmutableSortedSetDefaultCheckTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MissingImmutableSortedSetDefaultCheckTest.java index dec6a045b82..0268846c376 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MissingImmutableSortedSetDefaultCheckTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MissingImmutableSortedSetDefaultCheckTest.java @@ -1,7 +1,5 @@ package tech.picnic.errorprone.bugpatterns; -import static com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers.SECOND; - import com.google.errorprone.BugCheckerRefactoringTestHelper; import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode; import com.google.errorprone.CompilationTestHelper; @@ -98,7 +96,7 @@ void identification() { } @Test - void replacementInImmutableInterface() { + void replacementInImmutable() { refactoringTestHelper .addInputLines( "A.java", @@ -106,103 +104,13 @@ void replacementInImmutableInterface() { "import com.google.common.collect.ImmutableSortedSet;", "", "@Value.Immutable", - "interface A {", - " ImmutableSortedSet sortedSet();", - " default ImmutableSortedSet defaultSortedSet() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.Default", - " default ImmutableSortedSet defaultSortedSet2() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.NaturalOrder", - " ImmutableSortedSet defaultSortedSet3();", - "}") - .addOutputLines( - "A.java", - "import org.immutables.value.Value;", - "import com.google.common.collect.ImmutableSortedSet;", - "", - "@Value.Immutable", - "interface A {", - " @Value.NaturalOrder", - " ImmutableSortedSet sortedSet();", - " default ImmutableSortedSet defaultSortedSet() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.Default", - " default ImmutableSortedSet defaultSortedSet2() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.NaturalOrder", - " ImmutableSortedSet defaultSortedSet3();", - "}") - .doTest(TestMode.TEXT_MATCH); - } - - @Test - void replacementInModifiableInterface() { - refactoringTestHelper - .addInputLines( - "A.java", - "import org.immutables.value.Value;", - "import com.google.common.collect.ImmutableSortedSet;", - "", - "@Value.Modifiable", - "interface B {", - " ImmutableSortedSet sortedSet();", - " default ImmutableSortedSet defaultSortedSet() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.Default", - " default ImmutableSortedSet defaultSortedSet2() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.NaturalOrder", - " ImmutableSortedSet defaultSortedSet3();", - "}") - .addOutputLines( - "A.java", - "import org.immutables.value.Value;", - "import com.google.common.collect.ImmutableSortedSet;", - "", - "@Value.Modifiable", - "interface B {", - " @Value.NaturalOrder", - " ImmutableSortedSet sortedSet();", - " default ImmutableSortedSet defaultSortedSet() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.Default", - " default ImmutableSortedSet defaultSortedSet2() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.NaturalOrder", - " ImmutableSortedSet defaultSortedSet3();", - "}") - .doTest(TestMode.TEXT_MATCH); - } - - @Test - void replacementInImmutableAbstractClass() { - refactoringTestHelper - .addInputLines( - "A.java", - "import org.immutables.value.Value;", - "import com.google.common.collect.ImmutableSortedSet;", - "", - "@Value.Immutable", - "abstract class C {", + "abstract class A {", " abstract ImmutableSortedSet sortedSet();", - " ImmutableSortedSet defaultSortedSet() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.Default", - " ImmutableSortedSet defaultSortedSet2() {", - " return ImmutableSortedSet.of();", + "", + " @Value.Immutable", + " interface B {", + " ImmutableSortedSet sortedSet();", " }", - " @Value.NaturalOrder", - " abstract ImmutableSortedSet defaultSortedSet3();", "}") .addOutputLines( "A.java", @@ -210,24 +118,21 @@ void replacementInImmutableAbstractClass() { "import com.google.common.collect.ImmutableSortedSet;", "", "@Value.Immutable", - "abstract class C {", + "abstract class A {", " @Value.NaturalOrder", " abstract ImmutableSortedSet sortedSet();", - " ImmutableSortedSet defaultSortedSet() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.Default", - " ImmutableSortedSet defaultSortedSet2() {", - " return ImmutableSortedSet.of();", + "", + " @Value.Immutable", + " interface B {", + " @Value.NaturalOrder", + " ImmutableSortedSet sortedSet();", " }", - " @Value.NaturalOrder", - " abstract ImmutableSortedSet defaultSortedSet3();", "}") .doTest(TestMode.TEXT_MATCH); } @Test - void replacementInModifiableAbstractClass() { + void replacementInModifiable() { refactoringTestHelper .addInputLines( "A.java", @@ -235,61 +140,13 @@ void replacementInModifiableAbstractClass() { "import com.google.common.collect.ImmutableSortedSet;", "", "@Value.Modifiable", - "abstract class D {", + "abstract class A {", " abstract ImmutableSortedSet sortedSet();", - " ImmutableSortedSet defaultSortedSet() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.Default", - " ImmutableSortedSet defaultSortedSet2() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.NaturalOrder", - " abstract ImmutableSortedSet defaultSortedSet3();", - "}") - .addOutputLines( - "A.java", - "import org.immutables.value.Value;", - "import com.google.common.collect.ImmutableSortedSet;", "", - "@Value.Modifiable", - "abstract class D {", - " @Value.NaturalOrder", - " abstract ImmutableSortedSet sortedSet();", - " ImmutableSortedSet defaultSortedSet() {", - " return ImmutableSortedSet.of();", + " @Value.Modifiable", + " interface B {", + " ImmutableSortedSet sortedSet();", " }", - " @Value.Default", - " ImmutableSortedSet defaultSortedSet2() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.NaturalOrder", - " abstract ImmutableSortedSet defaultSortedSet3();", - "}") - .doTest(TestMode.TEXT_MATCH); - } - - @Test - void secondaryReplacementInModifiableInterface() { - refactoringTestHelper - .setFixChooser(SECOND) - .addInputLines( - "A.java", - "import org.immutables.value.Value;", - "import com.google.common.collect.ImmutableSortedSet;", - "", - "@Value.Modifiable", - "interface B {", - " ImmutableSortedSet sortedSet();", - " default ImmutableSortedSet defaultSortedSet() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.Default", - " default ImmutableSortedSet defaultSortedSet2() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.NaturalOrder", - " ImmutableSortedSet defaultSortedSet3();", "}") .addOutputLines( "A.java", @@ -297,64 +154,15 @@ void secondaryReplacementInModifiableInterface() { "import com.google.common.collect.ImmutableSortedSet;", "", "@Value.Modifiable", - "interface B {", - " default ImmutableSortedSet sortedSet() {", - " return ImmutableSortedSet.of();", - " }", - " default ImmutableSortedSet defaultSortedSet() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.Default", - " default ImmutableSortedSet defaultSortedSet2() {", - " return ImmutableSortedSet.of();", - " }", + "abstract class A {", " @Value.NaturalOrder", - " ImmutableSortedSet defaultSortedSet3();", - "}") - .doTest(TestMode.TEXT_MATCH); - } - - @Test - void secondaryReplacementInImmutableAbstractClass() { - refactoringTestHelper - .setFixChooser(SECOND) - .addInputLines( - "A.java", - "import org.immutables.value.Value;", - "import com.google.common.collect.ImmutableSortedSet;", - "", - "@Value.Immutable", - "abstract class C {", " abstract ImmutableSortedSet sortedSet();", - " ImmutableSortedSet defaultSortedSet() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.Default", - " ImmutableSortedSet defaultSortedSet2() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.NaturalOrder", - " abstract ImmutableSortedSet defaultSortedSet3();", - "}") - .addOutputLines( - "A.java", - "import org.immutables.value.Value;", - "import com.google.common.collect.ImmutableSortedSet;", "", - "@Value.Immutable", - "abstract class C {", - " ImmutableSortedSet sortedSet() {", - " return ImmutableSortedSet.of();", - " }", - " ImmutableSortedSet defaultSortedSet() {", - " return ImmutableSortedSet.of();", + " @Value.Modifiable", + " interface B {", + " @Value.NaturalOrder", + " ImmutableSortedSet sortedSet();", " }", - " @Value.Default", - " ImmutableSortedSet defaultSortedSet2() {", - " return ImmutableSortedSet.of();", - " }", - " @Value.NaturalOrder", - " abstract ImmutableSortedSet defaultSortedSet3();", "}") .doTest(TestMode.TEXT_MATCH); }