01: package org.hansel.probes;
02:
03: import org.hansel.ProbeData;
04: import org.hansel.stack.HanselValue;
05: import org.hansel.stack.BinaryOperatorEntry;
06: import org.hansel.stack.NotOp;
07:
08: import org.objectweb.asm.MethodVisitor;
09: 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(), cmp
38: .getPrecedence(), op, value);
39: } else {
40: if (cmp instanceof EQComparator) {
41: return new NotOp(op);
42: } else if (cmp instanceof NEComparator) {
43: return op;
44: } else {
45: throw new IllegalStateException();
46: }
47: }
48: }
49:
50: public void hit(int value) {
51: cover(cmp.compare(value, 0));
52: }
53:
54: public void insertProbeCode(MethodVisitor cv) {
55: cv.visitInsn(Opcodes.DUP);
56: cv.visitLdcInsn(new Integer(getID()));
57: cv.visitMethodInsn(Opcodes.INVOKESTATIC, HIT_CLASS,
58: "hitBranch", "(II)V");
59: }
60: }
|