Skip to content

Commit e56e56c

Browse files
committed
use Collection instead of Iterable
1 parent 1479376 commit e56e56c

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

javascript/extractor/src/com/semmle/js/extractor/CFGExtractor.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
import com.semmle.util.trap.TrapWriter.Label;
111111
import java.util.ArrayList;
112112
import java.util.Arrays;
113+
import java.util.Collection;
113114
import java.util.Collections;
114115
import java.util.function.IntFunction;
115116
import java.util.HashMap;
@@ -183,40 +184,41 @@ public CFGExtractor(ASTExtractor astExtractor) {
183184
}
184185

185186
/**
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>`.
187189
*/
188190
@SuppressWarnings("unchecked")
189-
private static Iterable<Node> createIterable(Object nodes) {
191+
private static Collection<Node> createCollection(Object nodes) {
190192
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;
193195
}
194196

195197
/**
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`.
197199
*/
198-
private static Iterable<Node> createReversedIterable(final Object nodes) {
200+
private static Collection<Node> createReversedCollection(final Object nodes) {
199201
List<Node> list = new ArrayList<>();
200-
createIterable(nodes).forEach(list::add);
202+
createCollection(nodes).forEach(list::add);
201203
Collections.reverse(list);
202204
return list;
203205
}
204206

205207
/** 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) {
207209
return flattenNestedList(lists, new ArrayList<>());
208210
}
209211

210212
/**
211213
* Appends all the nodes in a tree of nested lists the given output list, and returns that list.
212214
*/
213-
private static List<Node> flattenNestedList(Iterable<?> lists, List<Node> output) {
215+
private static List<Node> flattenNestedList(Collection<?> lists, List<Node> output) {
214216
for (Object object : lists) {
215217
if (object == null) continue;
216218
if (object instanceof Node) {
217219
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);
220222
} else {
221223
throw new RuntimeException("Cannot flatten object: " + object);
222224
}
@@ -234,7 +236,7 @@ private static Object union(Object xs, Object ys) {
234236
if (xs instanceof List<?>) {
235237
@SuppressWarnings("unchecked")
236238
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);
238240
return xsCopy;
239241
} else {
240242
if (ys instanceof List<?>) {
@@ -262,7 +264,7 @@ private static Object union(Object xs, Object ys) {
262264
*/
263265
private void writeSuccessors(INode prev, Object succs) {
264266
Label prevKey = trapwriter.localID(prev);
265-
for (Node succ : createIterable(succs)) writeSuccessor(prevKey, succ);
267+
for (Node succ : createCollection(succs)) writeSuccessor(prevKey, succ);
266268
}
267269

268270
/**
@@ -947,7 +949,7 @@ private Node visitWithSuccessors(Node nd, Object successors) {
947949
private Object seq(Object... nodes) {
948950
Object fst = nodes[nodes.length - 1];
949951
for (int i = nodes.length - 2; i >= 0; --i) {
950-
for (Node node : createReversedIterable(nodes[i])) {
952+
for (Node node : createReversedCollection(nodes[i])) {
951953
Node ffst = visitWithSuccessors(node, fst);
952954
if (ffst != null) fst = ffst;
953955
}
@@ -1890,7 +1892,7 @@ public Void visit(JSXSpreadAttribute nd, SuccessorInfo c) {
18901892
Label propkey = trapwriter.localID(nd, "JSXSpreadAttribute");
18911893
Label spreadkey = trapwriter.localID(nd);
18921894
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);
18941896
return null;
18951897
}
18961898

0 commit comments

Comments
 (0)