1 package Analysis.SSJava;
3 import java.io.BufferedWriter;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.Comparator;
9 import java.util.HashMap;
10 import java.util.HashSet;
11 import java.util.Hashtable;
12 import java.util.Iterator;
13 import java.util.LinkedList;
14 import java.util.List;
17 import java.util.StringTokenizer;
18 import java.util.Vector;
20 import Analysis.CallGraph.CallGraph;
21 import Analysis.Loops.GlobalFieldType;
22 import Analysis.Loops.LoopOptimize;
23 import Analysis.Loops.LoopTerminate;
24 import IR.AnnotationDescriptor;
25 import IR.ClassDescriptor;
27 import IR.FieldDescriptor;
28 import IR.MethodDescriptor;
30 import IR.SymbolTable;
32 import IR.Flat.BuildFlat;
33 import IR.Flat.FlatMethod;
34 import IR.Flat.FlatNode;
37 public class SSJavaAnalysis {
39 public static final String SSJAVA = "SSJAVA";
40 public static final String LATTICE = "LATTICE";
41 public static final String METHODDEFAULT = "METHODDEFAULT";
42 public static final String THISLOC = "THISLOC";
43 public static final String GLOBALLOC = "GLOBALLOC";
44 public static final String RETURNLOC = "RETURNLOC";
45 public static final String PCLOC = "PCLOC";
46 public static final String LOC = "LOC";
47 public static final String DELTA = "DELTA";
48 public static final String TERMINATE = "TERMINATE";
49 public static final String DELEGATE = "DELEGATE";
50 public static final String DELEGATETHIS = "DELEGATETHIS";
51 public static final String TRUST = "TRUST";
53 public static final String TOP = "_top_";
54 public static final String BOTTOM = "_bottom_";
58 FlowDownCheck flowDownChecker;
59 MethodAnnotationCheck methodAnnotationChecker;
62 // set containing method requires to be annoated
63 Set<MethodDescriptor> annotationRequireSet;
65 // class -> field lattice
66 Hashtable<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
68 // class -> default local variable lattice
69 Hashtable<ClassDescriptor, MethodLattice<String>> cd2methodDefault;
71 // method -> local variable lattice
72 Hashtable<MethodDescriptor, MethodLattice<String>> md2lattice;
74 // method set that does not want to have loop termination analysis
75 Hashtable<MethodDescriptor, Integer> skipLoopTerminate;
77 // map shared location to its descriptors
78 Hashtable<Location, Set<Descriptor>> mapSharedLocation2DescriptorSet;
80 // set containing a class that has at least one annoated method
81 Set<ClassDescriptor> annotationRequireClassSet;
83 // the set of method descriptor required to check the linear type property
84 Set<MethodDescriptor> linearTypeCheckMethodSet;
86 // the set of method descriptors annotated as "TRUST"
87 Set<MethodDescriptor> trustWorthyMDSet;
89 // method -> the initial program counter location
90 Map<MethodDescriptor, CompositeLocation> md2pcLoc;
92 // points to method containing SSJAVA Loop
93 private MethodDescriptor methodContainingSSJavaLoop;
95 private FlatNode ssjavaLoopEntrance;
97 // keep the field ownership from the linear type checking
98 Hashtable<MethodDescriptor, Set<FieldDescriptor>> mapMethodToOwnedFieldSet;
100 Set<FlatNode> sameHeightWriteFlatNodeSet;
104 LinearTypeCheck checker;
106 // maps a descriptor to its known dependents: namely
107 // methods or tasks that call the descriptor's method
108 // AND are part of this analysis (reachable from main)
109 private Hashtable<Descriptor, Set<MethodDescriptor>> mapDescriptorToSetDependents;
111 private LinkedList<MethodDescriptor> sortedDescriptors;
113 private Map<Location, Set<Descriptor>> mapSharedLocToDescSet;
115 public SSJavaAnalysis(State state, TypeUtil tu, BuildFlat bf, CallGraph callgraph) {
118 this.callgraph = callgraph;
119 this.cd2lattice = new Hashtable<ClassDescriptor, SSJavaLattice<String>>();
120 this.cd2methodDefault = new Hashtable<ClassDescriptor, MethodLattice<String>>();
121 this.md2lattice = new Hashtable<MethodDescriptor, MethodLattice<String>>();
122 this.annotationRequireSet = new HashSet<MethodDescriptor>();
123 this.annotationRequireClassSet = new HashSet<ClassDescriptor>();
124 this.skipLoopTerminate = new Hashtable<MethodDescriptor, Integer>();
125 this.mapSharedLocation2DescriptorSet = new Hashtable<Location, Set<Descriptor>>();
126 this.linearTypeCheckMethodSet = new HashSet<MethodDescriptor>();
128 this.trustWorthyMDSet = new HashSet<MethodDescriptor>();
129 this.mapMethodToOwnedFieldSet = new Hashtable<MethodDescriptor, Set<FieldDescriptor>>();
130 this.sameHeightWriteFlatNodeSet = new HashSet<FlatNode>();
131 this.mapDescriptorToSetDependents = new Hashtable<Descriptor, Set<MethodDescriptor>>();
132 this.sortedDescriptors = new LinkedList<MethodDescriptor>();
133 this.md2pcLoc = new HashMap<MethodDescriptor, CompositeLocation>();
134 this.mapSharedLocToDescSet = new HashMap<Location, Set<Descriptor>>();
137 public void doCheck() {
138 doMethodAnnotationCheck();
140 if (state.SSJAVA && !state.SSJAVAINFER) {
142 computeLinearTypeCheckMethodSet();
146 if (state.SSJAVADEBUG) {
147 // debug_printAnnotationRequiredSet();
149 if (state.SSJAVAINFER) {
153 parseLocationAnnotation();
155 doDefinitelyWrittenCheck();
159 for (Iterator iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
160 MethodDescriptor md = (MethodDescriptor) iterator.next();
161 MethodLattice<String> locOrder = getMethodLattice(md);
162 writeLatticeDotFile(md.getClassDesc(), md, getMethodLattice(md));
163 // System.out.println("~~~\t" + md.getClassDesc() + "_" + md + "\t"
164 // + locOrder.getKeySet().size());
169 // perform topological sort over the set of methods accessed by the main
171 Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
172 methodDescriptorsToAnalyze.addAll(getAnnotationRequireSet());
173 sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
176 public LinkedList<MethodDescriptor> getSortedDescriptors() {
177 return (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
180 public void addSharedDesc(Location loc, Descriptor fd) {
181 if (!mapSharedLocToDescSet.containsKey(loc)) {
182 mapSharedLocToDescSet.put(loc, new HashSet<Descriptor>());
184 mapSharedLocToDescSet.get(loc).add(fd);
187 public Set<Descriptor> getSharedDescSet(Location loc) {
188 return mapSharedLocToDescSet.get(loc);
191 private void inference() {
192 LocationInference inferEngine = new LocationInference(this, state, tu);
193 inferEngine.inference();
196 private void doLoopCheck() {
197 GlobalFieldType gft = new GlobalFieldType(callgraph, state, tu.getMain());
198 LoopOptimize lo = new LoopOptimize(gft, tu);
200 SymbolTable classtable = state.getClassSymbolTable();
202 List<ClassDescriptor> toanalyzeList = new ArrayList<ClassDescriptor>();
203 List<MethodDescriptor> toanalyzeMethodList = new ArrayList<MethodDescriptor>();
205 toanalyzeList.addAll(classtable.getValueSet());
206 Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
207 public int compare(ClassDescriptor o1, ClassDescriptor o2) {
208 return o1.getClassName().compareTo(o2.getClassName());
212 for (int i = 0; i < toanalyzeList.size(); i++) {
213 ClassDescriptor cd = toanalyzeList.get(i);
215 SymbolTable methodtable = cd.getMethodTable();
216 toanalyzeMethodList.clear();
217 toanalyzeMethodList.addAll(methodtable.getValueSet());
218 Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
219 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
220 return o1.getSymbol().compareTo(o2.getSymbol());
224 for (int mdIdx = 0; mdIdx < toanalyzeMethodList.size(); mdIdx++) {
225 MethodDescriptor md = toanalyzeMethodList.get(mdIdx);
226 if (needTobeAnnotated(md)) {
227 lo.analyze(state.getMethodFlat(md));
228 doLoopTerminationCheck(lo, state.getMethodFlat(md));
236 public void addTrustMethod(MethodDescriptor md) {
237 trustWorthyMDSet.add(md);
240 public boolean isTrustMethod(MethodDescriptor md) {
241 return trustWorthyMDSet.contains(md);
244 private void computeLinearTypeCheckMethodSet() {
246 Set<MethodDescriptor> allCalledSet = callgraph.getMethodCalls(tu.getMain());
247 linearTypeCheckMethodSet.addAll(allCalledSet);
249 Set<MethodDescriptor> trustedSet = new HashSet<MethodDescriptor>();
251 for (Iterator iterator = trustWorthyMDSet.iterator(); iterator.hasNext();) {
252 MethodDescriptor trustMethod = (MethodDescriptor) iterator.next();
253 Set<MethodDescriptor> calledFromTrustMethodSet = callgraph.getMethodCalls(trustMethod);
254 trustedSet.add(trustMethod);
255 trustedSet.addAll(calledFromTrustMethodSet);
258 linearTypeCheckMethodSet.removeAll(trustedSet);
260 // if a method is called only by trusted method, no need to check linear
261 // type & flow down rule
262 for (Iterator iterator = trustedSet.iterator(); iterator.hasNext();) {
263 MethodDescriptor md = (MethodDescriptor) iterator.next();
264 Set<MethodDescriptor> callerSet = callgraph.getCallerSet(md);
265 if (!trustedSet.containsAll(callerSet) && !trustWorthyMDSet.contains(md)) {
266 linearTypeCheckMethodSet.add(md);
270 linearTypeCheckMethodSet.addAll(sortedDescriptors);
274 private void doLinearTypeCheck() {
275 LinearTypeCheck checker = new LinearTypeCheck(this, state);
276 checker.linearTypeCheck();
279 public void debug_printAnnotationRequiredSet() {
280 System.out.println("SSJAVA: SSJava is checking the following methods:");
281 for (Iterator<MethodDescriptor> iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
282 MethodDescriptor md = iterator.next();
283 System.out.println(md);
285 System.out.println();
288 private void doMethodAnnotationCheck() {
289 methodAnnotationChecker = new MethodAnnotationCheck(this, state, tu);
290 methodAnnotationChecker.methodAnnoatationCheck();
291 methodAnnotationChecker.methodAnnoataionInheritanceCheck();
292 if (state.SSJAVAINFER) {
293 annotationRequireClassSet.add(methodContainingSSJavaLoop.getClassDesc());
294 annotationRequireSet.add(methodContainingSSJavaLoop);
296 state.setAnnotationRequireSet(annotationRequireSet);
299 public void doFlowDownCheck() {
300 flowDownChecker = new FlowDownCheck(this, state);
301 flowDownChecker.flowDownCheck();
304 public void doDefinitelyWrittenCheck() {
305 DefinitelyWrittenCheck checker = new DefinitelyWrittenCheck(this, state);
306 checker.definitelyWrittenCheck();
309 private void parseLocationAnnotation() {
310 Iterator it = state.getClassSymbolTable().getDescriptorsIterator();
311 while (it.hasNext()) {
312 ClassDescriptor cd = (ClassDescriptor) it.next();
313 // parsing location hierarchy declaration for the class
314 Vector<AnnotationDescriptor> classAnnotations = cd.getModifier().getAnnotations();
315 for (int i = 0; i < classAnnotations.size(); i++) {
316 AnnotationDescriptor an = classAnnotations.elementAt(i);
317 String marker = an.getMarker();
318 if (marker.equals(LATTICE)) {
319 SSJavaLattice<String> locOrder =
320 new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
321 cd2lattice.put(cd, locOrder);
322 parseClassLatticeDefinition(cd, an.getValue(), locOrder);
324 if (state.SSJAVADEBUG) {
325 // generate lattice dot file
326 writeLatticeDotFile(cd, null, locOrder);
327 System.out.println("~~~\t" + cd + "\t" + locOrder.getKeySet().size());
330 } else if (marker.equals(METHODDEFAULT)) {
331 MethodLattice<String> locOrder =
332 new MethodLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
333 cd2methodDefault.put(cd, locOrder);
334 parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
335 // writeLatticeDotFile(cd, null, locOrder, "METHOD_DEFAULT");
339 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
340 MethodDescriptor md = (MethodDescriptor) method_it.next();
341 // parsing location hierarchy declaration for the method
343 if (needTobeAnnotated(md)) {
344 Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
345 if (methodAnnotations != null) {
346 for (int i = 0; i < methodAnnotations.size(); i++) {
347 AnnotationDescriptor an = methodAnnotations.elementAt(i);
348 if (an.getMarker().equals(LATTICE)) {
349 // developer explicitly defines method lattice
350 MethodLattice<String> locOrder =
351 new MethodLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
352 md2lattice.put(md, locOrder);
353 parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
354 writeLatticeDotFile(cd, md, locOrder, "");
355 } else if (an.getMarker().equals(TERMINATE)) {
356 // developer explicitly wants to skip loop termination analysis
357 String value = an.getValue();
358 int maxIteration = 0;
360 maxIteration = Integer.parseInt(value);
362 skipLoopTerminate.put(md, new Integer(maxIteration));
373 public <T> void writeLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
374 SSJavaLattice<T> locOrder) {
375 writeLatticeDotFile(cd, md, locOrder, "");
379 public <T> void writeLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
380 SSJavaLattice<T> locOrder, String nameSuffix) {
382 String fileName = "lattice_";
385 cd.getSymbol().replaceAll("[\\W_]", "") + "_" + md.toString().replaceAll("[\\W_]", "");
387 fileName += cd.getSymbol().replaceAll("[\\W_]", "");
390 fileName += nameSuffix;
392 Set<Pair<T, T>> pairSet = locOrder.getOrderingPairSet();
394 if (pairSet.size() > 0) {
396 BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".dot"));
398 bw.write("digraph " + fileName + " {\n");
400 for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
401 // pair is in the form of <higher, lower>
402 Pair<T, T> pair = (Pair<T, T>) iterator.next();
404 T highLocId = pair.getFirst();
405 String highLocStr, lowLocStr;
406 if (locOrder.isSharedLoc(highLocId)) {
407 highLocStr = "\"" + highLocId + "*\"";
409 highLocStr = highLocId.toString();
411 T lowLocId = pair.getSecond();
412 if (locOrder.isSharedLoc(lowLocId)) {
413 lowLocStr = "\"" + lowLocId + "*\"";
415 lowLocStr = lowLocId.toString();
417 bw.write(highLocStr + " -> " + lowLocStr + ";\n");
422 } catch (IOException e) {
430 private void parseMethodDefaultLatticeDefinition(ClassDescriptor cd, String value,
431 MethodLattice<String> locOrder) {
433 value = value.replaceAll(" ", ""); // remove all blank spaces
435 StringTokenizer tokenizer = new StringTokenizer(value, ",");
437 while (tokenizer.hasMoreTokens()) {
438 String orderElement = tokenizer.nextToken();
439 int idx = orderElement.indexOf("<");
440 if (idx > 0) {// relative order element
441 String lowerLoc = orderElement.substring(0, idx);
442 String higherLoc = orderElement.substring(idx + 1);
443 locOrder.put(higherLoc, lowerLoc);
444 if (locOrder.isIntroducingCycle(higherLoc)) {
445 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
446 + " introduces a cycle.");
448 } else if (orderElement.startsWith(THISLOC + "=")) {
449 String thisLoc = orderElement.substring(8);
450 locOrder.setThisLoc(thisLoc);
451 } else if (orderElement.startsWith(GLOBALLOC + "=")) {
452 String globalLoc = orderElement.substring(10);
453 locOrder.setGlobalLoc(globalLoc);
454 } else if (orderElement.startsWith(RETURNLOC + "=")) {
455 String returnLoc = orderElement.substring(10);
456 locOrder.setReturnLoc(returnLoc);
457 } else if (orderElement.endsWith("*")) {
458 // spin loc definition
459 locOrder.addSharedLoc(orderElement.substring(0, orderElement.length() - 1));
462 locOrder.put(orderElement);
467 if (locOrder.getThisLoc() != null && !locOrder.containsKey(locOrder.getThisLoc())) {
468 throw new Error("Variable 'this' location '" + locOrder.getThisLoc()
469 + "' is not defined in the local variable lattice at " + cd.getSourceFileName());
472 if (locOrder.getGlobalLoc() != null && !locOrder.containsKey(locOrder.getGlobalLoc())) {
473 throw new Error("Variable global location '" + locOrder.getGlobalLoc()
474 + "' is not defined in the local variable lattice at " + cd.getSourceFileName());
478 private void parseClassLatticeDefinition(ClassDescriptor cd, String value,
479 SSJavaLattice<String> locOrder) {
481 value = value.replaceAll(" ", ""); // remove all blank spaces
483 StringTokenizer tokenizer = new StringTokenizer(value, ",");
485 while (tokenizer.hasMoreTokens()) {
486 String orderElement = tokenizer.nextToken();
487 int idx = orderElement.indexOf("<");
489 if (idx > 0) {// relative order element
490 String lowerLoc = orderElement.substring(0, idx);
491 String higherLoc = orderElement.substring(idx + 1);
492 locOrder.put(higherLoc, lowerLoc);
493 if (locOrder.isIntroducingCycle(higherLoc)) {
494 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
495 + " introduces a cycle in the class lattice " + cd);
497 } else if (orderElement.contains("*")) {
498 // spin loc definition
499 locOrder.addSharedLoc(orderElement.substring(0, orderElement.length() - 1));
502 locOrder.put(orderElement);
507 Set<String> spinLocSet = locOrder.getSharedLocSet();
508 for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
509 String spinLoc = (String) iterator.next();
510 if (!locOrder.containsKey(spinLoc)) {
511 throw new Error("Spin location '" + spinLoc
512 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
517 public Hashtable<ClassDescriptor, SSJavaLattice<String>> getCd2lattice() {
521 public Hashtable<ClassDescriptor, MethodLattice<String>> getCd2methodDefault() {
522 return cd2methodDefault;
525 public Hashtable<MethodDescriptor, MethodLattice<String>> getMd2lattice() {
529 public SSJavaLattice<String> getClassLattice(ClassDescriptor cd) {
530 return cd2lattice.get(cd);
533 public MethodLattice<String> getMethodDefaultLattice(ClassDescriptor cd) {
534 return cd2methodDefault.get(cd);
537 public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
538 if (md2lattice.containsKey(md)) {
539 return md2lattice.get(md);
542 if (cd2methodDefault.containsKey(md.getClassDesc())) {
543 return cd2methodDefault.get(md.getClassDesc());
545 throw new Error("Method Lattice of " + md + " is not defined.");
551 public CompositeLocation getPCLocation(MethodDescriptor md) {
552 if (!md2pcLoc.containsKey(md)) {
553 // by default, the initial pc location is TOP
554 CompositeLocation pcLoc = new CompositeLocation(new Location(md, Location.TOP));
555 md2pcLoc.put(md, pcLoc);
557 return md2pcLoc.get(md);
560 public void setPCLocation(MethodDescriptor md, CompositeLocation pcLoc) {
561 md2pcLoc.put(md, pcLoc);
564 public boolean needToCheckLinearType(MethodDescriptor md) {
565 return linearTypeCheckMethodSet.contains(md);
568 public boolean needTobeAnnotated(MethodDescriptor md) {
569 return annotationRequireSet.contains(md);
572 public boolean needToBeAnnoated(ClassDescriptor cd) {
573 return annotationRequireClassSet.contains(cd);
576 public void addAnnotationRequire(ClassDescriptor cd) {
577 annotationRequireClassSet.add(cd);
580 public void addAnnotationRequire(MethodDescriptor md) {
582 ClassDescriptor cd = md.getClassDesc();
583 // if a method requires to be annotated, class containg that method also
584 // requires to be annotated
585 if (!isSSJavaUtil(cd)) {
586 annotationRequireClassSet.add(cd);
587 annotationRequireSet.add(md);
591 public Set<MethodDescriptor> getAnnotationRequireSet() {
592 return annotationRequireSet;
595 public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
596 LoopTerminate lt = new LoopTerminate(this, state);
597 if (needTobeAnnotated(fm.getMethod())) {
598 lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
602 public CallGraph getCallGraph() {
606 public SSJavaLattice<String> getLattice(Descriptor d) {
608 if (d instanceof MethodDescriptor) {
609 return getMethodLattice((MethodDescriptor) d);
611 return getClassLattice((ClassDescriptor) d);
616 public boolean isSharedLocation(Location loc) {
617 SSJavaLattice<String> lattice = getLattice(loc.getDescriptor());
618 return lattice.getSharedLocSet().contains(loc.getLocIdentifier());
621 public void mapSharedLocation2Descriptor(Location loc, Descriptor d) {
622 Set<Descriptor> set = mapSharedLocation2DescriptorSet.get(loc);
624 set = new HashSet<Descriptor>();
625 mapSharedLocation2DescriptorSet.put(loc, set);
630 public BuildFlat getBuildFlat() {
634 public MethodDescriptor getMethodContainingSSJavaLoop() {
635 return methodContainingSSJavaLoop;
638 public void setMethodContainingSSJavaLoop(MethodDescriptor methodContainingSSJavaLoop) {
639 this.methodContainingSSJavaLoop = methodContainingSSJavaLoop;
642 public boolean isSSJavaUtil(ClassDescriptor cd) {
643 if (cd.getSymbol().equals("SSJAVA")) {
649 public void setFieldOnwership(MethodDescriptor md, FieldDescriptor field) {
651 Set<FieldDescriptor> fieldSet = mapMethodToOwnedFieldSet.get(md);
652 if (fieldSet == null) {
653 fieldSet = new HashSet<FieldDescriptor>();
654 mapMethodToOwnedFieldSet.put(md, fieldSet);
659 public boolean isOwnedByMethod(MethodDescriptor md, FieldDescriptor field) {
660 Set<FieldDescriptor> fieldSet = mapMethodToOwnedFieldSet.get(md);
661 if (fieldSet != null) {
662 return fieldSet.contains(field);
667 public FlatNode getSSJavaLoopEntrance() {
668 return ssjavaLoopEntrance;
671 public void setSSJavaLoopEntrance(FlatNode ssjavaLoopEntrance) {
672 this.ssjavaLoopEntrance = ssjavaLoopEntrance;
675 public void addSameHeightWriteFlatNode(FlatNode fn) {
676 this.sameHeightWriteFlatNodeSet.add(fn);
679 public boolean isSameHeightWrite(FlatNode fn) {
680 return this.sameHeightWriteFlatNodeSet.contains(fn);
683 public LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
685 Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
687 LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
689 Iterator<MethodDescriptor> itr = toSort.iterator();
690 while (itr.hasNext()) {
691 MethodDescriptor d = itr.next();
693 if (!discovered.contains(d)) {
694 dfsVisit(d, toSort, sorted, discovered);
701 // While we're doing DFS on call graph, remember
702 // dependencies for efficient queuing of methods
703 // during interprocedural analysis:
705 // a dependent of a method decriptor d for this analysis is:
706 // 1) a method or task that invokes d
707 // 2) in the descriptorsToAnalyze set
708 private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
709 LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
713 Iterator itr2 = callgraph.getCalleeSet(md).iterator();
714 while (itr2.hasNext()) {
715 MethodDescriptor dCallee = (MethodDescriptor) itr2.next();
716 addDependent(dCallee, md);
719 Iterator itr = callgraph.getCallerSet(md).iterator();
720 while (itr.hasNext()) {
721 MethodDescriptor dCaller = (MethodDescriptor) itr.next();
722 // only consider callers in the original set to analyze
723 if (!toSort.contains(dCaller)) {
726 if (!discovered.contains(dCaller)) {
727 addDependent(md, // callee
731 dfsVisit(dCaller, toSort, sorted, discovered);
735 // for leaf-nodes last now!
739 public void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
740 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
742 deps = new HashSet<MethodDescriptor>();
745 mapDescriptorToSetDependents.put(callee, deps);
748 public Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
749 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
751 deps = new HashSet<MethodDescriptor>();
752 mapDescriptorToSetDependents.put(callee, deps);