Skip to content

Commit 8b57736

Browse files
committed
Add violations for Guava Iterables
Fixes #114.
1 parent e4c60b0 commit 8b57736

File tree

3 files changed

+192
-1
lines changed

3 files changed

+192
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ For example, Modernizer can detect uses of `Vector` instead of `ArrayList`,
1111
`String.getBytes(String)` instead of `String.getBytes(Charset)`, and
1212
Guava `Objects.equal` instead of Java 7 `Objects.equals`.
1313
The default configuration detects
14-
[roughly 200 legacy APIs](https://github.com/gaul/modernizer-maven-plugin/blob/master/modernizer-maven-plugin/src/main/resources/modernizer.xml),
14+
[over 200 legacy APIs](https://github.com/gaul/modernizer-maven-plugin/blob/master/modernizer-maven-plugin/src/main/resources/modernizer.xml),
1515
including third-party libraries like
1616
[Apache Commons](https://commons.apache.org/),
1717
[Guava](https://github.com/google/guava),

modernizer-maven-plugin/src/main/resources/modernizer.xml

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,162 @@ violation names use the same format that javap emits.
11221122
<comment>Prefer java.util.OptionalDouble.stream()</comment>
11231123
</violation>
11241124

1125+
<violation>
1126+
<name>com/google/common/collect/Iterables.getOnlyElement:(Ljava/lang/Iterable;)Ljava/lang/Object;</name>
1127+
<version>1.8</version>
1128+
<comment>Prefer Stream.collect(MoreCollectors.onlyElement())</comment>
1129+
</violation>
1130+
1131+
<violation>
1132+
<name>com/google/common/collect/Iterables.getOnlyElement:(Ljava/lang/Iterable;Ljava/lang/Object;)Ljava/lang/Object;</name>
1133+
<version>1.8</version>
1134+
<comment>Prefer Stream.collect(MoreCollectors.toOptional()).orElse(defaultValue)</comment>
1135+
</violation>
1136+
1137+
<violation>
1138+
<name>com/google/common/collect/Iterables.frequency:(Ljava/lang/Iterable;Ljava/lang/Object;)I</name>
1139+
<version>1.8</version>
1140+
<comment>Prefer Stream.filter(element::equals).count() or Stream.filter(Predicate.isEqual(element)).count()</comment>
1141+
</violation>
1142+
1143+
<violation>
1144+
<name>com/google/common/collect/Iterables.cycle:(Ljava/lang/Iterable;)Ljava/lang/Iterable;</name>
1145+
<version>1.8</version>
1146+
<comment>Prefer Stream.generate(() -> iterable).flatMap(Streams::stream)</comment>
1147+
</violation>
1148+
1149+
<violation>
1150+
<name>com/google/common/collect/Iterables.cycle:([Ljava/lang/Object;)Ljava/lang/Iterable;</name>
1151+
<version>1.8</version>
1152+
<comment>Prefer Stream equivalent of this method is Stream.generate(() -> e) or Stream.generate(() -> collection).flatMap(Collection::stream)</comment>
1153+
</violation>
1154+
1155+
<violation>
1156+
<name>com/google/common/collect/Iterables.concat:(Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/lang/Iterable;</name>
1157+
<version>1.8</version>
1158+
<comment>Prefer Stream.concat(a, b)</comment>
1159+
</violation>
1160+
1161+
<violation>
1162+
<name>com/google/common/collect/Iterables.concat:(Ljava/lang/Iterable;Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/lang/Iterable;</name>
1163+
<version>1.8</version>
1164+
<comment>Prefer Streams.concat(a, b, c)</comment>
1165+
</violation>
1166+
1167+
<violation>
1168+
<name>com/google/common/collect/Iterables.concat:(Ljava/lang/Iterable;Ljava/lang/Iterable;Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/lang/Iterable;</name>
1169+
<version>1.8</version>
1170+
<comment>Prefer Streams.concat(a, b, c, d)</comment>
1171+
</violation>
1172+
1173+
<violation>
1174+
<name>com/google/common/collect/Iterables.concat:(Ljava/lang/Iterable;)Ljava/lang/Iterable;</name>
1175+
<version>1.8</version>
1176+
<comment>Prefer Streams.concat(...)</comment>
1177+
</violation>
1178+
1179+
<violation>
1180+
<name>com/google/common/collect/Iterables.concat:([Ljava/lang/Iterable;)Ljava/lang/Iterable;</name>
1181+
<version>1.8</version>
1182+
<comment>Prefer Stream.flatMap(s -> s)</comment>
1183+
</violation>
1184+
1185+
<violation>
1186+
<name>com/google/common/collect/Iterables.filter:(Ljava/lang/Iterable;Lcom/google/common/base/Predicate;)Ljava/lang/Iterable;</name>
1187+
<version>1.8</version>
1188+
<comment>Prefer Stream.filter(java.util.function.Predicate&lt;? super T&gt;)</comment>
1189+
</violation>
1190+
1191+
<violation>
1192+
<name>com/google/common/collect/Iterables.filter:(Ljava/lang/Iterable;Ljava/lang/Class;)Ljava/lang/Iterable;</name>
1193+
<version>1.8</version>
1194+
<comment>Prefer Stream.filter(type::isInstance).map(type::cast)</comment>
1195+
</violation>
1196+
1197+
<violation>
1198+
<name>com/google/common/collect/Iterables.any:(Ljava/lang/Iterable;Lcom/google/common/base/Predicate;)Z</name>
1199+
<version>1.8</version>
1200+
<comment>Prefer Stream.anyMatch(java.util.function.Predicate&lt;? super T&gt;)</comment>
1201+
</violation>
1202+
1203+
<violation>
1204+
<name>com/google/common/collect/Iterables.all:(Ljava/lang/Iterable;Lcom/google/common/base/Predicate;)Z</name>
1205+
<version>1.8</version>
1206+
<comment>Prefer Stream.allMatch(java.util.function.Predicate&lt;? super T&gt;)</comment>
1207+
</violation>
1208+
1209+
<violation>
1210+
<name>com/google/common/collect/Iterables.find:(Ljava/lang/Iterable;Lcom/google/common/base/Predicate;)Ljava/lang/Object;</name>
1211+
<version>1.8</version>
1212+
<comment>Prefer Stream.filter(predicate).findFirst().get()</comment>
1213+
</violation>
1214+
1215+
<violation>
1216+
<name>com/google/common/collect/Iterables.find:(Ljava/lang/Iterable;Lcom/google/common/base/Predicate;Ljava/lang/Object;)Ljava/lang/Object;</name>
1217+
<version>1.8</version>
1218+
<comment>Prefer Stream.filter(predicate).findFirst().orElse(defaultValue)</comment>
1219+
</violation>
1220+
1221+
<violation>
1222+
<name>com/google/common/collect/Iterables.tryFind:(Ljava/lang/Iterable;Lcom/google/common/base/Predicate;)Lcom/google/common/base/Optional;</name>
1223+
<version>1.8</version>
1224+
<comment>Prefer Stream.filter(predicate).findFirst()</comment>
1225+
</violation>
1226+
1227+
<violation>
1228+
<name>com/google/common/collect/Iterables.transform:(Ljava/lang/Iterable;Lcom/google/common/base/Function;)Ljava/lang/Iterable;</name>
1229+
<version>1.8</version>
1230+
<comment>Prefer Stream.map(java.util.function.Function&lt;? super T, ? extends R&gt;)</comment>
1231+
</violation>
1232+
1233+
<violation>
1234+
<name>com/google/common/collect/Iterables.get:(Ljava/lang/Iterable;I)Ljava/lang/Object;</name>
1235+
<version>1.8</version>
1236+
<comment>Prefer Stream.skip(position).findFirst().get()</comment>
1237+
</violation>
1238+
1239+
<violation>
1240+
<name>com/google/common/collect/Iterables.get:(Ljava/lang/Iterable;ILjava/lang/Object;)Ljava/lang/Object;</name>
1241+
<version>1.8</version>
1242+
<comment>Prefer Stream.skip(position).findFirst().orElse(defaultValue)</comment>
1243+
</violation>
1244+
1245+
<violation>
1246+
<name>com/google/common/collect/Iterables.getFirst:(Ljava/lang/Iterable;Ljava/lang/Object;)Ljava/lang/Object;</name>
1247+
<version>1.8</version>
1248+
<comment>Prefer Stream.findFirst().orElse(defaultValue)</comment>
1249+
</violation>
1250+
1251+
<violation>
1252+
<name>com/google/common/collect/Iterables.getLast:(Ljava/lang/Iterable;)Ljava/lang/Object;</name>
1253+
<version>1.8</version>
1254+
<comment>Prefer Streams.findLast(stream).get()</comment>
1255+
</violation>
1256+
1257+
<violation>
1258+
<name>com/google/common/collect/Iterables.getLast:(Ljava/lang/Iterable;Ljava/lang/Object;)Ljava/lang/Object;</name>
1259+
<version>1.8</version>
1260+
<comment>Prefer Streams.findLast(stream).orElse(defaultValue)</comment>
1261+
</violation>
1262+
1263+
<violation>
1264+
<name>com/google/common/collect/Iterables.skip:(Ljava/lang/Iterable;I)Ljava/lang/Iterable;</name>
1265+
<version>1.8</version>
1266+
<comment>Prefer Stream.skip(long)</comment>
1267+
</violation>
1268+
1269+
<violation>
1270+
<name>com/google/common/collect/Iterables.limit:(Ljava/lang/Iterable;I)Ljava/lang/Iterable;</name>
1271+
<version>1.8</version>
1272+
<comment>Prefer Stream.limit(long)</comment>
1273+
</violation>
1274+
1275+
<violation>
1276+
<name>com/google/common/collect/Iterables.isEmpty:(Ljava/lang/Iterable;)Z</name>
1277+
<version>1.8</version>
1278+
<comment>Prefer !stream.findAny().isPresent()</comment>
1279+
</violation>
1280+
11251281
<violation>
11261282
<name>com/google/common/collect/Iterators.forEnumeration:(Ljava/util/Enumeration;)Lcom/google/common/collect/UnmodifiableIterator;</name>
11271283
<version>1.9</version>

modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/ModernizerTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import com.google.common.collect.ImmutableList;
6868
import com.google.common.collect.ImmutableMap;
6969
import com.google.common.collect.ImmutableSet;
70+
import com.google.common.collect.Iterables;
7071
import com.google.common.collect.Iterators;
7172
import com.google.common.collect.Lists;
7273
import com.google.common.collect.Maps;
@@ -386,6 +387,8 @@ public void testAllViolations() throws Exception {
386387
Modernizer modernizer = createModernizer("1.12");
387388
Collection<ViolationOccurrence> occurrences = modernizer.check(
388389
new ClassReader(AllViolations.class.getName()));
390+
occurrences.addAll(modernizer.check(
391+
new ClassReader(Java8Violations.class.getName())));
389392
occurrences.addAll(modernizer.check(
390393
new ClassReader(Java10Violations.class.getName())));
391394
occurrences.addAll(modernizer.check(
@@ -736,6 +739,38 @@ private static void method() throws Exception {
736739
}
737740
}
738741

742+
// TODO: move more methods from AllViolations to here
743+
private static class Java8Violations {
744+
private static void method() throws Exception {
745+
Iterables.getOnlyElement(null);
746+
Iterables.getOnlyElement(null, null);
747+
Iterables.frequency(null, null);
748+
Iterables.cycle((Iterable) null);
749+
Iterables.cycle();
750+
Iterables.concat(null, null);
751+
Iterables.concat(null, null, null);
752+
Iterables.concat(null, null, null, null);
753+
Iterables.concat((Iterable) null);
754+
Iterables.concat((Iterable<Iterable>) null);
755+
Iterables.filter(null, (Predicate) null);
756+
Iterables.filter(null, (Class) null);
757+
Iterables.any(null, null);
758+
Iterables.all(null, null);
759+
Iterables.find(null, null);
760+
Iterables.find(null, null, null);
761+
Iterables.tryFind(null, null);
762+
Iterables.transform(null, null);
763+
Iterables.get(null, 0);
764+
Iterables.get(null, 0, null);
765+
Iterables.getFirst(null, null);
766+
Iterables.getLast(null);
767+
Iterables.getLast(null, null);
768+
Iterables.skip(null, 0);
769+
Iterables.limit(null, 0);
770+
Iterables.isEmpty(null);
771+
}
772+
}
773+
739774
private static class Java10Violations {
740775
private static void method() throws Exception {
741776
new PrintStream((File) null, "");

0 commit comments

Comments
 (0)