1 2 package org.quilt.cover.stmt; 3 4 import java.lang.reflect.Field ; 5 import java.util.Iterator ; 6 import java.util.Hashtable ; 7 import java.util.Map ; 8 import java.util.Set ; 9 import org.quilt.cl.*; 10 import org.quilt.reg.*; 11 12 38 public class StmtRegistry extends QuiltRegistry { 39 40 41 private static StmtRegistry INSTANCE = null; 42 43 44 public static StmtRegistry getInstance () { 45 return INSTANCE; 46 } 47 48 49 private Map methodNames = new Hashtable (); 51 private Map methodEnds = new Hashtable (); 53 57 public StmtRegistry (QuiltClassLoader qcl) { 58 super(qcl); 59 INSTANCE = this; ClassAction classAct = new ClassAction(this); 61 62 cxf = new ClassXformer[] { classAct }; 63 mxf = new MethodXformer[] { new MethodAction(this) }; 64 gxf = new GraphXformer[] { new GraphAction(this, classAct) }; 65 setTransformers(); 66 } 67 68 71 public void reset() { 72 } 74 75 79 public String getReport() { 80 StringBuffer sb = new StringBuffer () 81 .append("\n=========================\n") 82 .append( " QUILT COVERAGE REPORT \n"); 83 if (isEmpty()) { 84 sb.append("* the registry is empty *\n"); 85 } else { 86 Set keys = keySet(); 87 Iterator i = keys.iterator(); 88 while (i.hasNext()) { 89 String [] name = (String []) i.next(); 91 int [] counts = (int[])get(name); 92 int count = counts.length; 93 String className = name[0]; 94 95 sb.append(className + ": " + count + " counters, " 96 + getClassCoverage(className) + "% coverage\n "); 97 104 String [] methods = (String [])methodNames.get(className); 105 if (methods == null) { 106 sb.append(" NULL\n"); 107 } else { 108 for (int k = 0; k < methods.length; k++) { 110 sb.append(" ").append(methods[k]) 111 .append(" ").append(getMethodCoverage(className,k)) 112 .append("% coverage\n"); 113 } 114 } 115 } 116 } 117 sb.append("=========================\n"); 118 return sb.toString(); 119 } 120 121 128 int getClassCoverage (String className) { 129 int nonZero = 0; 130 int[] hitCounts = (int[]) get(new String [] {className}); 131 if (hitCounts != null) { 132 for (int k = 0; k < hitCounts.length; k++) { 133 if (hitCounts[k] > 0) { 134 nonZero++; 135 } 136 } 137 nonZero = (nonZero*100)/hitCounts.length; 138 } 139 return nonZero; 140 } 141 142 152 int getMethodCoverage (String className, int n) { 153 int nonZero = 0; 154 int[] hitCounts = (int[]) get(new String [] {className}); 157 int[] ends = (int[]) methodEnds.get(className); 158 if ( n < 0 || n >= hitCounts.length ) { 159 throw new IllegalArgumentException ("index out of range"); 160 } 161 int counterCount = 0; 162 int lastCounter = ends[n] -1 ; 163 int firstCounter = n == 0 ? 0 : ends[n - 1]; 164 if (hitCounts != null && ends != null) { 165 for (int k = firstCounter; k <= lastCounter; k++) { 166 counterCount++; 167 if (hitCounts[k] > 0) { 168 nonZero++; 169 } 170 } 171 if (counterCount > 0) { 172 nonZero = (nonZero*100)/counterCount; 173 } 174 } 175 return nonZero; 176 } 177 178 private static int nextClassID = 0; 181 182 public int getClassID (String className) { 183 int classID = -1; 184 try { 185 Field qField = Class.forName(className).getField("q$$qID"); 186 classID = qField.getInt(qField); 188 } catch (Exception e) { 189 System.out.println("StmtRegistry.getClassID(" 192 + className + ") failed - " + e); 193 } 195 return classID; 196 } 197 201 public int [] getCounts (String className) { 202 int[] counts = null; 203 try { 204 counts = (int[])get ( new String [] {className} ); 205 } catch (Exception e) { 206 System.out.println("StmtRegistry.getCounts (" 208 + className + ") failed - " + e); 209 } 210 return counts; 211 } 212 224 225 public int registerCounts (String className, int [] counts) { 226 int classID = -1; 227 try { 228 put ( new String [] {className} , counts ); 229 classID = nextClassID++; 230 } catch (Exception e) { 231 System.out.println("StmtRegistry.registerCounts for " 233 + className + ", q$$q) failed - " + e); 234 } 235 return classID; 236 } 237 238 public void registerMethods (String className, String [] methods, 239 int [] endCounts) { 240 if (className == null || methods == null || endCounts == null) { 241 throw new IllegalArgumentException ("null parameter"); 242 } 243 methodNames.put ( className, methods ); 244 methodEnds.put ( className, endCounts ); 245 } 246 public int getQuiltVersion (String className) { 248 int quiltVersion = -1; 249 try { 250 Field qField = Class.forName(className).getField("q$$qVer"); 251 quiltVersion = qField.getInt(qField); 253 } catch (Exception e) { 254 System.out.println("StmtRegistry.getClassID(" 257 + className + ") failed - " + e); 258 } 260 return quiltVersion; 261 } 262 264 private Map ephemera = new Hashtable (); 266 267 Ephemera getEphemera (String className) { 268 if (ephemera.containsKey(className)) { 269 return (Ephemera) ephemera.get(className); 270 } else { 271 return null; 272 } 273 } 274 275 boolean putEphemera (String className, Ephemera eph) { 276 if (ephemera.containsKey(className)) { 277 return false; } 279 ephemera.put(className, eph); 281 return true; 282 } 283 290 Ephemera removeEphemera (String className) { 291 return (Ephemera) ephemera.remove(className); 292 } 293 295 public int registerClass(String name, org.quilt.cover.stmt.QIC junk) { 296 System.out.println( 297 "**************************" 298 + "\nQCL.registerClass " + name 299 + "\n**************************" ); 300 return 52; } 302 303 } 304 | Popular Tags |