View Javadoc
1 /*** 2 * ClassLoader 3 * 4 * This class instruments code according 5 * to the Thomas Ball 1994 Branch Profiling 6 * algorithm. 7 */ 8 9 package junit.quilt.cover.ball94; 10 11 import java.net.URL; 12 13 import java.util.Set; 14 import java.util.Map; 15 import java.util.List; 16 import java.util.HashSet; 17 import java.util.HashMap; 18 import java.util.Iterator; 19 import java.util.ArrayList; 20 21 import junit.quilt.framework.*; 22 import junit.quilt.cover.generic.*; 23 import junit.quilt.exception.*; 24 25 import cern.colt.matrix.*; 26 import cern.colt.matrix.impl.*; 27 28 import org.apache.bcel.*; 29 import org.apache.bcel.generic.*; 30 import org.apache.bcel.classfile.*; 31 32 import org.apache.commons.graph.*; 33 import org.apache.commons.graph.algorithm.spanning.*; 34 35 public class B94ClassLoader 36 extends MethInstClassLoader 37 { 38 private B94Registry registry; 39 40 public B94ClassLoader( String packages[], 41 URL path[], 42 ClassLoader parent, 43 B94Registry registry) { 44 super( packages, path, parent ); 45 this.registry = registry; 46 } 47 48 public void instrumentMethod( InstContext context, 49 ControlFlowGraph graph ) 50 throws QuiltException { 51 52 FlowControlEdge dummy = 53 getEdgeFactory().makeDummyEdge( graph.getEndVertex(), 54 graph.getStartVertex() ); 55 graph.addEdge( dummy ); 56 57 58 MinimumSpanningForest msf = 59 new MinimumSpanningForest( graph ); 60 61 List chords = new ArrayList( msf.getChords() ); 62 List vList = new ArrayList( graph.getVertices() ); 63 List eList = new ArrayList( graph.getEdges() ); 64 65 int stats[] = new int[chords.size()]; 66 67 DoubleMatrix2D matrix = 68 makeMatrix( graph, chords, vList, eList ); 69 70 registry.bind( context.getClassGen().getClassName(), 71 graph.getMethodName(), 72 new B94Collector( vList, eList, matrix, stats )); 73 74 int fieldRef = addStaticField( context, 75 new ArrayType( Type.INT, 1), 76 stats ); 77 78 Iterator c = chords.iterator(); 79 short pos = 0; 80 while (c.hasNext()) { 81 FlowControlEdge chord = (FlowControlEdge) c.next(); 82 instrumentEdge(context, 83 graph, 84 chord, 85 new ArrayIncrement(fieldRef, pos)); 86 pos++; 87 } 88 89 } 90 91 /*** 92 * makeMatrix 93 * 94 * Create a matrix which will get run through 95 * the LUDecomposition to make it easy to find 96 * the number of times an edge/vertex was visited. 97 * <pre> 98 * V = | Vertices | 99 * E = | Edges | 100 * I = | Instrumented Edge Set | 101 * 102 * For Vertices v0, v1, v2, ... vV 103 * And Edges e0, e1, e2, ... eE 104 * And Instrumented Edges i0, i1, i2, ... iI 105 * (where iX = eX, X<I) 106 * And Instrumented Edge Values l0, l1, l2, ... lI 107 * 108 * e0 e1 e2 ... eI ... eE Val 109 * i0 1 l0 110 * i1 1 l1 111 * . . . 112 * . . . 113 * . . . 114 * iI 1 lI 115 * v0 1 -1 0 116 * v1 -1 1 0 117 * v2 -1 1 1 0 118 * . 119 * . 120 * . 121 * vV 1 -1 122 * 123 * For each cell: 124 * cell(iH, eD) = 1 (H < I, D < E, iH = eD) 125 * cell(iN, Val) = lN (N < I) // Filled in when Queried. 126 * 127 * cell(vU, eD) = 1 (U < V, 128 * D < E, 129 * if source(eD) == vU) 130 * cell(vU, eD) = -1 (U < V, 131 * D < E, 132 * if target(eD) == vU) 133 * </pre> 134 */ 135 DoubleMatrix2D makeMatrix( DirectedGraph graph, 136 List chords, 137 List vertices, 138 List edges ) { 139 DoubleMatrix2D RC = 140 new DenseDoubleMatrix2D(vertices.size() + 141 chords.size(), 142 edges.size()); 143 144 int colPos = 0; 145 int rowPos = 0; 146 147 Iterator iChords = chords.iterator(); 148 while (iChords.hasNext()) { 149 Edge chord = (Edge) iChords.next(); 150 colPos = edges.lastIndexOf( chord ); 151 152 RC.set( rowPos, colPos, 1.0 ); 153 rowPos ++; 154 } 155 156 Iterator iVerts = vertices.iterator(); 157 while (iVerts.hasNext()) { 158 Vertex v = (Vertex) iVerts.next(); 159 160 Iterator iInbound = graph.getInbound(v).iterator(); 161 while (iInbound.hasNext()) { 162 colPos = edges.lastIndexOf( iInbound.next() ); 163 RC.set( rowPos, colPos, 1.0 ); 164 } 165 166 Iterator iOutbound = graph.getOutbound(v).iterator(); 167 while (iOutbound.hasNext()) { 168 colPos = edges.lastIndexOf( iOutbound.next() ); 169 RC.set( rowPos, colPos, -1.0 ); 170 } 171 rowPos++; 172 } 173 174 return RC; 175 } 176 } 177 178 179 180 181 182

This page was automatically generated by Maven