KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hansel > ProbeFactory


1 package org.hansel;
2
3 import java.lang.reflect.Constructor JavaDoc;
4
5 import org.hansel.probes.ACmpBranchProbe;
6 import org.hansel.probes.BinaryBranchProbe;
7 import org.hansel.probes.BranchProbe;
8 import org.hansel.probes.EQComparator;
9 import org.hansel.probes.ExceptionProbe;
10 import org.hansel.probes.GEComparator;
11 import org.hansel.probes.GTComparator;
12 import org.hansel.probes.IntComparator;
13 import org.hansel.probes.LEComparator;
14 import org.hansel.probes.LTComparator;
15 import org.hansel.probes.MethodProbe;
16 import org.hansel.probes.NEComparator;
17 import org.hansel.probes.NullCmpBranchProbe;
18 import org.hansel.probes.SelectProbe;
19 import org.hansel.probes.UnaryBranchProbe;
20 import org.objectweb.asm.Label;
21 import org.objectweb.asm.Opcodes;
22
23 public class ProbeFactory {
24     private final static Class JavaDoc[] BRANCH_CLASS_ARGS = { ProbeData.class,
25                                                         IntComparator.class };
26     
27     private final static BranchClassEntry[] BRANCH_CLASSES =
28         {
29             new BranchClassEntry(Opcodes.IFEQ, UnaryBranchProbe.class,
30                                  new EQComparator()),
31             new BranchClassEntry(Opcodes.IFNE, UnaryBranchProbe.class,
32                                  new NEComparator()),
33             new BranchClassEntry(Opcodes.IFGE, UnaryBranchProbe.class,
34                                  new GEComparator()),
35             new BranchClassEntry(Opcodes.IFGT, UnaryBranchProbe.class,
36                                  new GTComparator()),
37             new BranchClassEntry(Opcodes.IFLE, UnaryBranchProbe.class,
38                                  new LEComparator()),
39             new BranchClassEntry(Opcodes.IFLT, UnaryBranchProbe.class,
40                                  new LTComparator()),
41
42             new BranchClassEntry(Opcodes.IF_ICMPEQ, BinaryBranchProbe.class,
43                                  new EQComparator()),
44             new BranchClassEntry(Opcodes.IF_ICMPNE, BinaryBranchProbe.class,
45                                  new NEComparator()),
46             new BranchClassEntry(Opcodes.IF_ICMPGE, BinaryBranchProbe.class,
47                                  new GEComparator()),
48             new BranchClassEntry(Opcodes.IF_ICMPGT, BinaryBranchProbe.class,
49                                  new GTComparator()),
50             new BranchClassEntry(Opcodes.IF_ICMPLE, BinaryBranchProbe.class,
51                                  new LEComparator()),
52             new BranchClassEntry(Opcodes.IF_ICMPLT, BinaryBranchProbe.class,
53                                  new LTComparator()),
54         };
55
56     public static MethodProbe createMethodProbe(ProbeData pd) {
57         MethodProbe cached = (MethodProbe) ProbeTable.getProbeTable().getCached(pd);
58         if (cached != null) {
59             return cached;
60         }
61
62         return new MethodProbe(pd);
63     }
64
65     public static ExceptionProbe createExceptionProbe(ProbeData pd,
66                                                       Probe mp) {
67         ExceptionProbe cached =
68                 (ExceptionProbe) ProbeTable.getProbeTable().getCached(pd);
69         if (cached != null) {
70             return cached;
71         }
72
73         return new ExceptionProbe(mp, pd);
74     }
75
76     public static SelectProbe createSelectProbe(ProbeData pd,
77                                                 Label dflt,
78                                                 int[] keys,
79                                                 Label[] labels) {
80         SelectProbe cached =
81                 (SelectProbe) ProbeTable.getProbeTable().getCached(pd);
82         if (cached != null) {
83             return cached;
84         }
85
86         return new SelectProbe(pd, dflt, keys, labels);
87     }
88
89     public static SelectProbe createSelectProbe(ProbeData pd,
90                                                 int min,
91                                                 int max,
92                                                 Label dflt,
93                                                 Label labels[]) {
94         SelectProbe cached =
95                 (SelectProbe) ProbeTable.getProbeTable().getCached(pd);
96         if (cached != null) {
97             return cached;
98         }
99
100         return new SelectProbe(pd, min, max, dflt, labels);
101     }
102
103     public static BranchProbe createBranchProbe(ProbeData pd,
104                                                 int opcode) {
105
106         BranchProbe cached = (BranchProbe) ProbeTable.getProbeTable().getCached(pd);
107         if (cached != null) {
108             return cached;
109         }
110
111         for (int i=0; i<BRANCH_CLASSES.length; i++) {
112             if (opcode == BRANCH_CLASSES[i].getBranchOpcode()) {
113                 return BRANCH_CLASSES[i].createProbe(pd);
114             }
115         }
116
117         if (opcode == Opcodes.IF_ACMPEQ) {
118             return new ACmpBranchProbe(pd, true);
119         } else if (opcode == Opcodes.IF_ACMPNE) {
120             return new ACmpBranchProbe(pd, false);
121         } else if (opcode == Opcodes.IFNONNULL) {
122             return new NullCmpBranchProbe(pd, false);
123         } else if (opcode == Opcodes.IFNULL) {
124             return new NullCmpBranchProbe(pd, true);
125         }
126
127         throw new UnsupportedOperationException JavaDoc("Unknown branch opcode:"
128                                                 + opcode);
129     }
130
131     private static class BranchClassEntry {
132         private int opcode;
133         private Class JavaDoc probeClazz;
134         private IntComparator cmp;
135
136         public BranchClassEntry(int opcode,
137                                 Class JavaDoc probeClazz,
138                                 IntComparator cmp) {
139             this.opcode = opcode;
140             this.probeClazz = probeClazz;
141             this.cmp = cmp;
142         }
143
144         public int getBranchOpcode() {
145             return opcode;
146         }
147
148         public BranchProbe createProbe(ProbeData pd) {
149             try {
150                 Constructor JavaDoc constr = probeClazz.getConstructor(BRANCH_CLASS_ARGS);
151                 return (BranchProbe) constr.newInstance(new Object JavaDoc[] {pd, cmp});
152             } catch (Exception JavaDoc e) {
153                 // Should not happen ...
154
e.printStackTrace();
155                 return null;
156             }
157         }
158     }
159 }
160
Popular Tags