1 23 24 25 package com.sun.jdo.api.persistence.enhancer.impl; 26 27 import java.util.Hashtable ; 28 import java.util.Iterator ; 29 import java.util.HashMap ; 30 import java.util.ArrayList ; 31 32 import java.io.File ; 33 import java.io.PrintWriter ; 35 36 import com.sun.jdo.api.persistence.enhancer.classfile.ClassFile; 37 import com.sun.jdo.api.persistence.enhancer.classfile.ClassField; 38 39 import com.sun.jdo.api.persistence.enhancer.util.ClassPath; 40 import com.sun.jdo.api.persistence.enhancer.util.ClassFileSource; 41 import com.sun.jdo.api.persistence.enhancer.util.Support; 42 43 import com.sun.jdo.api.persistence.enhancer.meta.JDOMetaData; 45 46 47 67 69 70 74 public final class Environment 75 extends Support { 76 77 78 private PrintWriter out = new PrintWriter (System.out, true); 80 81 82 private PrintWriter err = new PrintWriter (System.err, true); 84 85 86 private boolean timingOption = false; 88 89 90 private boolean verboseOption = false; 91 92 93 private boolean quietOption = false; 94 95 96 100 101 102 private boolean noWriteOption = false; 103 104 105 109 110 111 115 116 118 private boolean disableThisHookHoisting; 119 120 121 private boolean disableInitializerAnnotationSuppression; 122 123 124 128 129 130 131 135 136 137 private boolean forceOverwriteOption = false; 138 139 141 private boolean updateInPlaceOption = false; 142 143 144 private int errorsEncountered = 0; 145 146 147 private File destinationDirectory = null; 148 149 150 private Hashtable classMap = new Hashtable (11); 151 152 153 private HashMap missingClasses = new HashMap (11); 155 156 159 163 164 166 170 171 172 private ClassPath classPathOption = new ClassPath(""); 175 176 private ClassPath destClassPath; 177 178 179 private Hashtable fieldSuppressions = new Hashtable (); 180 181 182 private Hashtable classSuppressions = new Hashtable (); 183 184 185 private JDOMetaData jdoMetaData; 187 188 189 private String lastErrorMessage = null; 190 191 193 public void setDoTimingStatistics(boolean dontOpt) { 194 timingOption = dontOpt; 195 } 196 197 public boolean doTimingStatistics() { 198 return timingOption; 199 } 200 201 public void error(String error) { 203 errorsEncountered++; 204 err.println(lastErrorMessage = getI18N("enhancer.enumerated_error", 209 errorsEncountered, 210 error)); 211 } 212 213 public void warning(String warn) { 214 if (!quietOption) { 215 out.println(getI18N("enhancer.warning", warn)); } 221 } 222 223 public void warning(String warn, String classname) { 224 if (!quietOption && 225 !classWarningsSuppressed(classname)) { 226 out.println(getI18N("enhancer.warning", warn)); } 232 } 233 234 public void warning(String warn, String classname, String fieldname) { 235 if (!quietOption && 236 !classWarningsSuppressed(classname) && 237 !fieldWarningsSuppressed(classname, fieldname)) { 238 out.print(getI18N("enhancer.warning", warn)); } 244 } 245 246 public void message(String mess) { 247 if (verboseOption) { 248 out.println("JDO ENHANCER: " + mess); } 252 } 253 254 public void messageNL(String mess) { 255 if (verboseOption) { 256 out.println(); 259 out.println("JDO ENHANCER: " + mess); } 261 } 262 263 public int errorCount() { 264 return errorsEncountered; 265 } 266 267 public final String getLastErrorMessage () { 268 return this.lastErrorMessage; 269 } 270 271 281 282 public boolean forceOverwrite() { 283 return forceOverwriteOption; 284 } 285 286 public boolean updateInPlace() { 287 return updateInPlaceOption; 288 } 289 290 public File destinationDirectory() { 292 return destinationDirectory; 293 } 294 295 302 303 310 311 315 321 322 public boolean writeClasses() { 323 return (noWriteOption == false && errorsEncountered == 0); 324 } 325 326 332 333 public boolean doThisOptimization() { 334 return disableThisHookHoisting == false; 335 } 336 337 343 344 public boolean doInitializerOptimization() { 345 return disableInitializerAnnotationSuppression == false; 346 } 347 348 354 355 361 362 366 383 384 385 public JDOMetaData getJDOMetaData() 387 { 388 return jdoMetaData; 389 } 390 391 392 public void setJDOMetaData(JDOMetaData jdoMetaData) 394 { 395 this.jdoMetaData = jdoMetaData; 396 } 397 398 405 417 418 422 434 435 439 public void addClass(ClassControl cc) { 441 String className = cc.className(); 442 ClassControl existCC = getClass(className); 443 444 if (existCC != null) { 445 446 if (!existCC.source().sameAs(cc.source())) { 447 error(getI18N("enhancer.class_already_entered", cc.userClassName(), 450 cc.sourceName(), 451 existCC.sourceName())); 452 return; 453 } 454 455 if (cc.persistType() == ClassControl.PersistUnknown || 458 existCC.persistType() == ClassControl.PersistCapable || 459 (existCC.persistType() == ClassControl.PersistAware && 460 cc.persistType() != ClassControl.PersistCapable)) 461 return; 462 463 } 464 465 if (existCC == null && cc.sourceName() != null) 466 message("adding class " + cc.userClassName() + " from " + cc.sourceName()); 469 classMap.put(className, cc); 470 } 471 472 475 485 486 491 public boolean canFindClass(String className) { 492 return findClass(className) != null; 493 } 494 495 500 public ClassControl getClass(String className) { 501 return (ClassControl)classMap.get(className); 502 } 503 504 public Iterator getClasses() { 506 return classMap.values().iterator(); 507 } 508 509 514 public ClassControl findClass(String className) { 515 ClassControl cc = (ClassControl) classMap.get(className); 516 517 if ((cc == null) && (missingClasses.get(className) == null)) { 518 519 cc = lookupClass(className); 521 if (cc != null) { 522 message("Reading class " + cc.userClassName() + " from " + cc.sourceName()); classMap.put(className, cc); 525 } else { 526 missingClasses.put(className, className); 527 } 528 } 529 530 return cc; 531 } 532 533 538 public ClassControl lookupClass(String className) { 539 ClassFileSource source = classPathOption.findClass(className); 540 541 while (true) { 542 if (source == null) 543 return null; 544 545 try { 547 ClassControl cc = new ClassControl(source, this); 548 if (cc.className() != null && 549 cc.className().equals(className)) 550 return cc; 551 } catch (ClassFormatError e) { 552 } 553 554 source = source.nextSource(className); 556 } 557 } 558 559 560 564 570 571 575 public ArrayList collectClasses(int persistType) { 576 ArrayList v = new ArrayList (); 577 for (Iterator e = classMap.values().iterator(); e.hasNext();) { 578 ClassControl cc = (ClassControl)e.next(); 579 if (cc.persistType() == persistType) 580 v.add(cc); 581 } 582 return v; 583 } 584 585 589 public ArrayList collectClasses() { 590 ArrayList v = new ArrayList (); 591 for (Iterator e = classMap.values().iterator(); e.hasNext(); ) 592 v.add(e.next()); 593 return v; 594 } 595 596 597 602 public ClassFileSource lookupDestClass(String className) { 603 if (destClassPath == null && destinationDirectory != null) 604 destClassPath = new ClassPath(destinationDirectory.getPath()); 605 return (destClassPath == null 606 ? null : destClassPath.findClass(className)); 607 } 608 609 611 612 615 public Environment() { 616 } 617 618 625 626 public void setDestinationDirectory(String dir) { 627 final File dest = new File (dir); 628 if (destinationDirectory != null) { 629 error(getI18N("destination_directory_already_set", dir, 632 destinationDirectory.getPath())); 633 return; 634 } 635 if (!dest.isDirectory()) { 636 error(getI18N("enhancer.destination_directory_not_exist", dir)); 638 return; 639 } 640 destinationDirectory = dest; 641 } 642 643 647 public void excludeDestinationDirectory() { 648 if (destinationDirectory != null) 649 classPathOption.remove(destinationDirectory); 650 } 651 652 656 public void moveDestinationDirectoryToEnd() { 657 if (destinationDirectory != null && 658 classPathOption.remove(destinationDirectory)) 659 classPathOption.append(destinationDirectory); 660 } 661 662 public void setOutputWriter(PrintWriter out) { 664 this.out = out; 665 } 666 667 public PrintWriter getOutputWriter() { 669 return out; 670 } 671 672 public void setErrorWriter(PrintWriter err) { 674 this.err = err; 675 } 676 677 public PrintWriter getErrorWriter() { 679 return err; 680 } 681 682 public void setVerbose(boolean beVerbose) { 683 verboseOption = beVerbose; 684 } 685 686 public boolean isVerbose() { 687 return this.verboseOption; 688 } 689 690 public void setQuiet(boolean beQuiet) { 691 quietOption = beQuiet; 692 } 693 694 700 701 707 708 public void setNoWrite(boolean dontWrite) { 709 noWriteOption = dontWrite; 710 } 711 712 714 721 722 public void setNoThisOptimization(boolean dontOpt) { 723 disableThisHookHoisting = dontOpt; 724 } 725 726 public void setNoInitializerOptimization(boolean dontOpt) { 727 disableInitializerAnnotationSuppression = dontOpt; 728 } 729 730 public void setNoOptimization(boolean dontOpt) { 731 735 disableThisHookHoisting = dontOpt; 736 disableInitializerAnnotationSuppression = dontOpt; 737 741 } 742 743 public void setForceOverwrite(boolean forceOverwrite) { 744 forceOverwriteOption = forceOverwrite; 745 } 746 747 753 754 774 775 778 public void suppressClassWarnings(String className) { 779 classSuppressions.put(className, className); 780 } 781 782 785 public void suppressFieldWarnings(String fullFieldName) { 786 fieldSuppressions.put(fullFieldName, fullFieldName); 787 } 788 789 793 static String validVMPackage(String pkg) { 794 StringBuffer buf = new StringBuffer (); 795 796 int i=0; 797 while (i<pkg.length()) { 798 if (i != 0) { 799 if (pkg.charAt(i) != '.') 801 return null; 802 803 buf.append("/"); 806 i++; 808 if (i == pkg.length()) 809 return null; 810 } 811 812 if (!Character.isJavaIdentifierStart(pkg.charAt(i))) 813 return null; 814 buf.append(pkg.charAt(i++)); 815 816 while (i < pkg.length() && 817 Character.isJavaIdentifierPart(pkg.charAt(i))) 818 buf.append(pkg.charAt(i++)); 819 } 820 821 return buf.toString(); 822 } 823 824 827 private boolean classWarningsSuppressed(String classname) { 828 return classSuppressions.get(classname) != null; 829 } 830 831 835 private boolean fieldWarningsSuppressed(String classname, 836 String fieldName) { 837 return fieldSuppressions.get(classname + "." + fieldName) != null; } 839 840 843 public void reset() { 845 867 errorsEncountered = 0; 868 869 classMap.clear(); 870 missingClasses.clear(); 871 872 fieldSuppressions.clear(); 873 classSuppressions.clear(); 874 } 875 } 876 | Popular Tags |