Change tabbing for everything....
[IRC.git] / Robust / src / IR / Virtual.java
1 package IR;
2 import java.util.*;
3 import Analysis.Locality.LocalityBinding;
4 import Analysis.Locality.LocalityAnalysis;
5
6 public class Virtual {
7   State state;
8   LocalityAnalysis locality;
9   Hashtable<MethodDescriptor, Integer> methodnumber;
10   Hashtable<ClassDescriptor, Integer> classmethodcount;
11   Hashtable<LocalityBinding, Integer> localitynumber;
12
13   public int getMethodNumber(MethodDescriptor md) {
14     return methodnumber.get(md).intValue();
15   }
16
17   public int getMethodCount(ClassDescriptor md) {
18     return classmethodcount.get(md).intValue();
19   }
20
21   public int getLocalityNumber(LocalityBinding lb) {
22     return localitynumber.get(lb).intValue();
23   }
24
25   public Virtual(State state, LocalityAnalysis locality) {
26     this.state=state;
27     this.locality=locality;
28     classmethodcount=new Hashtable<ClassDescriptor, Integer>();
29     if (state.DSM)
30       localitynumber=new Hashtable<LocalityBinding, Integer>();
31     else
32       methodnumber=new Hashtable<MethodDescriptor, Integer>();
33     doAnalysis();
34   }
35
36   private void doAnalysis() {
37     Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
38     while(classit.hasNext()) {
39       ClassDescriptor cd=(ClassDescriptor)classit.next();
40       if (state.DSM)
41         numberLocality(cd);
42       else
43         numberMethods(cd);
44     }
45   }
46
47   private int numberLocality(ClassDescriptor cd) {
48     if (classmethodcount.containsKey(cd))
49       return classmethodcount.get(cd).intValue();
50     ClassDescriptor superdesc=cd.getSuperDesc();
51     int start=0;
52     if (superdesc!=null)
53       start=numberLocality(superdesc);
54
55     if (locality.getClassBindings(cd)!=null)
56       for(Iterator<LocalityBinding> lbit=locality.getClassBindings(cd).iterator(); lbit.hasNext();) {
57         LocalityBinding lb=lbit.next();
58         MethodDescriptor md=lb.getMethod();
59         //Is it a static method or constructor
60         if (md.isStatic()||md.getReturnType()==null)
61           continue;
62
63         if (superdesc!=null) {
64           Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
65           boolean foundmatch=false;
66           for(Iterator matchit=possiblematches.iterator(); matchit.hasNext();) {
67             MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
68             if (md.matches(matchmd)) {
69               Set<LocalityBinding> lbset=locality.getMethodBindings(matchmd);
70               if (lbset!=null)
71                 for(Iterator<LocalityBinding> suplbit=lbset.iterator(); suplbit.hasNext();) {
72                   LocalityBinding suplb=suplbit.next();
73                   if (lb.contextMatches(suplb)) {
74                     foundmatch=true;
75                     localitynumber.put(lb, localitynumber.get(suplb));
76                     break;
77                   }
78                 }
79               break;
80             }
81           }
82           if (!foundmatch)
83             localitynumber.put(lb, new Integer(start++));
84         } else {
85           localitynumber.put(lb, new Integer(start++));
86         }
87       }
88     classmethodcount.put(cd, new Integer(start));
89     return start;
90   }
91
92   private int numberMethods(ClassDescriptor cd) {
93     if (classmethodcount.containsKey(cd))
94       return classmethodcount.get(cd).intValue();
95     ClassDescriptor superdesc=cd.getSuperDesc();
96     int start=0;
97     if (superdesc!=null)
98       start=numberMethods(superdesc);
99     for(Iterator it=cd.getMethods(); it.hasNext();) {
100       MethodDescriptor md=(MethodDescriptor)it.next();
101       if (md.isStatic()||md.getReturnType()==null)
102         continue;
103       if (superdesc!=null) {
104         Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
105         boolean foundmatch=false;
106         for(Iterator matchit=possiblematches.iterator(); matchit.hasNext();) {
107           MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
108           if (md.matches(matchmd)) {
109             int num=((Integer)methodnumber.get(matchmd)).intValue();
110             methodnumber.put(md, new Integer(num));
111             foundmatch=true;
112             break;
113           }
114         }
115         if (!foundmatch)
116           methodnumber.put(md, new Integer(start++));
117       } else {
118         methodnumber.put(md, new Integer(start++));
119       }
120     }
121     classmethodcount.put(cd, new Integer(start));
122     return start;
123   }
124 }
125