Skip to content

Commit

Permalink
Add violations for Guava Iterables
Browse files Browse the repository at this point in the history
Fixes #114.
  • Loading branch information
gaul committed Jul 22, 2021
1 parent e4c60b0 commit 8b57736
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ For example, Modernizer can detect uses of `Vector` instead of `ArrayList`,
`String.getBytes(String)` instead of `String.getBytes(Charset)`, and
Guava `Objects.equal` instead of Java 7 `Objects.equals`.
The default configuration detects
[roughly 200 legacy APIs](https://github.com/gaul/modernizer-maven-plugin/blob/master/modernizer-maven-plugin/src/main/resources/modernizer.xml),
[over 200 legacy APIs](https://github.com/gaul/modernizer-maven-plugin/blob/master/modernizer-maven-plugin/src/main/resources/modernizer.xml),
including third-party libraries like
[Apache Commons](https://commons.apache.org/),
[Guava](https://github.com/google/guava),
Expand Down
156 changes: 156 additions & 0 deletions modernizer-maven-plugin/src/main/resources/modernizer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,162 @@ violation names use the same format that javap emits.
<comment>Prefer java.util.OptionalDouble.stream()</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.getOnlyElement:(Ljava/lang/Iterable;)Ljava/lang/Object;</name>
<version>1.8</version>
<comment>Prefer Stream.collect(MoreCollectors.onlyElement())</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.getOnlyElement:(Ljava/lang/Iterable;Ljava/lang/Object;)Ljava/lang/Object;</name>
<version>1.8</version>
<comment>Prefer Stream.collect(MoreCollectors.toOptional()).orElse(defaultValue)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.frequency:(Ljava/lang/Iterable;Ljava/lang/Object;)I</name>
<version>1.8</version>
<comment>Prefer Stream.filter(element::equals).count() or Stream.filter(Predicate.isEqual(element)).count()</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.cycle:(Ljava/lang/Iterable;)Ljava/lang/Iterable;</name>
<version>1.8</version>
<comment>Prefer Stream.generate(() -> iterable).flatMap(Streams::stream)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.cycle:([Ljava/lang/Object;)Ljava/lang/Iterable;</name>
<version>1.8</version>
<comment>Prefer Stream equivalent of this method is Stream.generate(() -> e) or Stream.generate(() -> collection).flatMap(Collection::stream)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.concat:(Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/lang/Iterable;</name>
<version>1.8</version>
<comment>Prefer Stream.concat(a, b)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.concat:(Ljava/lang/Iterable;Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/lang/Iterable;</name>
<version>1.8</version>
<comment>Prefer Streams.concat(a, b, c)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.concat:(Ljava/lang/Iterable;Ljava/lang/Iterable;Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/lang/Iterable;</name>
<version>1.8</version>
<comment>Prefer Streams.concat(a, b, c, d)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.concat:(Ljava/lang/Iterable;)Ljava/lang/Iterable;</name>
<version>1.8</version>
<comment>Prefer Streams.concat(...)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.concat:([Ljava/lang/Iterable;)Ljava/lang/Iterable;</name>
<version>1.8</version>
<comment>Prefer Stream.flatMap(s -> s)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.filter:(Ljava/lang/Iterable;Lcom/google/common/base/Predicate;)Ljava/lang/Iterable;</name>
<version>1.8</version>
<comment>Prefer Stream.filter(java.util.function.Predicate&lt;? super T&gt;)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.filter:(Ljava/lang/Iterable;Ljava/lang/Class;)Ljava/lang/Iterable;</name>
<version>1.8</version>
<comment>Prefer Stream.filter(type::isInstance).map(type::cast)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.any:(Ljava/lang/Iterable;Lcom/google/common/base/Predicate;)Z</name>
<version>1.8</version>
<comment>Prefer Stream.anyMatch(java.util.function.Predicate&lt;? super T&gt;)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.all:(Ljava/lang/Iterable;Lcom/google/common/base/Predicate;)Z</name>
<version>1.8</version>
<comment>Prefer Stream.allMatch(java.util.function.Predicate&lt;? super T&gt;)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.find:(Ljava/lang/Iterable;Lcom/google/common/base/Predicate;)Ljava/lang/Object;</name>
<version>1.8</version>
<comment>Prefer Stream.filter(predicate).findFirst().get()</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.find:(Ljava/lang/Iterable;Lcom/google/common/base/Predicate;Ljava/lang/Object;)Ljava/lang/Object;</name>
<version>1.8</version>
<comment>Prefer Stream.filter(predicate).findFirst().orElse(defaultValue)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.tryFind:(Ljava/lang/Iterable;Lcom/google/common/base/Predicate;)Lcom/google/common/base/Optional;</name>
<version>1.8</version>
<comment>Prefer Stream.filter(predicate).findFirst()</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.transform:(Ljava/lang/Iterable;Lcom/google/common/base/Function;)Ljava/lang/Iterable;</name>
<version>1.8</version>
<comment>Prefer Stream.map(java.util.function.Function&lt;? super T, ? extends R&gt;)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.get:(Ljava/lang/Iterable;I)Ljava/lang/Object;</name>
<version>1.8</version>
<comment>Prefer Stream.skip(position).findFirst().get()</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.get:(Ljava/lang/Iterable;ILjava/lang/Object;)Ljava/lang/Object;</name>
<version>1.8</version>
<comment>Prefer Stream.skip(position).findFirst().orElse(defaultValue)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.getFirst:(Ljava/lang/Iterable;Ljava/lang/Object;)Ljava/lang/Object;</name>
<version>1.8</version>
<comment>Prefer Stream.findFirst().orElse(defaultValue)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.getLast:(Ljava/lang/Iterable;)Ljava/lang/Object;</name>
<version>1.8</version>
<comment>Prefer Streams.findLast(stream).get()</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.getLast:(Ljava/lang/Iterable;Ljava/lang/Object;)Ljava/lang/Object;</name>
<version>1.8</version>
<comment>Prefer Streams.findLast(stream).orElse(defaultValue)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.skip:(Ljava/lang/Iterable;I)Ljava/lang/Iterable;</name>
<version>1.8</version>
<comment>Prefer Stream.skip(long)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.limit:(Ljava/lang/Iterable;I)Ljava/lang/Iterable;</name>
<version>1.8</version>
<comment>Prefer Stream.limit(long)</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterables.isEmpty:(Ljava/lang/Iterable;)Z</name>
<version>1.8</version>
<comment>Prefer !stream.findAny().isPresent()</comment>
</violation>

<violation>
<name>com/google/common/collect/Iterators.forEnumeration:(Ljava/util/Enumeration;)Lcom/google/common/collect/UnmodifiableIterator;</name>
<version>1.9</version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
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.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
Expand Down Expand Up @@ -386,6 +387,8 @@ public void testAllViolations() throws Exception {
Modernizer modernizer = createModernizer("1.12");
Collection<ViolationOccurrence> occurrences = modernizer.check(
new ClassReader(AllViolations.class.getName()));
occurrences.addAll(modernizer.check(
new ClassReader(Java8Violations.class.getName())));
occurrences.addAll(modernizer.check(
new ClassReader(Java10Violations.class.getName())));
occurrences.addAll(modernizer.check(
Expand Down Expand Up @@ -736,6 +739,38 @@ private static void method() throws Exception {
}
}

// TODO: move more methods from AllViolations to here
private static class Java8Violations {
private static void method() throws Exception {
Iterables.getOnlyElement(null);
Iterables.getOnlyElement(null, null);
Iterables.frequency(null, null);
Iterables.cycle((Iterable) null);
Iterables.cycle();
Iterables.concat(null, null);
Iterables.concat(null, null, null);
Iterables.concat(null, null, null, null);
Iterables.concat((Iterable) null);
Iterables.concat((Iterable<Iterable>) null);
Iterables.filter(null, (Predicate) null);
Iterables.filter(null, (Class) null);
Iterables.any(null, null);
Iterables.all(null, null);
Iterables.find(null, null);
Iterables.find(null, null, null);
Iterables.tryFind(null, null);
Iterables.transform(null, null);
Iterables.get(null, 0);
Iterables.get(null, 0, null);
Iterables.getFirst(null, null);
Iterables.getLast(null);
Iterables.getLast(null, null);
Iterables.skip(null, 0);
Iterables.limit(null, 0);
Iterables.isEmpty(null);
}
}

private static class Java10Violations {
private static void method() throws Exception {
new PrintStream((File) null, "");
Expand Down

0 comments on commit 8b57736

Please sign in to comment.