1 package csdl.jblanket.modifier; 2 3 import csdl.jblanket.JBlanketException; 4 import csdl.jblanket.methodset.MethodInfo; 5 import csdl.jblanket.methodset.MethodSet; 6 import csdl.jblanket.methodset.MethodSetManager; 7 8 import java.io.File ; 9 import java.io.FileInputStream ; 10 import java.io.FileNotFoundException ; 11 import java.io.FileOutputStream ; 12 import java.io.IOException ; 13 import java.text.DateFormat ; 14 import java.text.SimpleDateFormat ; 15 import java.util.ArrayList ; 16 import java.util.Date ; 17 import java.util.Properties ; 18 19 import org.apache.tools.ant.DirectoryScanner; 20 21 44 public class MethodCollector { 45 46 47 private static String currentClassName; 48 49 50 private static final String COVER_PREFIX = "COVER-"; 51 52 private static final String XML_EXTENSION = ".xml"; 53 54 55 private Date earliestDate; 56 57 58 private static final String SLASH = File.separator; 59 60 61 private MethodCollector() { } 62 63 78 public static synchronized void storeMethodTypeSignature(String className, String methodName, 79 ArrayList paramList, String testGrammar) 80 throws JBlanketException { 81 82 final String constructor = "<init>"; 84 85 if ((currentClassName == null) || isTestClass(className, testGrammar)) { 87 currentClassName = className; 88 } 89 90 MethodSet methodSet = MethodSetManager.getInstance().getMethodSet(currentClassName); 92 ArrayList reconstructedList = new ArrayList (); 93 94 for (int i = 0; i < paramList.size(); i++) { 96 reconstructedList.add(reconstructType((String ) paramList.get(i))); 97 } 98 99 if (constructor.equals(methodName)) { 101 methodName = removePackagePrefix(className); 102 } 103 if ("<clinit>".equals(methodName)) { 104 methodName = MethodCollector.removePackagePrefix(className) + "[static initializer]"; 105 } 106 methodName = methodName.replaceAll("<", "_").replaceAll(">", "_"); 107 108 MethodInfo methodInfo = new MethodInfo(className, methodName, reconstructedList); 110 111 if (methodSet.add(methodInfo)) { 113 String jblanketDir = getJBlanketDir(); 114 String outFileName = jblanketDir + SLASH + COVER_PREFIX + currentClassName + XML_EXTENSION; 115 116 storeMethodData(methodSet, outFileName, currentClassName, new Date ()); 118 } 119 } 120 121 130 public static boolean isTestClass(String className, String testGrammar) { 131 132 if (testGrammar.endsWith(".java") || testGrammar.endsWith(".class") 134 || testGrammar.lastIndexOf('.') == testGrammar.length() - 1) { 135 testGrammar = testGrammar.substring(0, testGrammar.lastIndexOf('.')); 136 } 137 138 if (className.indexOf('.') > -1) { 139 className = className.substring(className.lastIndexOf('.') + 1, className.length()); 141 } 142 143 if (testGrammar.endsWith("*")) { 145 String test = testGrammar.substring(0, testGrammar.length() - 1); 146 return className.startsWith(test); 147 } 148 else if (testGrammar.startsWith("*")) { 149 String test = testGrammar.substring(1, testGrammar.length()); 150 return className.endsWith(test); 151 } 152 153 return false; 154 } 155 156 164 public static String getJBlanketDir() { 165 166 String jblanketDir = System.getProperty("jblanket.dir"); 168 169 if (jblanketDir == null || jblanketDir.equals("")) { 171 jblanketDir = getJBlanketDirInTomcatProperties(); 172 } 173 174 if (jblanketDir == null || jblanketDir.equals("")) { 176 jblanketDir = System.getProperty("user.home") + SLASH + "jblanket"; 177 String message = "Warning: jblanket.dir system property missing. Using: " + jblanketDir; 179 System.out.println(message); 180 System.setProperty("jblanket.dir", jblanketDir); 182 } 183 184 File jblanketDirectory = new File (jblanketDir); 186 if (!jblanketDirectory.exists()) { 187 jblanketDirectory.mkdirs(); 188 } 189 return jblanketDir; 190 } 191 192 199 private static String getJBlanketDirInTomcatProperties() { 200 201 File catalinaHome = new File (System.getProperty("catalina.home", "")); 203 if (!catalinaHome.exists()) { 204 return null; 205 } 206 207 File confDir = new File (catalinaHome, "conf"); 209 DirectoryScanner scanner = new DirectoryScanner(); 210 scanner.setBasedir(confDir); 211 scanner.setIncludes(new String [] {"*.properties"}); 212 scanner.scan(); 213 String [] files = scanner.getIncludedFiles(); 214 215 for (int i = 0; i < files.length; i++) { 217 Properties properties = new Properties (); 218 try { 219 properties.load(new FileInputStream (new File (confDir, files[i]))); 220 } 221 catch (Exception e) { 222 } 225 226 String tomcatJBlanketDir = properties.getProperty("jblanket.dir", null); 228 if (tomcatJBlanketDir != null) { 229 return tomcatJBlanketDir; 230 } 231 } 232 233 return null; 235 } 236 237 249 public static String reconstructType(String type) { 250 251 int arrayDimensions = 0; 253 254 while (type.indexOf("[") > -1 && type.length() > 1) { 255 arrayDimensions++; 256 type = type.substring(1, type.length()); 257 } 258 259 StringBuffer reconstructedType = null; 260 261 if (type.length() == 1) { 263 reconstructedType = new StringBuffer (reconstructBaseType(type)); 264 } 265 else { 267 int classNameStart = type.indexOf('L') + 1; 271 int classNameEnd = type.lastIndexOf(';'); 272 type = type.substring(classNameStart, classNameEnd); 273 reconstructedType = new StringBuffer (type.replace('/', '.')); 274 } 275 276 while (arrayDimensions > 0) { 277 reconstructedType.append("[]"); 278 arrayDimensions--; 279 } 280 return reconstructedType.toString(); 281 } 282 283 294 private static String reconstructBaseType(String type) { 295 296 char t = type.charAt(0); 297 switch (t) { 298 case 'B': 299 return "byte"; 300 case 'C': 301 return "char"; 302 case 'D': 303 return "double"; 304 case 'F': 305 return "float"; 306 case 'I': 307 return "int"; 308 case 'J': 309 return "long"; 310 case 'S': 311 return "short"; 312 case 'Z': 313 return "boolean"; 314 default: 315 return "Error! type is an invalid string"; 318 } 319 } 320 321 332 public static synchronized void storeMethodData(MethodSet methodSet, String outFileName, 333 String className, Date timeStamp) 334 throws JBlanketException { 335 336 MethodSet fileMethodSet = new MethodSet(); 338 try { 339 FileInputStream istream = new FileInputStream (new File (outFileName)); 340 fileMethodSet.load(istream); 342 istream.close(); 343 } 344 catch (Exception e) { 345 } 347 348 FileOutputStream fostream = null; 349 try { 350 fostream = new FileOutputStream (outFileName); 351 } 352 catch (FileNotFoundException e) { 353 throw new JBlanketException("Unable to open file " + outFileName, e); 354 } 355 356 fileMethodSet.union(methodSet); 357 try { 358 fileMethodSet.store(fostream, className, timeStamp); 359 } 360 catch (IOException e) { 361 throw new JBlanketException("Unable to store MethodSet to " + outFileName, e); 362 } 363 364 try { 365 fostream.close(); 366 } 367 catch (IOException e) { 368 throw new JBlanketException("Unable to close file " + outFileName, e); 369 } 370 } 371 372 378 public static String removePackagePrefix(String className) { 379 return className.substring(className.lastIndexOf('.') + 1, className.length()); 380 } 381 382 387 public static DateFormat getDateFormat() { 388 return new SimpleDateFormat ("MMM d, yyyy h:mm:ss a"); 389 } 390 } 391 | Popular Tags |