19
19
import static com .google .errorprone .BugPattern .SeverityLevel .WARNING ;
20
20
import static com .google .errorprone .matchers .Description .NO_MATCH ;
21
21
import static com .google .errorprone .predicates .TypePredicates .anyOf ;
22
+ import static com .google .errorprone .predicates .TypePredicates .anything ;
22
23
import static com .google .errorprone .predicates .TypePredicates .isDescendantOf ;
23
24
import static com .google .errorprone .predicates .TypePredicates .isExactType ;
25
+ import static com .google .errorprone .predicates .TypePredicates .not ;
24
26
import static com .google .errorprone .util .ASTHelpers .getSymbol ;
25
27
import static com .google .errorprone .util .ASTHelpers .getType ;
26
28
import static com .google .errorprone .util .ASTHelpers .isSameType ;
33
35
import com .google .errorprone .bugpatterns .BugChecker .MethodTreeMatcher ;
34
36
import com .google .errorprone .matchers .Description ;
35
37
import com .google .errorprone .predicates .TypePredicate ;
36
- import com .google .errorprone .predicates .TypePredicates ;
37
38
import com .sun .source .tree .MethodTree ;
38
39
import com .sun .source .tree .Tree ;
39
40
import com .sun .tools .javac .code .Type ;
@@ -54,12 +55,13 @@ public final class NonApiType extends BugChecker implements MethodTreeMatcher {
54
55
private static final String INTERFACES_NOT_IMPLS_LINK = "" ;
55
56
private static final String PRIMITIVE_ARRAYS_LINK = "" ;
56
57
private static final String PROTO_TIME_SERIALIZATION_LINK = "" ;
58
+ private static final String ITERATOR_LINK = "" ;
59
+ private static final String STREAM_LINK = "" ;
57
60
private static final String OPTIONAL_AS_PARAM_LINK = "" ;
58
61
private static final String PREFER_JDK_OPTIONAL_LINK = "" ;
59
62
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" ));
63
65
64
66
private static final ImmutableSet <TypeToCheck > NON_API_TYPES =
65
67
ImmutableSet .of (
@@ -96,23 +98,23 @@ public final class NonApiType extends BugChecker implements MethodTreeMatcher {
96
98
// ImmutableFoo as params
97
99
withPublicVisibility (
98
100
isExactType ("com.google.common.collect.ImmutableCollection" ),
99
- GRAPH_WRAPPER ,
101
+ NON_GRAPH_WRAPPER ,
100
102
"Consider accepting a java.util.Collection or Iterable instead. "
101
103
+ TYPE_GENERALITY_LINK ,
102
104
ApiElementType .PARAMETER ),
103
105
withPublicVisibility (
104
106
isExactType ("com.google.common.collect.ImmutableList" ),
105
- GRAPH_WRAPPER ,
107
+ NON_GRAPH_WRAPPER ,
106
108
"Consider accepting a java.util.List or Iterable instead. " + TYPE_GENERALITY_LINK ,
107
109
ApiElementType .PARAMETER ),
108
110
withPublicVisibility (
109
111
isExactType ("com.google.common.collect.ImmutableSet" ),
110
- GRAPH_WRAPPER ,
112
+ NON_GRAPH_WRAPPER ,
111
113
"Consider accepting a java.util.Set or Iterable instead. " + TYPE_GENERALITY_LINK ,
112
114
ApiElementType .PARAMETER ),
113
115
withPublicVisibility (
114
116
isExactType ("com.google.common.collect.ImmutableMap" ),
115
- GRAPH_WRAPPER ,
117
+ NON_GRAPH_WRAPPER ,
116
118
"Consider accepting a java.util.Map instead. " + TYPE_GENERALITY_LINK ,
117
119
ApiElementType .PARAMETER ),
118
120
@@ -136,6 +138,20 @@ public final class NonApiType extends BugChecker implements MethodTreeMatcher {
136
138
"Prefer a java.util.Map instead. " + INTERFACES_NOT_IMPLS_LINK ,
137
139
ApiElementType .ANY ),
138
140
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
+
139
155
// ProtoTime
140
156
withPublicVisibility (
141
157
isExactType ("com.google.protobuf.Duration" ),
@@ -242,8 +258,7 @@ enum ApiVisibility {
242
258
243
259
private static TypeToCheck withPublicVisibility (
244
260
TypePredicate typePredicate , String failureMessage , ApiElementType elementType ) {
245
- return withPublicVisibility (
246
- typePredicate , TypePredicates .anything (), failureMessage , elementType );
261
+ return withPublicVisibility (typePredicate , anything (), failureMessage , elementType );
247
262
}
248
263
249
264
private static TypeToCheck withPublicVisibility (
@@ -258,7 +273,7 @@ private static TypeToCheck withPublicVisibility(
258
273
private static TypeToCheck withAnyVisibility (
259
274
TypePredicate typePredicate , String failureMessage , ApiElementType elementType ) {
260
275
return new AutoValue_NonApiType_TypeToCheck (
261
- typePredicate , TypePredicates . anything (), failureMessage , ApiVisibility .ANY , elementType );
276
+ typePredicate , anything (), failureMessage , ApiVisibility .ANY , elementType );
262
277
}
263
278
264
279
@ AutoValue
0 commit comments