Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Aug 18, 2022
1 parent 402da2b commit 42c5bb7
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
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.sun.source.tree.MethodTree;
Expand Down Expand Up @@ -70,11 +71,11 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
return Description.NO_MATCH;
}

SuggestedFix.Builder builder = SuggestedFix.builder();
String valueTypeIdentifier =
SuggestedFixes.qualifyType(state, builder, "org.immutables.value.Value");
return describeMatch(
tree,
SuggestedFix.builder()
.addImport("org.immutables.value.Value")
.prefixWith(tree, "@Value.NaturalOrder ")
.build());
builder.prefixWith(tree, String.format("@%s.NaturalOrder ", valueTypeIdentifier)).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,133 +16,164 @@ void identification() {
compilationTestHelper
.addSourceLines(
"A.java",
"import com.google.common.collect.ContiguousSet;",
"import com.google.common.collect.ImmutableSet;",
"import com.google.common.collect.ImmutableSortedSet;",
"import java.util.NavigableSet;",
"import java.util.Set;",
"import java.util.SortedSet;",
"import java.util.TreeSet;",
"import org.immutables.value.Value;",
"",
"@Value.Immutable",
"interface A {",
" // BUG: Diagnostic contains:",
" ImmutableSortedSet<String> sortedSet();",
"",
" default ImmutableSortedSet<String> defaultSortedSet() {",
" return ImmutableSortedSet.of();",
" }",
" @Value.Immutable",
" interface ImmutableInterface {",
" Set<String> set();",
"",
" @Value.NaturalOrder",
" ImmutableSortedSet<String> defaultSortedSet2();",
"}",
" // BUG: Diagnostic contains:",
" SortedSet<String> sortedSet();",
"",
"@Value.Modifiable",
"interface B {",
" // BUG: Diagnostic contains:",
" ImmutableSortedSet<String> sortedSet();",
" @Value.NaturalOrder",
" SortedSet<String> sortedSet2();",
" }",
"",
" default ImmutableSortedSet<String> defaultSortedSet() {",
" return ImmutableSortedSet.of();",
" @Value.Modifiable",
" interface ModifiableInterfaceWithDefaults {",
" @Value.Default",
" default Set<Integer> set() {",
" return new TreeSet<>();",
" }",
"",
" @Value.Default",
" // BUG: Diagnostic contains:",
" default NavigableSet<Integer> navigableSet() {",
" return new TreeSet<>();",
" }",
"",
" @Value.Default",
" @Value.ReverseOrder",
" default NavigableSet<Integer> navigableSet2() {",
" return new TreeSet<>();",
" }",
"",
" default NavigableSet<Integer> nonPropertyNavigableSet() {",
" return new TreeSet<>();",
" }",
" }",
"",
" @Value.NaturalOrder",
" ImmutableSortedSet<String> defaultSortedSet2();",
"}",
" interface NonImmutablesInterface {",
" SortedSet<String> sortedSet();",
" }",
"",
"@Value.Immutable",
"abstract class C {",
" // BUG: Diagnostic contains:",
" abstract ImmutableSortedSet<String> sortedSet();",
" @Value.Immutable",
" abstract class AbstractImmutableWithDefaults {",
" @Value.Default",
" ImmutableSet<Integer> immutableSet() {",
" return ImmutableSet.of();",
" }",
"",
" @Value.Default",
" // BUG: Diagnostic contains:",
" ImmutableSortedSet<String> immutableSortedSet() {",
" return ImmutableSortedSet.of();",
" }",
"",
" @Value.Default",
" @Value.NaturalOrder",
" ImmutableSortedSet<String> immutableSortedSet2() {",
" return ImmutableSortedSet.of();",
" }",
"",
" ImmutableSortedSet<String> defaultSortedSet() {",
" return ImmutableSortedSet.of();",
" ImmutableSortedSet<String> nonPropertyImmutableSortedSet() {",
" return ImmutableSortedSet.of();",
" }",
" }",
"",
" @Value.NaturalOrder",
" abstract ImmutableSortedSet<String> defaultSortedSet2();",
"}",
" @Value.Modifiable",
" abstract class AbstractModifiable {",
" abstract ImmutableSet<Integer> immutableSet();",
"",
"@Value.Modifiable",
"abstract class D {",
" // BUG: Diagnostic contains:",
" abstract ImmutableSortedSet<String> sortedSet();",
" // BUG: Diagnostic contains:",
" abstract ContiguousSet<Integer> contiguousSet();",
"",
" ImmutableSortedSet<String> defaultSortedSet() {",
" return ImmutableSortedSet.of();",
" @Value.ReverseOrder",
" abstract ContiguousSet<Integer> contiguousSet2();",
" }",
"",
" @Value.NaturalOrder",
" abstract ImmutableSortedSet<String> defaultSortedSet2();",
"}",
"",
"abstract class E {",
" abstract ImmutableSortedSet<String> sortedSet();",
" abstract class AbstractNonImmutables {",
" abstract SortedSet<Integer> sortedSet();",
" }",
"}")
.doTest();
}

