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) {
141 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);
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);
272 private void doLinearTypeCheck() {
273 LinearTypeCheck checker = new LinearTypeCheck(this, state);
274 checker.linearTypeCheck();
277 public void debug_printAnnotationRequiredSet() {
278 System.out.println("SSJAVA: SSJava is checking the following methods:");
279 for (Iterator<MethodDescriptor> iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
280 MethodDescriptor md = iterator.next();
281 System.out.println(md);
283 System.out.println();
286 private void doMethodAnnotationCheck() {
287 methodAnnotationChecker = new MethodAnnotationCheck(this, state, tu);
288 methodAnnotationChecker.methodAnnoatationCheck();
289 methodAnnotationChecker.methodAnnoataionInheritanceCheck();
290 if (state.SSJAVAINFER) {
291 annotationRequireClassSet.add(methodContainingSSJavaLoop.getClassDesc());
292 annotationRequireSet.add(methodContainingSSJavaLoop);
294 state.setAnnotationRequireSet(annotationRequireSet);
297 public void doFlowDownCheck() {
298 flowDownChecker = new FlowDownCheck(this, state);
299 flowDownChecker.flowDownCheck();
302 public void doDefinitelyWrittenCheck() {
303 DefinitelyWrittenCheck checker = new DefinitelyWrittenCheck(this, state);
304 checker.definitelyWrittenCheck();
307 private void parseLocationAnnotation() {
308 Iterator it = state.getClassSymbolTable().getDescriptorsIterator();
309 while (it.hasNext()) {
310 ClassDescriptor cd = (ClassDescriptor) it.next();
311 // parsing location hierarchy declaration for the class
312 Vector<AnnotationDescriptor> classAnnotations = cd.getModifier().getAnnotations();
313 for (int i = 0; i < classAnnotations.size(); i++) {
314 AnnotationDescriptor an = classAnnotations.elementAt(i);
315 String marker = an.getMarker();
316 if (marker.equals(LATTICE)) {
317 SSJavaLattice<String> locOrder =
318 new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
319 cd2lattice.put(cd, locOrder);
320 parseClassLatticeDefinition(cd, an.getValue(), locOrder);
322 if (state.SSJAVADEBUG) {
323 // generate lattice dot file
324 writeLatticeDotFile(cd, null, locOrder);
325 System.out.println("~~~\t" + cd + "\t" + locOrder.getKeySet().size());
328 } else if (marker.equals(METHODDEFAULT)) {
329 MethodLattice<String> locOrder =
330 new MethodLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
331 cd2methodDefault.put(cd, locOrder);
332 parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
333 // writeLatticeDotFile(cd, null, locOrder, "METHOD_DEFAULT");
337 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
338 MethodDescriptor md = (MethodDescriptor) method_it.next();
339 // parsing location hierarchy declaration for the method
341 if (needTobeAnnotated(md)) {
342 Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
343 if (methodAnnotations != null) {
344 for (int i = 0; i < methodAnnotations.size(); i++) {
345 AnnotationDescriptor an = methodAnnotations.elementAt(i);
346 if (an.getMarker().equals(LATTICE)) {
347 // developer explicitly defines method lattice
348 MethodLattice<String> locOrder =
349 new MethodLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
350 md2lattice.put(md, locOrder);
351 parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
352 writeLatticeDotFile(cd, md, locOrder, "");
353 } else if (an.getMarker().equals(TERMINATE)) {
354 // developer explicitly wants to skip loop termination analysis
355 String value = an.getValue();
356 int maxIteration = 0;
358 maxIteration = Integer.parseInt(value);
360 skipLoopTerminate.put(md, new Integer(maxIteration));
371 public <T> void writeLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
372 SSJavaLattice<T> locOrder) {
373 writeLatticeDotFile(cd, md, locOrder, "");
377 public <T> void writeLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
378 SSJavaLattice<T> locOrder, String nameSuffix) {
380 String fileName = "lattice_";
383 cd.getSymbol().replaceAll("[\\W_]", "") + "_" + md.toString().replaceAll("[\\W_]", "");
385 fileName += cd.getSymbol().replaceAll("[\\W_]", "");
388 fileName += nameSuffix;
390 Set<Pair<T, T>> pairSet = locOrder.getOrderingPairSet();
392 if (pairSet.size() > 0) {
394 BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".dot"));
396 bw.write("digraph " + fileName + " {\n");
398 for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
399 // pair is in the form of <higher, lower>
400 Pair<T, T> pair = (Pair<T, T>) iterator.next();
402 T highLocId = pair.getFirst();
403 String highLocStr, lowLocStr;
404 if (locOrder.isSharedLoc(highLocId)) {
405 highLocStr = "\"" + highLocId + "*\"";
407 highLocStr = highLocId.toString();
409 T lowLocId = pair.getSecond();
410 if (locOrder.isSharedLoc(lowLocId)) {
411 lowLocStr = "\"" + lowLocId + "*\"";
413 lowLocStr = lowLocId.toString();
415 bw.write(highLocStr + " -> " + lowLocStr + ";\n");
420 } catch (IOException e) {
428 private void parseMethodDefaultLatticeDefinition(ClassDescriptor cd, String value,
429 MethodLattice<String> locOrder) {
431 value = value.replaceAll(" ", ""); // remove all blank spaces
433 StringTokenizer tokenizer = new StringTokenizer(value, ",");
435 while (tokenizer.hasMoreTokens()) {
436 String orderElement = tokenizer.nextToken();
437 int idx = orderElement.indexOf("<");
438 if (idx > 0) {// relative order element
439 String lowerLoc = orderElement.substring(0, idx);
440 String higherLoc = orderElement.substring(idx + 1);
441 locOrder.put(higherLoc, lowerLoc);
442 if (locOrder.isIntroducingCycle(higherLoc)) {
443 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
444 + " introduces a cycle.");
446 } else if (orderElement.startsWith(THISLOC + "=")) {
447 String thisLoc = orderElement.substring(8);
448 locOrder.setThisLoc(thisLoc);
449 } else if (orderElement.startsWith(GLOBALLOC + "=")) {
450 String globalLoc = orderElement.substring(10);
451 locOrder.setGlobalLoc(globalLoc);
452 } else if (orderElement.startsWith(RETURNLOC + "=")) {
453 String returnLoc = orderElement.substring(10);
454 locOrder.setReturnLoc(returnLoc);
455 } else if (orderElement.endsWith("*")) {
456 // spin loc definition
457 locOrder.addSharedLoc(orderElement.substring(0, orderElement.length() - 1));
460 locOrder.put(orderElement);
465 if (locOrder.getThisLoc() != null && !locOrder.containsKey(locOrder.getThisLoc())) {
466 throw new Error("Variable 'this' location '" + locOrder.getThisLoc()
467 + "' is not defined in the local variable lattice at " + cd.getSourceFileName());
470 if (locOrder.getGlobalLoc() != null && !locOrder.containsKey(locOrder.getGlobalLoc())) {
471 throw new Error("Variable global location '" + locOrder.getGlobalLoc()
472 + "' is not defined in the local variable lattice at " + cd.getSourceFileName());
476 private void parseClassLatticeDefinition(ClassDescriptor cd, String value,
477 SSJavaLattice<String> locOrder) {
479 value = value.replaceAll(" ", ""); // remove all blank spaces
481 StringTokenizer tokenizer = new StringTokenizer(value, ",");
483 while (tokenizer.hasMoreTokens()) {
484 String orderElement = tokenizer.nextToken();
485 int idx = orderElement.indexOf("<");
487 if (idx > 0) {// relative order element
488 String lowerLoc = orderElement.substring(0, idx);
489 String higherLoc = orderElement.substring(idx + 1);
490 locOrder.put(higherLoc, lowerLoc);
491 if (locOrder.isIntroducingCycle(higherLoc)) {
492 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
493 + " introduces a cycle in the class lattice " + cd);
495 } else if (orderElement.contains("*")) {
496 // spin loc definition
497 locOrder.addSharedLoc(orderElement.substring(0, orderElement.length() - 1));
500 locOrder.put(orderElement);
505 Set<String> spinLocSet = locOrder.getSharedLocSet();
506 for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
507 String spinLoc = (String) iterator.next();
508 if (!locOrder.containsKey(spinLoc)) {
509 throw new Error("Spin location '" + spinLoc
510 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
515 public Hashtable<ClassDescriptor, SSJavaLattice<String>> getCd2lattice() {
519 public Hashtable<ClassDescriptor, MethodLattice<String>> getCd2methodDefault() {
520 return cd2methodDefault;
523 public Hashtable<MethodDescriptor, MethodLattice<String>> getMd2lattice() {
527 public SSJavaLattice<String> getClassLattice(ClassDescriptor cd) {
528 return cd2lattice.get(cd);
531 public MethodLattice<String> getMethodDefaultLattice(ClassDescriptor cd) {
532 return cd2methodDefault.get(cd);
535 public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
536 if (md2lattice.containsKey(md)) {
537 return md2lattice.get(md);
540 if (cd2methodDefault.containsKey(md.getClassDesc())) {
541 return cd2methodDefault.get(md.getClassDesc());
543 throw new Error("Method Lattice of " + md + " is not defined.");
549 public CompositeLocation getPCLocation(MethodDescriptor md) {
550 if (!md2pcLoc.containsKey(md)) {
551 // by default, the initial pc location is TOP
552 CompositeLocation pcLoc = new CompositeLocation(new Location(md, Location.TOP));
553 md2pcLoc.put(md, pcLoc);
555 return md2pcLoc.get(md);
558 public void setPCLocation(MethodDescriptor md, CompositeLocation pcLoc) {
559 md2pcLoc.put(md, pcLoc);
562 public boolean needToCheckLinearType(MethodDescriptor md) {
563 return linearTypeCheckMethodSet.contains(md);
566 public boolean needTobeAnnotated(MethodDescriptor md) {
567 return annotationRequireSet.contains(md);
570 public boolean needToBeAnnoated(ClassDescriptor cd) {
571 return annotationRequireClassSet.contains(cd);
574 public void addAnnotationRequire(ClassDescriptor cd) {
575 annotationRequireClassSet.add(cd);
578 public void addAnnotationRequire(MethodDescriptor md) {
580 ClassDescriptor cd = md.getClassDesc();
581 // if a method requires to be annotated, class containg that method also
582 // requires to be annotated
583 if (!isSSJavaUtil(cd)) {
584 annotationRequireClassSet.add(cd);
585 annotationRequireSet.add(md);
589 public Set<MethodDescriptor> getAnnotationRequireSet() {
590 return annotationRequireSet;
593 public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
594 LoopTerminate lt = new LoopTerminate(this, state);
595 if (needTobeAnnotated(fm.getMethod())) {
596 lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
600 public CallGraph getCallGraph() {
604 public SSJavaLattice<String> getLattice(Descriptor d) {
606 if (d instanceof MethodDescriptor) {
607 return getMethodLattice((MethodDescriptor) d);
609 return getClassLattice((ClassDescriptor) d);
614 public boolean isSharedLocation(Location loc) {
615 SSJavaLattice<String> lattice = getLattice(loc.getDescriptor());
616 return lattice.getSharedLocSet().contains(loc.getLocIdentifier());
619 public void mapSharedLocation2Descriptor(Location loc, Descriptor d) {
620 Set<Descriptor> set = mapSharedLocation2DescriptorSet.get(loc);
622 set = new HashSet<Descriptor>();
623 mapSharedLocation2DescriptorSet.put(loc, set);
628 public BuildFlat getBuildFlat() {
632 public MethodDescriptor getMethodContainingSSJavaLoop() {
633 return methodContainingSSJavaLoop;
636 public void setMethodContainingSSJavaLoop(MethodDescriptor methodContainingSSJavaLoop) {
637 this.methodContainingSSJavaLoop = methodContainingSSJavaLoop;
640 public boolean isSSJavaUtil(ClassDescriptor cd) {
641 if (cd.getSymbol().equals("SSJAVA")) {
647 public void setFieldOnwership(MethodDescriptor md, FieldDescriptor field) {
649 Set<FieldDescriptor> fieldSet = mapMethodToOwnedFieldSet.get(md);
650 if (fieldSet == null) {
651 fieldSet = new HashSet<FieldDescriptor>();
652 mapMethodToOwnedFieldSet.put(md, fieldSet);
657 public boolean isOwnedByMethod(MethodDescriptor md, FieldDescriptor field) {
658 Set<FieldDescriptor> fieldSet = mapMethodToOwnedFieldSet.get(md);
659 if (fieldSet != null) {
660 return fieldSet.contains(field);
665 public FlatNode getSSJavaLoopEntrance() {
666 return ssjavaLoopEntrance;
669 public void setSSJavaLoopEntrance(FlatNode ssjavaLoopEntrance) {
670 this.ssjavaLoopEntrance = ssjavaLoopEntrance;
673 public void addSameHeightWriteFlatNode(FlatNode fn) {
674 this.sameHeightWriteFlatNodeSet.add(fn);
677 public boolean isSameHeightWrite(FlatNode fn) {
678 return this.sameHeightWriteFlatNodeSet.contains(fn);
681 public LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
683 Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
685 LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
687 Iterator<MethodDescriptor> itr = toSort.iterator();
688 while (itr.hasNext()) {
689 MethodDescriptor d = itr.next();
691 if (!discovered.contains(d)) {
692 dfsVisit(d, toSort, sorted, discovered);
699 // While we're doing DFS on call graph, remember
700 // dependencies for efficient queuing of methods
701 // during interprocedural analysis:
703 // a dependent of a method decriptor d for this analysis is:
704 // 1) a method or task that invokes d
705 // 2) in the descriptorsToAnalyze set
706 private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
707 LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
711 Iterator itr2 = callgraph.getCalleeSet(md).iterator();
712 while (itr2.hasNext()) {
713 MethodDescriptor dCallee = (MethodDescriptor) itr2.next();
714 addDependent(dCallee, md);
717 Iterator itr = callgraph.getCallerSet(md).iterator();
718 while (itr.hasNext()) {
719 MethodDescriptor dCaller = (MethodDescriptor) itr.next();
720 // only consider callers in the original set to analyze
721 if (!toSort.contains(dCaller)) {
724 if (!discovered.contains(dCaller)) {
725 addDependent(md, // callee
729 dfsVisit(dCaller, toSort, sorted, discovered);
733 // for leaf-nodes last now!
737 public void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
738 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
740 deps = new HashSet<MethodDescriptor>();
743 mapDescriptorToSetDependents.put(callee, deps);
746 public Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
747 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
749 deps = new HashSet<MethodDescriptor>();
750 mapDescriptorToSetDependents.put(callee, deps);