110
110
import com .semmle .util .trap .TrapWriter .Label ;
111
111
import java .util .ArrayList ;
112
112
import java .util .Arrays ;
113
+ import java .util .Collection ;
113
114
import java .util .Collections ;
114
115
import java .util .function .IntFunction ;
115
116
import java .util .HashMap ;
@@ -183,40 +184,41 @@ public CFGExtractor(ASTExtractor astExtractor) {
183
184
}
184
185
185
186
/**
186
- * Creates an `Iterable<Node>` from the one-or-more nodes contained in `nodes`.
187
+ * Creates an `Collection<Node>` from the one-or-more nodes contained in `nodes`.
188
+ * `nodes` is either `null`, a `Node`, or a `Collection<Node>`.
187
189
*/
188
190
@ SuppressWarnings ("unchecked" )
189
- private static Iterable <Node > createIterable (Object nodes ) {
191
+ private static Collection <Node > createCollection (Object nodes ) {
190
192
if (nodes == null ) return Collections .<Node >emptySet ();
191
- if (nodes instanceof Node ) return CollectionUtil . singletonIterable ((Node ) nodes );
192
- return (Iterable <Node >) nodes ;
193
+ if (nodes instanceof Node ) return Collections .< Node > singleton ((Node ) nodes );
194
+ return (Collection <Node >) nodes ;
193
195
}
194
196
195
197
/**
196
- * Creates an `Iterable <Node>` that iterates the nodes in reverse order from the one-or-more nodes contained in `nodes`.
198
+ * Creates an `Collection <Node>` that iterates the nodes in reverse order from the one-or-more nodes contained in `nodes`.
197
199
*/
198
- private static Iterable <Node > createReversedIterable (final Object nodes ) {
200
+ private static Collection <Node > createReversedCollection (final Object nodes ) {
199
201
List <Node > list = new ArrayList <>();
200
- createIterable (nodes ).forEach (list ::add );
202
+ createCollection (nodes ).forEach (list ::add );
201
203
Collections .reverse (list );
202
204
return list ;
203
205
}
204
206
205
207
/** Returns a list of all the nodes in a tree of nested lists. */
206
- private static List <Node > flattenNestedList (Iterable <?> lists ) {
208
+ private static List <Node > flattenNestedList (Collection <?> lists ) {
207
209
return flattenNestedList (lists , new ArrayList <>());
208
210
}
209
211
210
212
/**
211
213
* Appends all the nodes in a tree of nested lists the given output list, and returns that list.
212
214
*/
213
- private static List <Node > flattenNestedList (Iterable <?> lists , List <Node > output ) {
215
+ private static List <Node > flattenNestedList (Collection <?> lists , List <Node > output ) {
214
216
for (Object object : lists ) {
215
217
if (object == null ) continue ;
216
218
if (object instanceof Node ) {
217
219
output .add ((Node ) object );
218
- } else if (object instanceof Iterable <?>) {
219
- flattenNestedList ((Iterable <?>) object , output );
220
+ } else if (object instanceof Collection <?>) {
221
+ flattenNestedList ((Collection <?>) object , output );
220
222
} else {
221
223
throw new RuntimeException ("Cannot flatten object: " + object );
222
224
}
@@ -234,7 +236,7 @@ private static Object union(Object xs, Object ys) {
234
236
if (xs instanceof List <?>) {
235
237
@ SuppressWarnings ("unchecked" )
236
238
List <Node > xsCopy = new ArrayList <Node >((List <Node >) xs );
237
- for (Node y : createIterable (ys )) if (!xsCopy .contains (y )) xsCopy .add (y );
239
+ for (Node y : createCollection (ys )) if (!xsCopy .contains (y )) xsCopy .add (y );
238
240
return xsCopy ;
239
241
} else {
240
242
if (ys instanceof List <?>) {
@@ -262,7 +264,7 @@ private static Object union(Object xs, Object ys) {
262
264
*/
263
265
private void writeSuccessors (INode prev , Object succs ) {
264
266
Label prevKey = trapwriter .localID (prev );
265
- for (Node succ : createIterable (succs )) writeSuccessor (prevKey , succ );
267
+ for (Node succ : createCollection (succs )) writeSuccessor (prevKey , succ );
266
268
}
267
269
268
270
/**
@@ -947,7 +949,7 @@ private Node visitWithSuccessors(Node nd, Object successors) {
947
949
private Object seq (Object ... nodes ) {
948
950
Object fst = nodes [nodes .length - 1 ];
949
951
for (int i = nodes .length - 2 ; i >= 0 ; --i ) {
950
- for (Node node : createReversedIterable (nodes [i ])) {
952
+ for (Node node : createReversedCollection (nodes [i ])) {
951
953
Node ffst = visitWithSuccessors (node , fst );
952
954
if (ffst != null ) fst = ffst ;
953
955
}
@@ -1890,7 +1892,7 @@ public Void visit(JSXSpreadAttribute nd, SuccessorInfo c) {
1890
1892
Label propkey = trapwriter .localID (nd , "JSXSpreadAttribute" );
1891
1893
Label spreadkey = trapwriter .localID (nd );
1892
1894
trapwriter .addTuple ("successor" , spreadkey , propkey );
1893
- for (Node succ : createIterable (c .getAllSuccessors ())) writeSuccessor (propkey , succ );
1895
+ for (Node succ : createCollection (c .getAllSuccessors ())) writeSuccessor (propkey , succ );
1894
1896
return null ;
1895
1897
}
1896
1898
0 commit comments