1 salcianu 1.1 // AbstrCallGraph.java, created Wed Apr 10 23:37:29 2002 by salcianu
 2 salcianu 1.1 // Copyright (C) 2000 Alexandru SALCIANU <salcianu@MIT.EDU>
 3 salcianu 1.1 // Licensed under the terms of the GNU GPL; see COPYING for details.
 4 salcianu 1.1 package harpoon.Analysis.Quads;
 5 salcianu 1.1 
 6 salcianu 1.4 import java.util.Set;
 7 salcianu 1.6 import java.util.HashSet;
 8 salcianu 1.1 import java.util.Map;
 9 salcianu 1.1 import java.util.HashMap;
10 salcianu 1.1 import java.util.List;
11 salcianu 1.6 import java.util.Iterator;
12 salcianu 1.1 
13 salcianu 1.6 import harpoon.ClassFile.HClass;
14 salcianu 1.1 import harpoon.ClassFile.HMethod;
15 salcianu 1.1 import harpoon.ClassFile.HCodeFactory;
16 salcianu 1.1 import harpoon.IR.Quads.Code;
17 salcianu 1.1 import harpoon.IR.Quads.Quad;
18 salcianu 1.1 import harpoon.IR.Quads.CALL;
19 salcianu 1.1 
20 salcianu 1.1 
21 salcianu 1.1 /**
22 salcianu 1.1  * <code>AbstrCallGraph</code> contains some common code for several
23 salcianu 1.1  * implementations of <code>CallGraph</code>.
24 salcianu 1.1  * 
25 salcianu 1.1  * @author  Alexandru SALCIANU <salcianu@MIT.EDU>
26 cananian 1.7  * @version $Id: AbstrCallGraph.java,v 1.7 2004/02/08 04:53:33 cananian Exp $
27 salcianu 1.1  */
28 salcianu 1.5 abstract class AbstrCallGraph extends CallGraph {
29 salcianu 1.1 
30 salcianu 1.1     // the code factory that produces the code of the methods
31 salcianu 1.1     protected HCodeFactory hcf;
32 salcianu 1.1     
33 salcianu 1.1     /** Returns a list of all <code>CALL</code>s quads in the code 
34 salcianu 1.1         of <code>hm</code>. */
35 salcianu 1.1     public CALL[] getCallSites(final HMethod hm) {
36 cananian 1.3         CALL[] retval = cache_cs.get(hm);
37 salcianu 1.1         if(retval == null) {
38 salcianu 1.2             Code code = (Code) hcf.convert(hm);
39 salcianu 1.2             if(code == null) {
40 salcianu 1.2                 retval = new CALL[0];
41 salcianu 1.2             }
42 salcianu 1.2             else {
43 salcianu 1.2                 List<Quad> l = code.selectCALLs();
44 salcianu 1.2                 retval = l.toArray(new CALL[l.size()]);
45 salcianu 1.2             }
46 salcianu 1.1             cache_cs.put(hm, retval);
47 salcianu 1.1         }
48 salcianu 1.1         return retval;
49 salcianu 1.1     }
50 cananian 1.3     final private Map<HMethod,CALL[]> cache_cs = new HashMap<HMethod,CALL[]>();
51 salcianu 1.2     final private static CALL[] empty_array = new CALL[0];
52 salcianu 1.4 
53 salcianu 1.6     /** Safe implementation of <code>getRunMethods</code>: returns the
54 salcianu 1.6         set of callable methods named &quot;run&quot;, with no
55 salcianu 1.6         argument and declared by a subclass of
56 salcianu 1.6         <code>java.lang.Thread</code>. */
57 salcianu 1.6     public Set<HMethod> getRunMethods() {
58 salcianu 1.6         Set<HMethod> runs = new HashSet<HMethod>();
59 cananian 1.7         for(HMethod hm : callableMethods()) {
60 salcianu 1.6             if((hm.getParameterNames().length == 0) &&
61 salcianu 1.6                hm.getName().equals("run") && 
62 salcianu 1.6                isThread(hm.getDeclaringClass()))
63 salcianu 1.6                 runs.add(hm);
64 salcianu 1.6         }
65 salcianu 1.6         return runs;
66 salcianu 1.6     }
67 salcianu 1.6 
68 salcianu 1.6     private static boolean isThread(HClass hclass) {
69 salcianu 1.6         HClass jlt = hclass.getLinker().forName("java.lang.Thread");
70 salcianu 1.6         return hclass.isInstanceOf(jlt);
71 salcianu 1.4     }
72 salcianu 1.1 }