Skip to content

Commit a86e28b

Browse files
klueverError Prone Team
authored and
Error Prone Team
committed
Warn when on Stream parameters and Iterator return types.
#klippy4apis PiperOrigin-RevId: 540344813
1 parent 4f11cba commit a86e28b

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/NonApiType.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
2020
import static com.google.errorprone.matchers.Description.NO_MATCH;
2121
import static com.google.errorprone.predicates.TypePredicates.anyOf;
22+
import static com.google.errorprone.predicates.TypePredicates.anything;
2223
import static com.google.errorprone.predicates.TypePredicates.isDescendantOf;
2324
import static com.google.errorprone.predicates.TypePredicates.isExactType;
25+
import static com.google.errorprone.predicates.TypePredicates.not;
2426
import static com.google.errorprone.util.ASTHelpers.getSymbol;
2527
import static com.google.errorprone.util.ASTHelpers.getType;
2628
import static com.google.errorprone.util.ASTHelpers.isSameType;
@@ -33,7 +35,6 @@
3335
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
3436
import com.google.errorprone.matchers.Description;
3537
import com.google.errorprone.predicates.TypePredicate;
36-
import com.google.errorprone.predicates.TypePredicates;
3738
import com.sun.source.tree.MethodTree;
3839
import com.sun.source.tree.Tree;
3940
import com.sun.tools.javac.code.Type;
@@ -54,12 +55,13 @@ public final class NonApiType extends BugChecker implements MethodTreeMatcher {
5455
private static final String INTERFACES_NOT_IMPLS_LINK = "";
5556
private static final String PRIMITIVE_ARRAYS_LINK = "";
5657
private static final String PROTO_TIME_SERIALIZATION_LINK = "";
58+
private static final String ITERATOR_LINK = "";
59+
private static final String STREAM_LINK = "";
5760
private static final String OPTIONAL_AS_PARAM_LINK = "";
5861
private static final String PREFER_JDK_OPTIONAL_LINK = "";
5962

60-
private static final TypePredicate GRAPH_WRAPPER =
61-
TypePredicates.not(
62-
TypePredicates.isDescendantOf("com.google.apps.framework.producers.GraphWrapper"));
63+
private static final TypePredicate NON_GRAPH_WRAPPER =
64+
not(isDescendantOf("com.google.apps.framework.producers.GraphWrapper"));
6365

6466
private static final ImmutableSet<TypeToCheck> NON_API_TYPES =
6567
ImmutableSet.of(
@@ -96,23 +98,23 @@ public final class NonApiType extends BugChecker implements MethodTreeMatcher {
9698
// ImmutableFoo as params
9799
withPublicVisibility(
98100
isExactType("com.google.common.collect.ImmutableCollection"),
99-
GRAPH_WRAPPER,
101+
NON_GRAPH_WRAPPER,
100102
"Consider accepting a java.util.Collection or Iterable instead. "
101103
+ TYPE_GENERALITY_LINK,
102104
ApiElementType.PARAMETER),
103105
withPublicVisibility(
104106
isExactType("com.google.common.collect.ImmutableList"),
105-
GRAPH_WRAPPER,
107+
NON_GRAPH_WRAPPER,
106108
"Consider accepting a java.util.List or Iterable instead. " + TYPE_GENERALITY_LINK,
107109
ApiElementType.PARAMETER),
108110
withPublicVisibility(
109111
isExactType("com.google.common.collect.ImmutableSet"),
110-
GRAPH_WRAPPER,
112+
NON_GRAPH_WRAPPER,
111113
"Consider accepting a java.util.Set or Iterable instead. " + TYPE_GENERALITY_LINK,
112114
ApiElementType.PARAMETER),
113115
withPublicVisibility(
114116
isExactType("com.google.common.collect.ImmutableMap"),
115-
GRAPH_WRAPPER,
117+
NON_GRAPH_WRAPPER,
116118
"Consider accepting a java.util.Map instead. " + TYPE_GENERALITY_LINK,
117119
ApiElementType.PARAMETER),
118120

@@ -136,6 +138,20 @@ public final class NonApiType extends BugChecker implements MethodTreeMatcher {
136138
"Prefer a java.util.Map instead. " + INTERFACES_NOT_IMPLS_LINK,
137139
ApiElementType.ANY),
138140

141+
// Iterators
142+
withPublicVisibility(
143+
isDescendantOf("java.util.Iterator"),
144+
"Prefer returning a Stream (or collecting to an ImmutableList/ImmutableSet) instead. "
145+
+ ITERATOR_LINK,
146+
ApiElementType.RETURN_TYPE),
147+
// TODO(b/279464660): consider also warning on an Iterator as a ApiElementType.PARAMETER
148+
149+
// Streams
150+
withPublicVisibility(
151+
isDescendantOf("java.util.stream.Stream"),
152+
"Prefer accepting an Iterable or Collection instead. " + STREAM_LINK,
153+
ApiElementType.PARAMETER),
154+
139155
// ProtoTime
140156
withPublicVisibility(
141157
isExactType("com.google.protobuf.Duration"),
@@ -242,8 +258,7 @@ enum ApiVisibility {
242258

243259
private static TypeToCheck withPublicVisibility(
244260
TypePredicate typePredicate, String failureMessage, ApiElementType elementType) {
245-
return withPublicVisibility(
246-
typePredicate, TypePredicates.anything(), failureMessage, elementType);
261+
return withPublicVisibility(typePredicate, anything(), failureMessage, elementType);
247262
}
248263

249264
private static TypeToCheck withPublicVisibility(
@@ -258,7 +273,7 @@ private static TypeToCheck withPublicVisibility(
258273
private static TypeToCheck withAnyVisibility(
259274
TypePredicate typePredicate, String failureMessage, ApiElementType elementType) {
260275
return new AutoValue_NonApiType_TypeToCheck(
261-
typePredicate, TypePredicates.anything(), failureMessage, ApiVisibility.ANY, elementType);
276+
typePredicate, anything(), failureMessage, ApiVisibility.ANY, elementType);
262277
}
263278

264279
@AutoValue

core/src/test/java/com/google/errorprone/bugpatterns/NonApiTypeTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,32 @@ public void normalApisAreNotFlagged() {
235235
}
236236

237237
// Tests below are copied from FloggerPassedAroundTest (so it can be deleted)
238+
239+
@Test
240+
public void streams() {
241+
helper
242+
.addSourceLines(
243+
"Test.java",
244+
"import java.util.stream.Stream;",
245+
"public class Test {",
246+
" // BUG: Diagnostic contains: NonApiType",
247+
" public Test(Stream<String> iterator) {}",
248+
" // BUG: Diagnostic contains: NonApiType",
249+
" public void methodParam(Stream<String> iterator) {}",
250+
"}")
251+
.doTest();
252+
}
253+
254+
@Test
255+
public void iterators() {
256+
helper
257+
.addSourceLines(
258+
"Test.java",
259+
"import java.util.Iterator;",
260+
"public class Test {",
261+
" // BUG: Diagnostic contains: NonApiType",
262+
" public Iterator<String> returnType() { return null; }",
263+
"}")
264+
.doTest();
265+
}
238266
}

0 commit comments

Comments
 (0)