KickJava   Java API By Example, From Geeks To Geeks.

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


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
10 /**
11  * A probe for a comparing conditional branch. For decision coverage both
12  * possible conditions for the branch have to be encountered.
13  *
14  * @author Niklas Mehner.
15  */

16 public class ACmpBranchProbe extends BranchProbe {
17     /** Wether the compared references should be equal. */
18     private boolean equal;
19
20     public ACmpBranchProbe(ProbeData pd,
21                            boolean equal) {
22         super(pd, getStackEntry(pd.getStackEntry(0), pd.getStackEntry(1), equal));
23  
24         this.equal = equal;
25     }
26
27     private static HanselValue getStackEntry(HanselValue stackEntry0,
28                                              HanselValue stackEntry1,
29                                              boolean equal) {
30                                                 
31         if (equal) {
32             return new EqOp(stackEntry1, stackEntry0);
33         } else {
34             return new NeOp(stackEntry1, stackEntry0);
35         }
36     }
37
38     /**
39      * Called, when a probe is hit.
40      * @param obj1 First object to be compared.
41      * @param obj2 Second object to be compared.
42      */

43     public void hit(Object JavaDoc obj1, Object JavaDoc obj2) {
44         cover((obj1 == obj2) == equal);
45     }
46
47     /**
48      * Returns the code of the probe.
49      * @param cp ConstantPool all names of methods etc. have to be
50      * inserted.
51      * @return List of instructions to call this probe.
52      */

53     public void insertProbeCode(MethodVisitor cv) {
54         cv.visitInsn(Opcodes.DUP2);
55         cv.visitLdcInsn(new Integer JavaDoc(getID()));
56         cv.visitMethodInsn(Opcodes.INVOKESTATIC, HIT_CLASS, "hitBranch",
57         "(Ljava/lang/Object;Ljava/lang/Object;I)V");
58      }
59  }
60
Popular Tags