@Test
void replacementInImmutable() {
void replacement() {
refactoringTestHelper
.addInputLines(
"A.java",
"import com.google.common.collect.ImmutableSortedSet;",
"import java.util.SortedSet;",
"import org.immutables.value.Value;",
"",
"@Value.Immutable",
"abstract class A {",
" abstract ImmutableSortedSet<String> sortedSet();",
"",
" @Value.Immutable",
" @Value.Modifiable",
" interface B {",
" ImmutableSortedSet<String> sortedSet();",
" SortedSet<String> sortedSet();",
" }",
"}")
.addOutputLines(
"A.java",
"import com.google.common.collect.ImmutableSortedSet;",
"import java.util.SortedSet;",
"import org.immutables.value.Value;",
"",
"@Value.Immutable",
"abstract class A {",
" @Value.NaturalOrder",
" abstract ImmutableSortedSet<String> sortedSet();",
"",
" @Value.Immutable",
" @Value.Modifiable",
" interface B {",
" @Value.NaturalOrder",
" ImmutableSortedSet<String> sortedSet();",
" SortedSet<String> sortedSet();",
" }",
"}")
.doTest(TestMode.TEXT_MATCH);
}

@Test
void replacementInModifiable() {
void replacementWithImportClash() {
refactoringTestHelper
.addInputLines(
"A.java",
"MySpringService.java",
"import com.google.common.collect.ImmutableSortedSet;",
"import org.immutables.value.Value;",
"import org.springframework.beans.factory.annotation.Value;",
"",
"@Value.Modifiable",
"abstract class A {",
" abstract ImmutableSortedSet<String> sortedSet();",
"class MySpringService {",
" MySpringService(@Value(\"${someProperty}\") String prop) {}",
" ;",
"",
" @Value.Modifiable",
" interface B {",
" @org.immutables.value.Value.Immutable",
" interface A {",
" ImmutableSortedSet<String> sortedSet();",
" }",
"}")
.addOutputLines(
"A.java",
"MySpringService.java",
"import com.google.common.collect.ImmutableSortedSet;",
"import org.immutables.value.Value;",
"import org.springframework.beans.factory.annotation.Value;",
"",
"@Value.Modifiable",
"abstract class A {",
" @Value.NaturalOrder",
" abstract ImmutableSortedSet<String> sortedSet();",
"class MySpringService {",
" MySpringService(@Value(\"${someProperty}\") String prop) {}",
" ;",
"",
" @Value.Modifiable",
" interface B {",
" @Value.NaturalOrder",
" @org.immutables.value.Value.Immutable",
" interface A {",
" @org.immutables.value.Value.NaturalOrder",
" ImmutableSortedSet<String> sortedSet();",
" }",
"}")
Expand Down

0 comments on commit 42c5bb7

Please sign in to comment.