KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hansel > probes > UnaryBranchProbe


1 package org.hansel.probes;
2
3 import org.hansel.ProbeData;
4 import org.hansel.stack.HanselValue;
5 import org.hansel.stack.BinaryOperatorEntry;
6 import org.hansel.stack.NotOp;
7
8 import org.objectweb.asm.MethodVisitor;
9 import org.objectweb.asm.Opcodes;
10 import org.objectweb.asm.tree.analysis.AnalyzerException;
11
12 /**
13  * A probe for a conditional branch. For decision coverage both possible
14  * conditions for the branch have to be encountered.
15  *
16  * @author Niklas Mehner.
17  */

18 public class UnaryBranchProbe extends BranchProbe {
19     private IntComparator cmp;
20     
21     public UnaryBranchProbe(ProbeData pd, IntComparator cmp)
22     throws AnalyzerException {
23         super(pd, getStackEntry(cmp, pd.getStackEntry(0)));
24         this.cmp = cmp;
25     }
26     
27     private static HanselValue getStackEntry(IntComparator cmp,
28                                              HanselValue op) {
29         if (!op.isBoolType()) {
30             HanselValue value;
31             if (op.getSize() == 1) {
32                 value = HanselValue.ZERO_1;
33             } else {
34                 value = HanselValue.ZERO_2;
35             }
36
37             return new BinaryOperatorEntry(cmp.getSign(),
38                                            cmp.getPrecedence(),
39                                            op,
40                                            value);
41         } else {
42             if (cmp instanceof EQComparator) {
43                 return new NotOp(op);
44             } else if (cmp instanceof NEComparator) {
45                 return op;
46             } else {
47                 throw new IllegalStateException JavaDoc();
48             }
49         }
50     }
51     
52     public void hit(int value) {
53         cover(cmp.compare(value, 0));
54     }
55     
56     public void insertProbeCode(MethodVisitor cv) {
57         cv.visitInsn(Opcodes.DUP);
58         cv.visitLdcInsn(new Integer JavaDoc(getID()));
59         cv.visitMethodInsn(Opcodes.INVOKESTATIC, HIT_CLASS, "hitBranch",
60         "(II)V");
61     }
62 }
63
Popular Tags