From e08ed2663f3b0a2572fea0374a99b4677e8aace5 Mon Sep 17 00:00:00 2001 From: msridhar1 Date: Thu, 9 Apr 2009 15:24:42 +0000 Subject: [PATCH] Deprecate Iterator2Collection.toCollection(); use toSet() or toList() instead. git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@3421 f5eafffb-2e1d-0410-98e4-8ec43c5233c4 --- .../wala/core/tests/basic/PrimitivesTest.java | 14 +++---- .../src/com/ibm/wala/cfg/AbstractCFG.java | 14 +++---- .../ibm/wala/classLoader/ClassLoaderImpl.java | 4 +- .../dataflow/IFDS/BackwardsSupergraph.java | 2 +- .../wala/dataflow/IFDS/TabulationSolver.java | 2 +- .../alg/DemandRefinementPointsTo.java | 2 +- .../demandpa/alg/SimpleDemandPointsTo.java | 2 +- .../wala/escape/LocalLiveRangeAnalysis.java | 2 +- .../propagation/PointerAnalysisImpl.java | 2 +- .../propagation/PreTransitiveSolver.java | 2 +- .../propagation/PropagationSystem.java | 4 +- .../propagation/ReflectionHandler.java | 2 +- .../rta/TypeBasedPointerAnalysis.java | 2 +- .../src/com/ibm/wala/ipa/cfg/PrunedCFG.java | 10 ++--- .../com/ibm/wala/ipa/cha/ClassHierarchy.java | 2 +- .../ibm/wala/ipa/slicer/HeapReachingDefs.java | 41 +++++++----------- .../src/com/ibm/wala/ipa/slicer/PDG.java | 4 +- .../src/com/ibm/wala/ipa/slicer/SDG.java | 6 +-- .../com/ibm/wala/ipa/slicer/thin/CISDG.java | 4 +- .../src/com/ibm/wala/ssa/SSACFG.java | 2 +- .../util/collections/Iterator2Collection.java | 42 +++++++++++++++---- .../wala/util/graph/GraphReachability.java | 2 +- .../SparseNumberedLabeledEdgeManager.java | 4 +- .../com/ibm/wala/util/graph/traverse/DFS.java | 2 +- .../com/ibm/wala/util/intset/OrdinalSet.java | 4 +- .../src/com/ibm/wala/viz/DotUtil.java | 2 +- .../com/ibm/wala/j2ee/J2EEContainerModel.java | 2 +- 27 files changed, 99 insertions(+), 82 deletions(-) diff --git a/com.ibm.wala.core.tests/src/com/ibm/wala/core/tests/basic/PrimitivesTest.java b/com.ibm.wala.core.tests/src/com/ibm/wala/core/tests/basic/PrimitivesTest.java index d64f639ea..1f356f7b3 100644 --- a/com.ibm.wala.core.tests/src/com/ibm/wala/core/tests/basic/PrimitivesTest.java +++ b/com.ibm.wala.core.tests/src/com/ibm/wala/core/tests/basic/PrimitivesTest.java @@ -677,31 +677,31 @@ public class PrimitivesTest extends WalaTestCase { NumberedGraph G = makeBFSTestGraph(); BoundedBFSIterator bfs = new BoundedBFSIterator(G, G.getNode(0), 0); - Collection c = Iterator2Collection.toCollection(bfs); + Collection c = Iterator2Collection.toSet(bfs); assertTrue(c.size() == 1); bfs = new BoundedBFSIterator(G, G.getNode(0), 1); - c = Iterator2Collection.toCollection(bfs); + c = Iterator2Collection.toSet(bfs); assertTrue(c.size() == 3); bfs = new BoundedBFSIterator(G, G.getNode(0), 2); - c = Iterator2Collection.toCollection(bfs); + c = Iterator2Collection.toSet(bfs); assertTrue(c.size() == 5); bfs = new BoundedBFSIterator(G, G.getNode(0), 3); - c = Iterator2Collection.toCollection(bfs); + c = Iterator2Collection.toSet(bfs); assertTrue(c.size() == 7); bfs = new BoundedBFSIterator(G, G.getNode(0), 4); - c = Iterator2Collection.toCollection(bfs); + c = Iterator2Collection.toSet(bfs); assertTrue(c.size() == 9); bfs = new BoundedBFSIterator(G, G.getNode(0), 5); - c = Iterator2Collection.toCollection(bfs); + c = Iterator2Collection.toSet(bfs); assertTrue(c.size() == 10); bfs = new BoundedBFSIterator(G, G.getNode(0), 500); - c = Iterator2Collection.toCollection(bfs); + c = Iterator2Collection.toSet(bfs); assertTrue(c.size() == 10); } diff --git a/com.ibm.wala.core/src/com/ibm/wala/cfg/AbstractCFG.java b/com.ibm.wala.core/src/com/ibm/wala/cfg/AbstractCFG.java index e19edcd12..9a24b55cd 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/cfg/AbstractCFG.java +++ b/com.ibm.wala.core/src/com/ibm/wala/cfg/AbstractCFG.java @@ -146,7 +146,7 @@ public abstract class AbstractCFG> implements Contro boolean exceptionalIn = getNumberOfExceptionalIn(N) > 0; if (normalIn) { if (exceptionalIn) { - return Iterator2Collection.toCollection(getPredNodes(N)).size(); + return Iterator2Collection.toSet(getPredNodes(N)).size(); } else { return getNumberOfNormalIn(N); } @@ -241,8 +241,8 @@ public abstract class AbstractCFG> implements Contro if (normalIn) { if (exceptionalIn) { HashSet result = HashSetFactory.make(getNumberOfNormalIn(N) + getNumberOfExceptionalIn(N)); - result.addAll(Iterator2Collection.toCollection(normalEdgeManager.getPredNodes(N))); - result.addAll(Iterator2Collection.toCollection(exceptionalEdgeManager.getPredNodes(N))); + result.addAll(Iterator2Collection.toSet(normalEdgeManager.getPredNodes(N))); + result.addAll(Iterator2Collection.toSet(exceptionalEdgeManager.getPredNodes(N))); if (fallThru.get(number - 1)) { result.add(getNode(number - 1)); } @@ -300,7 +300,7 @@ public abstract class AbstractCFG> implements Contro } private int slowCountSuccNodes(T N) { - return Iterator2Collection.toCollection(getSuccNodes(N)).size(); + return Iterator2Collection.toSet(getSuccNodes(N)).size(); } public Iterator getSuccNodes(T N) { @@ -595,7 +595,7 @@ public abstract class AbstractCFG> implements Contro if (b == null) { throw new IllegalArgumentException("b is null"); } - return Iterator2Collection.toCollection(iterateNormalSuccessors(b.getNumber())); + return Iterator2Collection.toSet(iterateNormalSuccessors(b.getNumber())); } /* @@ -628,7 +628,7 @@ public abstract class AbstractCFG> implements Contro if (b == null) { throw new IllegalArgumentException("b is null"); } - return Iterator2Collection.toCollection(iterateExceptionalPredecessors(b)); + return Iterator2Collection.toSet(iterateExceptionalPredecessors(b)); } /* @@ -638,7 +638,7 @@ public abstract class AbstractCFG> implements Contro if (b == null) { throw new IllegalArgumentException("b is null"); } - return Iterator2Collection.toCollection(iterateNormalPredecessors(b)); + return Iterator2Collection.toSet(iterateNormalPredecessors(b)); } public IntSet getPredNodeNumbers(T node) throws UnimplementedError { diff --git a/com.ibm.wala.core/src/com/ibm/wala/classLoader/ClassLoaderImpl.java b/com.ibm.wala.core/src/com/ibm/wala/classLoader/ClassLoaderImpl.java index 6d3e1d18a..329a850b7 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/classLoader/ClassLoaderImpl.java +++ b/com.ibm.wala.core/src/com/ibm/wala/classLoader/ClassLoaderImpl.java @@ -112,7 +112,7 @@ public class ClassLoaderImpl implements IClassLoader { System.err.println("Get source files for " + M); } TreeSet sortedEntries = new TreeSet(HashCodeComparator.instance()); - sortedEntries.addAll(Iterator2Collection.toCollection(M.getEntries())); + sortedEntries.addAll(Iterator2Collection.toSet(M.getEntries())); HashSet result = HashSetFactory.make(); for (Iterator it = sortedEntries.iterator(); it.hasNext();) { @@ -144,7 +144,7 @@ public class ClassLoaderImpl implements IClassLoader { System.err.println("Get class files for " + M); } TreeSet sortedEntries = new TreeSet(HashCodeComparator.instance()); - sortedEntries.addAll(Iterator2Collection.toCollection(M.getEntries())); + sortedEntries.addAll(Iterator2Collection.toSet(M.getEntries())); HashSet result = HashSetFactory.make(); for (Iterator it = sortedEntries.iterator(); it.hasNext();) { diff --git a/com.ibm.wala.core/src/com/ibm/wala/dataflow/IFDS/BackwardsSupergraph.java b/com.ibm.wala.core/src/com/ibm/wala/dataflow/IFDS/BackwardsSupergraph.java index ea97a6213..e5516958c 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/dataflow/IFDS/BackwardsSupergraph.java +++ b/com.ibm.wala.core/src/com/ibm/wala/dataflow/IFDS/BackwardsSupergraph.java @@ -93,7 +93,7 @@ public class BackwardsSupergraph implements ISupergraph { if (DEBUG_LEVEL > 1) { System.err.println(getClass() + " getCalledNodes " + ret); System.err.println("called nodes: " - + Iterator2Collection.toCollection(new FilterIterator(getSuccNodes(ret), exitFilter))); + + Iterator2Collection.toSet(new FilterIterator(getSuccNodes(ret), exitFilter))); } return new FilterIterator(getSuccNodes(ret), exitFilter); } diff --git a/com.ibm.wala.core/src/com/ibm/wala/dataflow/IFDS/TabulationSolver.java b/com.ibm.wala.core/src/com/ibm/wala/dataflow/IFDS/TabulationSolver.java index 4579dc16a..db9b2f928 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/dataflow/IFDS/TabulationSolver.java +++ b/com.ibm.wala.core/src/com/ibm/wala/dataflow/IFDS/TabulationSolver.java @@ -555,7 +555,7 @@ public class TabulationSolver { System.err.println(" process callee: " + callee); } MutableSparseIntSet reached = MutableSparseIntSet.makeEmpty(); - final Collection returnSitesForCallee = Iterator2Collection.toCollection(supergraph.getReturnSites(edge.target, supergraph + final Collection returnSitesForCallee = Iterator2Collection.toSet(supergraph.getReturnSites(edge.target, supergraph .getProcOf(callee))); allReturnSites.addAll(returnSitesForCallee); // we modify this to handle each return site individually. Some types of problems diff --git a/com.ibm.wala.core/src/com/ibm/wala/demandpa/alg/DemandRefinementPointsTo.java b/com.ibm.wala.core/src/com/ibm/wala/demandpa/alg/DemandRefinementPointsTo.java index 9d4d6a846..deec87c3a 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/demandpa/alg/DemandRefinementPointsTo.java +++ b/com.ibm.wala.core/src/com/ibm/wala/demandpa/alg/DemandRefinementPointsTo.java @@ -677,7 +677,7 @@ public class DemandRefinementPointsTo extends AbstractDemandPointsTo { } public Collection getP2Set(LocalPointerKey lpk) { - return Iterator2Collection.toCollection(new MapIterator(makeOrdinalSet( + return Iterator2Collection.toSet(new MapIterator(makeOrdinalSet( find(pkToP2Set, new PointerKeyAndState(lpk, stateMachine.getStartState()))).iterator(), new Function() { diff --git a/com.ibm.wala.core/src/com/ibm/wala/demandpa/alg/SimpleDemandPointsTo.java b/com.ibm.wala.core/src/com/ibm/wala/demandpa/alg/SimpleDemandPointsTo.java index 72f362282..eb05ce6a6 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/demandpa/alg/SimpleDemandPointsTo.java +++ b/com.ibm.wala.core/src/com/ibm/wala/demandpa/alg/SimpleDemandPointsTo.java @@ -106,7 +106,7 @@ public class SimpleDemandPointsTo extends AbstractDemandPointsTo { SlowDFSDiscoverTimeIterator dfs = new SlowDFSDiscoverTimeIterator(g, pk); // Collection reached = // DFS.getReachableNodes(g,Collections.singleton(pk)); - return Iterator2Collection.toCollection((Iterator)(Iterator)new FilterIterator(dfs, iKeyFilter)); + return Iterator2Collection.toSet((Iterator)(Iterator)new FilterIterator(dfs, iKeyFilter)); } } diff --git a/com.ibm.wala.core/src/com/ibm/wala/escape/LocalLiveRangeAnalysis.java b/com.ibm.wala.core/src/com/ibm/wala/escape/LocalLiveRangeAnalysis.java index b4621ffe0..282680a7f 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/escape/LocalLiveRangeAnalysis.java +++ b/com.ibm.wala.core/src/com/ibm/wala/escape/LocalLiveRangeAnalysis.java @@ -105,7 +105,7 @@ public class LocalLiveRangeAnalysis { * Iterator */ private static Collection findBlocks(IR ir, Iterator statements) { - Collection s = Iterator2Collection.toCollection(statements); + Collection s = Iterator2Collection.toSet(statements); Collection result = HashSetFactory.make(); outer: for (Iterator it = ir.getControlFlowGraph().iterator(); it.hasNext();) { SSACFG.BasicBlock b = (SSACFG.BasicBlock) it.next(); diff --git a/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/PointerAnalysisImpl.java b/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/PointerAnalysisImpl.java index a179d9300..890729e2b 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/PointerAnalysisImpl.java +++ b/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/PointerAnalysisImpl.java @@ -548,7 +548,7 @@ public class PointerAnalysisImpl extends AbstractPointerAnalysis { * @see com.ibm.wala.ipa.callgraph.propagation.PointerAnalysis#iteratePointerKeys() */ public Collection getPointerKeys() { - return Iterator2Collection.toCollection(pointsToMap.iterateKeys()); + return Iterator2Collection.toSet(pointsToMap.iterateKeys()); } public IClassHierarchy getClassHierarchy() { diff --git a/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/PreTransitiveSolver.java b/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/PreTransitiveSolver.java index 1c41ffc93..549201afa 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/PreTransitiveSolver.java +++ b/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/PreTransitiveSolver.java @@ -144,7 +144,7 @@ public class PreTransitiveSolver extends AbstractPointsToSolver { } // cache the predecessors before unification might screw things up. - Iterator2Collection origPred = Iterator2Collection.toCollection(ag.getPredNodes(v)); + Iterator2Collection origPred = Iterator2Collection.toSet(ag.getPredNodes(v)); for (Iterator it = origPred.iterator(); it.hasNext();) { PointsToSetVariable n = (PointsToSetVariable) it.next(); diff --git a/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/PropagationSystem.java b/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/PropagationSystem.java index 004ffceab..f696e0181 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/PropagationSystem.java +++ b/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/PropagationSystem.java @@ -838,7 +838,7 @@ public class PropagationSystem extends DefaultFixedPointSolver casts = Iterator2Collection.toCollection(new FilterIterator(slice.iterator(), f)); + Collection casts = Iterator2Collection.toSet(new FilterIterator(slice.iterator(), f)); changedNodes.addAll(modifyFactoryInterpreter(st, casts, builder.getContextInterpreter(), builder.getClassHierarchy())); } for (Iterator it = changedNodes.iterator(); it.hasNext();) { diff --git a/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/rta/TypeBasedPointerAnalysis.java b/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/rta/TypeBasedPointerAnalysis.java index b26b561c9..18bd72223 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/rta/TypeBasedPointerAnalysis.java +++ b/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/rta/TypeBasedPointerAnalysis.java @@ -177,7 +177,7 @@ public class TypeBasedPointerAnalysis extends AbstractPointerAnalysis { } public Collection getPointerKeys() { - return Iterator2Collection.toCollection(heapModel.iteratePointerKeys()); + return Iterator2Collection.toSet(heapModel.iteratePointerKeys()); } public boolean isFiltered(PointerKey pk) { diff --git a/com.ibm.wala.core/src/com/ibm/wala/ipa/cfg/PrunedCFG.java b/com.ibm.wala.core/src/com/ibm/wala/ipa/cfg/PrunedCFG.java index f70176314..14db979a2 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/ipa/cfg/PrunedCFG.java +++ b/com.ibm.wala.core/src/com/ibm/wala/ipa/cfg/PrunedCFG.java @@ -117,7 +117,7 @@ public class PrunedCFG> extends AbstractNumberedGrap } public int getSuccNodeCount(T N) { - return Iterator2Collection.toCollection(getSuccNodes(N)).size(); + return Iterator2Collection.toSet(getSuccNodes(N)).size(); } public IntSet getSuccNodeNumbers(T N) { @@ -139,7 +139,7 @@ public class PrunedCFG> extends AbstractNumberedGrap } public int getPredNodeCount(T N) { - return Iterator2Collection.toCollection(getPredNodes(N)).size(); + return Iterator2Collection.toSet(getPredNodes(N)).size(); } public IntSet getPredNodeNumbers(T N) { @@ -301,15 +301,15 @@ public class PrunedCFG> extends AbstractNumberedGrap } public Collection getNormalSuccessors(final T N) { - return Iterator2Collection.toCollection(edges.getNormalSuccessors(N)); + return Iterator2Collection.toSet(edges.getNormalSuccessors(N)); } public Collection getExceptionalPredecessors(final T N) { - return Iterator2Collection.toCollection(edges.getExceptionalPredecessors(N)); + return Iterator2Collection.toSet(edges.getExceptionalPredecessors(N)); } public Collection getNormalPredecessors(final T N) { - return Iterator2Collection.toCollection(edges.getNormalPredecessors(N)); + return Iterator2Collection.toSet(edges.getNormalPredecessors(N)); } public T entry() { diff --git a/com.ibm.wala.core/src/com/ibm/wala/ipa/cha/ClassHierarchy.java b/com.ibm.wala.core/src/com/ibm/wala/ipa/cha/ClassHierarchy.java index 19e378021..fc1e4baf4 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/ipa/cha/ClassHierarchy.java +++ b/com.ibm.wala.core/src/com/ibm/wala/ipa/cha/ClassHierarchy.java @@ -1109,7 +1109,7 @@ public class ClassHierarchy implements IClassHierarchy { return n.klass; } }; - return Iterator2Collection.toCollection(new MapIterator(findNode(klass).children.iterator(), node2Class)); + return Iterator2Collection.toSet(new MapIterator(findNode(klass).children.iterator(), node2Class)); } private Collection getImmediateArraySubclasses(IClass klass) { diff --git a/com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/HeapReachingDefs.java b/com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/HeapReachingDefs.java index c8028cdca..2903a5194 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/HeapReachingDefs.java +++ b/com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/HeapReachingDefs.java @@ -48,6 +48,7 @@ import com.ibm.wala.util.collections.Filter; import com.ibm.wala.util.collections.FilterIterator; import com.ibm.wala.util.collections.HashMapFactory; import com.ibm.wala.util.collections.Iterator2Collection; +import com.ibm.wala.util.collections.Iterator2Iterable; import com.ibm.wala.util.collections.ObjectArrayMapping; import com.ibm.wala.util.debug.Assertions; import com.ibm.wala.util.intset.BasicNaturalRelation; @@ -81,24 +82,16 @@ public class HeapReachingDefs { /** * For each statement s, return the set of statements that may def the heap value read by s. * - * @param node - * the node we are computing heap reaching defs for - * @param ir - * IR for the node - * @param pa - * governing pointer analysis - * @param mod - * the set of heap locations which may be written (transitively) by this node. These are logically return - * values in the SDG. - * @param statements - * the statements whose def-use are considered interesting - * @param exclusions - * heap locations that should be excluded from data dependence tracking + * @param node the node we are computing heap reaching defs for + * @param ir IR for the node + * @param pa governing pointer analysis + * @param mod the set of heap locations which may be written (transitively) by this node. These are logically return values in the + * SDG. + * @param statements the statements whose def-use are considered interesting + * @param exclusions heap locations that should be excluded from data dependence tracking * - * @throws IllegalArgumentException - * if pa is null - * @throws IllegalArgumentException - * if statements is null + * @throws IllegalArgumentException if pa is null + * @throws IllegalArgumentException if statements is null */ public Map> computeReachingDefs(CGNode node, IR ir, PointerAnalysis pa, Map> mod, Collection statements, HeapExclusions exclusions, CallGraph cg) { @@ -482,8 +475,8 @@ public class HeapReachingDefs { private final HeapExclusions exclusions; /** - * if (i,j) \in heapReturnCaller, then statement j is a HeapStatement.ReturnCaller for statement i, a - * NormalStatement representing an invoke + * if (i,j) \in heapReturnCaller, then statement j is a HeapStatement.ReturnCaller for statement i, a NormalStatement + * representing an invoke */ private final IBinaryNaturalRelation heapReturnCaller = new BasicNaturalRelation(); @@ -523,7 +516,7 @@ public class HeapReachingDefs { System.err.println("heapEntry " + heapEntryStatements()); } return new BitVectorUnionVector(new BitVectorIntSet(heapEntryStatements()).getBitVector()); - } + } if (src.getInstruction() != null && !(src.getInstruction() instanceof SSAAbstractInvokeInstruction) && !cfg.getNormalSuccessors(src).contains(dst)) { // if the edge only happens due to exceptional control flow, then no @@ -640,8 +633,8 @@ public class HeapReachingDefs { return o instanceof StaticFieldKey; } }; - final Collection kill = Iterator2Collection.toCollection(new FilterIterator(mod.iterator(), - staticFilter)); + final Collection kill = Iterator2Collection + .toSet(new FilterIterator(mod.iterator(), staticFilter)); if (kill.isEmpty()) { return null; } else { @@ -658,10 +651,8 @@ public class HeapReachingDefs { return false; } }; - Collection killedStatements = Iterator2Collection.toCollection(new FilterIterator(domain - .iterator(), f)); BitVector result = new BitVector(); - for (Statement k : killedStatements) { + for (Statement k : Iterator2Iterable.make(new FilterIterator(domain.iterator(), f))) { result.set(domain.getMappedIndex(k)); } return result; diff --git a/com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/PDG.java b/com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/PDG.java index 9f63798c8..1892f90f6 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/PDG.java +++ b/com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/PDG.java @@ -648,7 +648,7 @@ public class PDG implements NumberedGraph { } } }; - Collection relevantStatements = Iterator2Collection.toCollection(new FilterIterator(iterator(), f)); + Collection relevantStatements = Iterator2Collection.toSet(new FilterIterator(iterator(), f)); Map> heapReachingDefs = new HeapReachingDefs(modRef).computeReachingDefs(node, ir, pa, mod, relevantStatements, new HeapExclusions(SetComplement.complement(new SingletonSet(t))), cg); @@ -741,7 +741,7 @@ public class PDG implements NumberedGraph { } } }; - return Iterator2Collection.toCollection(new FilterIterator(iterator(), filter)); + return Iterator2Collection.toSet(new FilterIterator(iterator(), filter)); } /** diff --git a/com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/SDG.java b/com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/SDG.java index 6d721338e..c695d0a50 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/SDG.java +++ b/com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/SDG.java @@ -312,7 +312,7 @@ public class SDG extends AbstractNumberedGraph implements ISDG { case EXC_RET_CALLER: { ExceptionalReturnCaller nrc = (ExceptionalReturnCaller) N; SSAAbstractInvokeInstruction call = nrc.getInstruction(); - Collection result = Iterator2Collection.toCollection(getPDG(N.getNode()).getPredNodes(N)); + Collection result = Iterator2Collection.toSet(getPDG(N.getNode()).getPredNodes(N)); if (!dOptions.equals(DataDependenceOptions.NONE)) { // data dependence predecessors for (CGNode t : cg.getPossibleTargets(N.getNode(), call.getCallSite())) { @@ -326,7 +326,7 @@ public class SDG extends AbstractNumberedGraph implements ISDG { case NORMAL_RET_CALLER: { NormalReturnCaller nrc = (NormalReturnCaller) N; SSAAbstractInvokeInstruction call = nrc.getInstruction(); - Collection result = Iterator2Collection.toCollection(getPDG(N.getNode()).getPredNodes(N)); + Collection result = Iterator2Collection.toSet(getPDG(N.getNode()).getPredNodes(N)); if (!dOptions.equals(DataDependenceOptions.NONE)) { // data dependence predecessors for (CGNode t : cg.getPossibleTargets(N.getNode(), call.getCallSite())) { @@ -340,7 +340,7 @@ public class SDG extends AbstractNumberedGraph implements ISDG { case HEAP_RET_CALLER: { HeapStatement.HeapReturnCaller r = (HeapStatement.HeapReturnCaller) N; SSAAbstractInvokeInstruction call = r.getCall(); - Collection result = Iterator2Collection.toCollection(getPDG(N.getNode()).getPredNodes(N)); + Collection result = Iterator2Collection.toSet(getPDG(N.getNode()).getPredNodes(N)); if (!dOptions.equals(DataDependenceOptions.NONE)) { // data dependence predecessors for (CGNode t : cg.getPossibleTargets(N.getNode(), call.getCallSite())) { diff --git a/com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/thin/CISDG.java b/com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/thin/CISDG.java index f76b3bd8e..26f7c82d4 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/thin/CISDG.java +++ b/com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/thin/CISDG.java @@ -141,7 +141,7 @@ public class CISDG implements ISDG { pred.addAll(invMod.get(p)); } } - pred.addAll(Iterator2Collection.toCollection(noHeap.getPredNodes(N))); + pred.addAll(Iterator2Collection.toSet(noHeap.getPredNodes(N))); return pred.iterator(); } } @@ -168,7 +168,7 @@ public class CISDG implements ISDG { succ.addAll(invRef.get(p)); } } - succ.addAll(Iterator2Collection.toCollection(noHeap.getSuccNodes(N))); + succ.addAll(Iterator2Collection.toSet(noHeap.getSuccNodes(N))); return succ.iterator(); } } diff --git a/com.ibm.wala.core/src/com/ibm/wala/ssa/SSACFG.java b/com.ibm.wala.core/src/com/ibm/wala/ssa/SSACFG.java index ed617c784..a1e780d05 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/ssa/SSACFG.java +++ b/com.ibm.wala.core/src/com/ibm/wala/ssa/SSACFG.java @@ -984,7 +984,7 @@ public class SSACFG implements ControlFlowGraph return basicBlocks[cfg.getNumber(object)]; } }; - return Iterator2Collection.toCollection(new MapIterator, ISSABasicBlock>(cfg.getExceptionalPredecessors(n) + return Iterator2Collection.toSet(new MapIterator, ISSABasicBlock>(cfg.getExceptionalPredecessors(n) .iterator(), f)); } diff --git a/com.ibm.wala.core/src/com/ibm/wala/util/collections/Iterator2Collection.java b/com.ibm.wala.core/src/com/ibm/wala/util/collections/Iterator2Collection.java index e60ed6377..bbc9f1fed 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/util/collections/Iterator2Collection.java +++ b/com.ibm.wala.core/src/com/ibm/wala/util/collections/Iterator2Collection.java @@ -10,39 +10,65 @@ *******************************************************************************/ package com.ibm.wala.util.collections; +import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; /** - * Converts an iterator to a collection + * Converts an {@link Iterator} to a {@link Collection}. Note that if you just want to use Java 5's for-each loop with an + * {@link Iterator}, use {@link Iterator2Iterable}. * - * @author sfink + * @see Iterator2Iterable */ public class Iterator2Collection implements Collection { private final Collection delegate; - - private Iterator2Collection(Iterator i) { - delegate = new LinkedHashSet(5); + private Iterator2Collection(Iterator i, Collection delegate) { + this.delegate = delegate; while (i.hasNext()) { delegate.add(i.next()); } } - + + /** + * Just calls {@link #toSet(Iterator)} + * + * @deprecated + */ + @Deprecated public static Iterator2Collection toCollection(Iterator i) throws IllegalArgumentException { + return toSet(i); + } + + /** + * Returns a {@link Set} containing all elements in i. Note that duplicates will be removed. + */ + public static Iterator2Collection toSet(Iterator i) throws IllegalArgumentException { if (i == null) { throw new IllegalArgumentException("i == null"); } - return new Iterator2Collection(i); + return new Iterator2Collection(i, new LinkedHashSet(5)); + } + + /** + * Returns a {@link List} containing all elements in i, preserving duplicates. + */ + public static Iterator2Collection toList(Iterator i) throws IllegalArgumentException { + if (i == null) { + throw new IllegalArgumentException("i == null"); + } + return new Iterator2Collection(i, new ArrayList(5)); } @Override public String toString() { return delegate.toString(); } - + /* * @see java.util.Collection#size() */ diff --git a/com.ibm.wala.core/src/com/ibm/wala/util/graph/GraphReachability.java b/com.ibm.wala.core/src/com/ibm/wala/util/graph/GraphReachability.java index c9a8c00d0..6591dd2d4 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/util/graph/GraphReachability.java +++ b/com.ibm.wala.core/src/com/ibm/wala/util/graph/GraphReachability.java @@ -72,7 +72,7 @@ public class GraphReachability { } this.g = g; Iterator i = new FilterIterator(g.iterator(), filter); - domain = new MutableMapping((Iterator2Collection.toCollection(i)).toArray()); + domain = new MutableMapping((Iterator2Collection.toSet(i)).toArray()); } /** diff --git a/com.ibm.wala.core/src/com/ibm/wala/util/graph/labeled/SparseNumberedLabeledEdgeManager.java b/com.ibm.wala.core/src/com/ibm/wala/util/graph/labeled/SparseNumberedLabeledEdgeManager.java index f90e8e040..034c45b6d 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/util/graph/labeled/SparseNumberedLabeledEdgeManager.java +++ b/com.ibm.wala.core/src/com/ibm/wala/util/graph/labeled/SparseNumberedLabeledEdgeManager.java @@ -230,7 +230,7 @@ public class SparseNumberedLabeledEdgeManager implements NumberedLabeledEd public Iterator getPredNodes(T N) { Collection preds = HashSetFactory.make(); for (U label : nodeToPredLabels.get(N)) { - preds.addAll(Iterator2Collection.toCollection(getPredNodes(N, label))); + preds.addAll(Iterator2Collection.toSet(getPredNodes(N, label))); } return preds.iterator(); } @@ -246,7 +246,7 @@ public class SparseNumberedLabeledEdgeManager implements NumberedLabeledEd public Iterator getSuccNodes(T N) { Collection succs = HashSetFactory.make(); for (U label : nodeToSuccLabels.get(N)) { - succs.addAll(Iterator2Collection.toCollection(getSuccNodes(N, label))); + succs.addAll(Iterator2Collection.toSet(getSuccNodes(N, label))); } return succs.iterator(); } diff --git a/com.ibm.wala.core/src/com/ibm/wala/util/graph/traverse/DFS.java b/com.ibm.wala.core/src/com/ibm/wala/util/graph/traverse/DFS.java index dbf34a5ab..f6d2f06c3 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/util/graph/traverse/DFS.java +++ b/com.ibm.wala.core/src/com/ibm/wala/util/graph/traverse/DFS.java @@ -52,7 +52,7 @@ public class DFS { return new FilterIterator(G.getSuccNodes(n), filter); } }; - return Iterator2Collection.toCollection(dfs); + return Iterator2Collection.toSet(dfs); } /** diff --git a/com.ibm.wala.core/src/com/ibm/wala/util/intset/OrdinalSet.java b/com.ibm.wala.core/src/com/ibm/wala/util/intset/OrdinalSet.java index dcd68af9c..f6344b329 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/util/intset/OrdinalSet.java +++ b/com.ibm.wala.core/src/com/ibm/wala/util/intset/OrdinalSet.java @@ -133,7 +133,7 @@ public class OrdinalSet implements Iterable { @Override public String toString() { - return Iterator2Collection.toCollection(iterator()).toString(); + return Iterator2Collection.toSet(iterator()).toString(); } /** @@ -169,7 +169,7 @@ public class OrdinalSet implements Iterable { * @throws NullPointerException if instances is null */ public static Collection toCollection(OrdinalSet instances) { - return Iterator2Collection.toCollection(instances.iterator()); + return Iterator2Collection.toSet(instances.iterator()); } /** diff --git a/com.ibm.wala.core/src/com/ibm/wala/viz/DotUtil.java b/com.ibm.wala.core/src/com/ibm/wala/viz/DotUtil.java index 01a9df33a..f9f0ed7b9 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/viz/DotUtil.java +++ b/com.ibm.wala.core/src/com/ibm/wala/viz/DotUtil.java @@ -241,7 +241,7 @@ public class DotUtil { * Compute the nodes to visualize */ private static Collection computeDotNodes(Graph g) throws WalaException { - return Iterator2Collection.toCollection(g.iterator()); + return Iterator2Collection.toSet(g.iterator()); } private static String getRankDir() throws WalaException { diff --git a/com.ibm.wala.j2ee/src/com/ibm/wala/j2ee/J2EEContainerModel.java b/com.ibm.wala.j2ee/src/com/ibm/wala/j2ee/J2EEContainerModel.java index e6423ad9f..6c1736244 100644 --- a/com.ibm.wala.j2ee/src/com/ibm/wala/j2ee/J2EEContainerModel.java +++ b/com.ibm.wala.j2ee/src/com/ibm/wala/j2ee/J2EEContainerModel.java @@ -214,7 +214,7 @@ class J2EEContainerModel extends SyntheticClass implements BytecodeConstants, EJ } } }; - staticFieldRefs = Iterator2Collection.toCollection(it); + staticFieldRefs = Iterator2Collection.toSet(it); initializeStaticFieldMap(); }