1 package csdl.jblanket.modifier; 2 3 import csdl.jblanket.JBlanket; 4 import csdl.jblanket.JBlanketException; 5 import csdl.jblanket.methodset.MethodSet; 6 import csdl.jblanket.methodset.MethodSetManager; 7 import csdl.jblanket.util.JarFactory; 8 import csdl.jblanket.util.MethodCategories; 9 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.io.FileNotFoundException ; 13 import java.io.FileOutputStream ; 14 import java.io.IOException ; 15 import java.text.ParseException ; 16 import java.util.ArrayList ; 17 import java.util.Date ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.StringTokenizer ; 21 22 import org.apache.tools.ant.DirectoryScanner; 23 24 118 public class Modifier extends JBlanket { 119 120 121 private String testGrammar = ""; 122 123 124 private MethodSet untestableSet; 125 126 private MethodSet excludedSet; 127 128 129 private List packagePrefixes; 130 131 132 private MethodCounter counter; 133 134 135 private Date date; 136 137 148 public Modifier(boolean verbose, String testGrammar, boolean excludeOneLineMethods, 149 boolean excludeConstructors, boolean excludeIndividualMethods, 150 List packagePrefixes) throws ParseException { 151 super(); 152 this.verbose = verbose; 153 if (isValidTestGrammar(testGrammar)) { 154 this.testGrammar = testGrammar; 155 } 156 157 MethodSetManager manager = MethodSetManager.getInstance(); 159 super.totalSet = manager.getMethodSet(super.categories.getFileName("totalFile")); 160 161 this.excludedSet = manager.getMethodSet(super.categories.getFileName("excludedFile")); 162 163 this.untestableSet = manager.getMethodSet(super.categories.getFileName("untestableFile")); 164 165 super.excludeOneLineMethods = excludeOneLineMethods; 167 if (super.excludeOneLineMethods) { 168 super.oneLineSet = manager.getMethodSet(super.categories.getFileName("oneLineFile")); 169 } 170 171 super.excludeConstructors = excludeConstructors; 173 if (super.excludeConstructors) { 174 super.constructorSet = manager.getMethodSet(super.categories.getFileName("constructorFile")); 175 } 176 177 super.excludeIndividualMethods = excludeIndividualMethods; 178 179 if (packagePrefixes.size() > 0) { 181 this.packagePrefixes = packagePrefixes; 182 } 183 else { 184 this.packagePrefixes = null; 185 } 186 187 this.counter = new MethodCounter(); 188 189 this.date = new Date (); 190 } 191 192 202 boolean isValidTestGrammar(String testGrammar) throws ParseException { 203 204 if (testGrammar.endsWith(".java") || testGrammar.endsWith(".class") 206 || testGrammar.lastIndexOf('.') == testGrammar.length() - 1) { 207 testGrammar = testGrammar.substring(0, testGrammar.lastIndexOf('.')); 208 } 209 210 if (testGrammar.indexOf('.') > -1 && 212 testGrammar.lastIndexOf('.') < testGrammar.length() - 1) { 213 throw new ParseException ("Ill-formed suffix of grammar <" + testGrammar + ">", 214 testGrammar.lastIndexOf('.')); 215 } 216 217 if (testGrammar.endsWith("*") || testGrammar.startsWith("*")) { 219 return true; 220 } 221 else { 222 throw new ParseException ("Ill-formed grammar for test class names", 0); 223 } 224 } 225 226 241 private void processIncludeClasses(String toDir, List includes) throws JBlanketException { 242 for (Iterator i = includes.iterator(); i.hasNext(); ) { 244 245 String className = (String ) i.next(); 247 if (!((className).endsWith(".class"))) { 248 continue; 249 } 250 251 if (this.packagePrefixes != null) { 253 254 boolean modify = false; 255 256 String fullyQualifiedClassName = className.replace(File.separatorChar, '.'); 257 258 for (Iterator p = this.packagePrefixes.iterator(); p.hasNext(); ) { 259 260 String packagePrefix = (String ) p.next(); 261 if (fullyQualifiedClassName.startsWith(packagePrefix)) { 263 264 modify = true; 265 break; 266 } 267 } 268 269 if (!modify) { 270 continue; 272 } 273 } 274 275 File classFile = new File (toDir, className); 277 ClassModifier classModifier = new ClassModifier(super.verbose, this.testGrammar, 278 super.excludeOneLineMethods, 279 super.excludeConstructors, 280 super.excludeIndividualMethods, 281 this.counter, classFile); 282 classModifier.modifyMethods(); 283 } 284 285 try { 287 288 this.counter.storeAllMethods(); 289 290 if (this.excludeOneLineMethods) { 292 storeMethods(this.oneLineSet, new File (super.categories.getFileName("oneLineFile"))); 293 } 294 295 if (this.excludeConstructors) { 297 storeMethods(this.constructorSet, 298 new File (super.categories.getFileName("constructorFile"))); 299 } 300 } 301 catch (IOException e) { 302 throw new JBlanketException("Unable to store methods", e); 303 } 304 } 305 306 313 protected void storeMethods(MethodSet methodSet, File file) throws IOException { 314 315 FileInputStream fistream = new FileInputStream (categories.getFileName("totalFile")); 317 try { 318 this.totalSet.load(fistream); 319 } 320 catch (ParseException e) { 321 } 324 325 methodSet.intersection(this.totalSet); 326 327 FileOutputStream fostream = new FileOutputStream (file); 329 methodSet.store(fostream, null, this.date); 330 331 fistream.close(); 332 fostream.close(); 333 } 334 335 343 private void processExcludeClasses(String toDir, List excludes) throws JBlanketException { 344 345 for (Iterator i = excludes.iterator(); i.hasNext(); ) { 347 348 String className = (String ) i.next(); 350 351 if (!className.endsWith(".class")) { 353 continue; 354 } 355 356 File classFile = new File (toDir, className); 357 ClassModifier modifier = new ClassModifier(this.verbose, counter, classFile); 358 modifier.excludeMethods(); 359 } 360 } 361 362 369 private void processJarFiles(String dir, List jars) throws JBlanketException { 370 371 final String slash = File.separator; 373 374 for (Iterator i = jars.iterator(); i.hasNext(); ) { 376 377 String temp = super.jblanketDir + slash + "temp"; 379 File tempDir = new File (temp); 380 381 if (tempDir.exists()) { 383 deleteDirectory(tempDir); 384 } 385 386 tempDir.mkdirs(); 388 389 JarFactory factory = new JarFactory(temp); 391 File jarFile = new File (dir, (String ) i.next()); 392 try { 393 factory.unJar(jarFile); 394 } 395 catch (Exception e) { 396 throw new JBlanketException("Unable to unjar JAR file " + jarFile.getAbsoluteFile(), e); 398 } 399 400 DirectoryScanner scanner = new DirectoryScanner(); 402 scanner.setIncludes(new String [] {"**"}); 403 scanner.setExcludes(new String [] {"**" + slash + "META-INF" + slash + "**"}); 404 scanner.setBasedir(tempDir); 405 scanner.scan(); 406 String files[] = scanner.getIncludedFiles(); 407 408 ArrayList includes = new ArrayList (); 410 for (int j = 0; j < files.length; j++) { 411 includes.add(files[j]); 412 } 413 processIncludeClasses(temp, includes); 414 415 try { 417 factory.jar(jarFile); 418 } 419 catch (Exception e) { 420 throw new JBlanketException("Unable to jar JAR file " + jarFile.getAbsoluteFile(), e); 422 } 423 424 deleteDirectory(tempDir); 426 } 427 } 428 429 436 void deleteDirectory(File dir) { 437 438 DirectoryScanner scanner = new DirectoryScanner(); 440 scanner.setIncludes(new String [] {"**/**"}); 441 scanner.setBasedir(dir); 442 scanner.scan(); 443 444 String [] files = scanner.getIncludedFiles(); 446 for (int j = 0; j < files.length; j++) { 447 (new File (dir.getAbsolutePath(), files[j])).delete(); 448 } 449 450 String [] directories = scanner.getIncludedDirectories(); 452 for (int j = directories.length - 1; j > -1 ; j--) { 453 (new File (dir.getAbsolutePath(), directories[j])).delete(); 454 } 455 } 456 457 465 private void updateTotalMethods() throws FileNotFoundException , IOException { 466 467 FileOutputStream fostream = new FileOutputStream (categories.getFileName("untestableFile")); 469 this.untestableSet.store(fostream, null, this.date); 470 471 fostream = new FileOutputStream (categories.getFileName("excludedFile")); 473 this.excludedSet.store(fostream, null, this.date); 474 475 this.totalSet.difference(this.untestableSet); 478 this.totalSet.difference(this.excludedSet); 479 fostream = new FileOutputStream (categories.getFileName("totalFile")); 480 this.totalSet.store(fostream, null, this.date); 481 fostream.close(); 482 } 483 484 492 static List stringToArrayList(String string) { 493 494 List list = new ArrayList (); 495 496 if (string != null) { 497 498 StringTokenizer tokens = new StringTokenizer (string, ";"); 499 while (tokens.hasMoreElements()) { 500 list.add(tokens.nextToken()); 501 } 502 } 503 504 return list; 505 } 506 507 517 public void modify(String classDir, List includes, List excludes, List jars) 518 throws JBlanketException { 519 520 processIncludeClasses(classDir, includes); 521 processExcludeClasses(classDir, excludes); 522 processJarFiles(classDir, jars); 523 524 try { 525 updateTotalMethods(); 526 } 527 catch (IOException e) { 528 throw new JBlanketException("Unable to update the total methods set", e); 529 } 530 } 531 532 541 public static void main(String args[]) throws IOException , Throwable { 542 main(java.util.Arrays.asList(args)); 543 } 544 545 572 public static void main(List args) throws IOException , JBlanketException, ParseException { 573 574 String classDir = null; 576 577 String includes = null; 579 String jars = null; 581 String excludes = null; 583 584 String packagePrefixes = null; 586 587 boolean verbose = false; 589 String testGrammar = ""; 591 592 boolean excludeOneLineMethods = false; 594 boolean excludeConstructors = false; 596 boolean excludeIndividualMethods = false; 597 598 MethodCategories categories = MethodCategories.getInstance(); 599 600 int i; 602 for (i = 0; i < args.size(); ++i) { 604 String argument = (String ) args.get(i); 605 if (argument.equals("-classDir")) { 606 classDir = (String ) args.get(++i); 607 } 608 else if (argument.equals("-include")) { 609 String includeFile = (String ) args.get(++i); 610 if (includeFile.endsWith(".jar")) { 611 jars = includeFile; 612 } 613 else { 614 includes = includeFile; 615 } 616 } 617 else if (argument.equals("-exclude")) { 618 excludes = (String ) args.get(++i); 619 } 620 else if (argument.equals("-verbose")) { 621 verbose = ((Boolean ) args.get(++i)).booleanValue(); 622 } 623 else if (argument.equals("-testGrammar")) { 624 testGrammar = (String ) args.get(++i); 625 } 626 else if (argument.equals("-excludeOneLineMethods")) { 627 excludeOneLineMethods = ((Boolean ) args.get(++i)).booleanValue(); 628 } 629 else if (argument.equals("-excludeConstructors")) { 630 excludeConstructors = ((Boolean ) args.get(++i)).booleanValue(); 631 } 632 else if (argument.equals("-excludeIndividualMethods")) { 633 excludeIndividualMethods = ((Boolean ) args.get(++i)).booleanValue(); 634 } 635 else if (argument.equals("-totalFile")) { 636 categories.addCategory("totalFile", (String ) args.get(++i)); 637 } 638 else if (argument.equals("-untestableFile")) { 639 categories.addCategory("untestableFile", (String ) args.get(++i)); 640 } 641 else if (argument.equals("-excludedFile")) { 642 categories.addCategory("excludedFile", (String ) args.get(++i)); 643 } 644 else if (argument.equals("-oneLineFile")) { 645 categories.addCategory("oneLineFile", (String ) args.get(++i)); 646 } 647 else if (argument.equals("-constructorFile")) { 648 categories.addCategory("constructorFile", (String ) args.get(++i)); 649 } 650 else if (argument.equals("-packagePrefix")) { 651 packagePrefixes = (String ) args.get(++i); 652 } 653 else { 654 System.out.println("Incorrect usage: " + argument); 655 System.exit(1); 656 } 657 } 658 Modifier modifier = new Modifier(verbose, testGrammar, excludeOneLineMethods, 660 excludeConstructors, excludeIndividualMethods, 661 stringToArrayList(packagePrefixes)); 662 modifier.modify(classDir, stringToArrayList(includes), stringToArrayList(excludes), 663 stringToArrayList(jars)); 664 } 665 } 666 | Popular Tags |