KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 public class NullCmpBranchProbe extends BranchProbe {
18     private static final Type NULL_TYPE = Type.getType("LNULL;");
19     /** Wether the compared references should be equal. */
20     private boolean equal;
21
22     public NullCmpBranchProbe(ProbeData pd,
23                               boolean equal) {
24         super(pd, getStackEntry(pd.getStackEntry(0), equal));
25
26         this.equal = equal;
27     }
28
29     private static HanselValue getStackEntry(HanselValue stackEntry,
30                                              boolean equal) {
31         if (equal) {
32             return new EqOp(stackEntry, HanselValue.NULL);
33         } else {
34             return new NeOp(stackEntry, HanselValue.NULL);
35         }
36     }
37
38     /**
39      * Called, when a probe is hit.
40      * @param obj Object to be compared.
41      */

42     public void hit(Object JavaDoc obj) {
43         cover((obj != null) ^ equal);
44     }
45
46     public void insertProbeCode(MethodVisitor cv) {
47         cv.visitInsn(Opcodes.DUP);
48         cv.visitLdcInsn(new Integer JavaDoc(getID()));
49         cv.visitMethodInsn(Opcodes.INVOKESTATIC, HIT_CLASS, "hitBranch",
50         "(Ljava/lang/Object;I)V");
51      }
52  }
53
Popular Tags