Skip to content

Commit

Permalink
Adding listOf template
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitor Mussatto committed Feb 18, 2022
1 parent 36b5d97 commit 58af714
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.errorprone.refaster.Refaster;
Expand All @@ -12,14 +10,11 @@
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NavigableSet;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
import java.util.SortedSet;
import java.util.function.IntFunction;
import java.util.stream.Stream;
Expand Down Expand Up @@ -408,39 +403,4 @@ Optional<T> after(Queue<T> queue) {
// XXX: collection.stream().noneMatch(e -> e.equals(other))
// ^ This is !collection.contains(other). Do we already rewrite variations on this?

static final class ImmutableSetOf<T> {
@BeforeTemplate
Set<T> before() {
return Collections.emptySet();
}

@AfterTemplate
ImmutableSet<T> after() {
return ImmutableSet.of();
}
}

static final class ImmutableListOf<T> {
@BeforeTemplate
List<T> before() {
return Collections.emptyList();
}

@AfterTemplate
ImmutableList<T> after() {
return ImmutableList.of();
}
}

static final class ImmutableMapOf<K, V> {
@BeforeTemplate
Map<K, V> before() {
return Collections.emptyMap();
}

@AfterTemplate
ImmutableMap<K, V> after() {
return ImmutableMap.of();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
Expand Down Expand Up @@ -186,4 +187,28 @@ ImmutableList<T> after(Stream<T> stream) {
return stream.collect(toImmutableSet()).asList();
}
}

static final class ImmutableListOf<T> {
@BeforeTemplate
List<T> before() {
return Collections.emptyList();
}

@AfterTemplate
ImmutableList<T> after() {
return ImmutableList.of();
}
}

static final class ImmutableListOf1<T> {
@BeforeTemplate
List<T> before(T item) {
return List.of(item);
}

@AfterTemplate
ImmutableCollection<T> after(T item) {
return ImmutableList.of(item);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,18 @@ ImmutableMap<K, V> after(ImmutableMap<K, V> map) {
}
}

static final class ImmutableMapOf<K, V> {
@BeforeTemplate
Map<K, V> before() {
return Collections.emptyMap();
}

@AfterTemplate
ImmutableMap<K, V> after() {
return ImmutableMap.of();
}
}

// XXX: Add a template for this:
// Maps.transformValues(streamOfEntries.collect(groupBy(fun)), ImmutableMap::copyOf)
// ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,16 @@ ImmutableSet<T> after(SetView<T> set) {
return set.immutableCopy();
}
}

static final class ImmutableSetOf<T> {
@BeforeTemplate
Set<T> before() {
return Collections.emptySet();
}

@AfterTemplate
ImmutableSet<T> after() {
return ImmutableSet.of();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Stream;

final class CollectionTemplatesTest implements RefasterTemplateTestCase {
@Override
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(Collections.class, Iterables.class, Lists.class);
return ImmutableSet.of(Iterables.class, Lists.class);
}

ImmutableSet<Boolean> testCollectionIsEmpty() {
Expand Down Expand Up @@ -188,28 +184,4 @@ ImmutableSet<Optional<String>> testRemoveOptionalFirstQueueElement() {
? Optional.ofNullable(new LinkedList<String>().remove())
: Optional.empty());
}

Set<?> testImmutableSetOf() {
return Collections.emptySet();
}

Set<String> testImmutableSetOfTyped() {
return Collections.emptySet();
}

List<?> testImmutableListOf() {
return Collections.emptyList();
}

List<String> testImmutableListOfTyped() {
return Collections.emptyList();
}

Map<?, ?> testImmutableMapOf() {
return Collections.emptyMap();
}

Map<String, String> testImmutableMapOfTyped() {
return Collections.emptyMap();
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
package tech.picnic.errorprone.bugpatterns;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Stream;

final class CollectionTemplatesTest implements RefasterTemplateTestCase {
@Override
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(Collections.class, Iterables.class, Lists.class);
return ImmutableSet.of(Iterables.class, Lists.class);
}

ImmutableSet<Boolean> testCollectionIsEmpty() {
Expand Down Expand Up @@ -139,28 +134,4 @@ ImmutableSet<Optional<String>> testRemoveOptionalFirstQueueElement() {
Optional.ofNullable(new LinkedList<String>().poll()),
Optional.ofNullable(new LinkedList<String>().poll()));
}

Set<?> testImmutableSetOf() {
return ImmutableSet.of();
}

Set<String> testImmutableSetOfTyped() {
return ImmutableSet.of();
}

List<?> testImmutableListOf() {
return ImmutableList.of();
}

List<String> testImmutableListOfTyped() {
return ImmutableList.of();
}

Map<?, ?> testImmutableMapOf() {
return ImmutableMap.of();
}

Map<String, String> testImmutableMapOfTyped() {
return ImmutableMap.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
Expand All @@ -19,6 +20,7 @@ final class ImmutableListTemplatesTest implements RefasterTemplateTestCase {
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(
Arrays.class,
Collection.class,
Collections.class,
Comparator.class,
Streams.class,
Expand Down Expand Up @@ -79,4 +81,16 @@ ImmutableSet<ImmutableList<String>> testImmutableListSortedCopyOfWithCustomCompa
ImmutableList<Integer> testStreamToDistinctImmutableList() {
return Stream.of(1).distinct().collect(toImmutableList());
}

List<?> testImmutableListOf() {
return Collections.emptyList();
}

List<String> testImmutableListOfTyped() {
return Collections.emptyList();
}

ImmutableSet<Collection<String>> testImmutableList1() {
return ImmutableSet.of(List.of("1"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
Expand All @@ -20,6 +21,7 @@ final class ImmutableListTemplatesTest implements RefasterTemplateTestCase {
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(
Arrays.class,
Collection.class,
Collections.class,
Comparator.class,
Streams.class,
Expand Down Expand Up @@ -74,4 +76,16 @@ ImmutableSet<ImmutableList<String>> testImmutableListSortedCopyOfWithCustomCompa
ImmutableList<Integer> testStreamToDistinctImmutableList() {
return Stream.of(1).collect(toImmutableSet()).asList();
}

List<?> testImmutableListOf() {
return ImmutableList.of();
}

List<String> testImmutableListOfTyped() {
return ImmutableList.of();
}

ImmutableSet<Collection<String>> testImmutableList1() {
return ImmutableSet.of(ImmutableList.of("1"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@ ImmutableSet<ImmutableMap<String, Integer>> testTransformMapValuesToImmutableMap
ImmutableMap<String, Integer> testImmutableMapCopyOfImmutableMap() {
return ImmutableMap.copyOf(ImmutableMap.of("foo", 1));
}

Map<?, ?> testImmutableMapOf() {
return Collections.emptyMap();
}

Map<String, String> testImmutableMapOfTyped() {
return Collections.emptyMap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,12 @@ ImmutableSet<ImmutableMap<String, Integer>> testTransformMapValuesToImmutableMap
ImmutableMap<String, Integer> testImmutableMapCopyOfImmutableMap() {
return ImmutableMap.of("foo", 1);
}

Map<?, ?> testImmutableMapOf() {
return ImmutableMap.of();
}

Map<String, String> testImmutableMapOfTyped() {
return ImmutableMap.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,12 @@ ImmutableSet<Integer> testImmutableSetCopyOfImmutableSet() {
ImmutableSet<Integer> testImmutableSetCopyOfSetView() {
return ImmutableSet.copyOf(Sets.difference(ImmutableSet.of(1), ImmutableSet.of(2)));
}

Set<?> testImmutableSetOf() {
return Collections.emptySet();
}

Set<String> testImmutableSetOfTyped() {
return Collections.emptySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,12 @@ ImmutableSet<Integer> testImmutableSetCopyOfImmutableSet() {
ImmutableSet<Integer> testImmutableSetCopyOfSetView() {
return Sets.difference(ImmutableSet.of(1), ImmutableSet.of(2)).immutableCopy();
}

Set<?> testImmutableSetOf() {
return ImmutableSet.of();
}

Set<String> testImmutableSetOfTyped() {
return ImmutableSet.of();
}
}

0 comments on commit 58af714

Please sign in to comment.