| 1 11 12 package org.eclipse.jdt.core.dom; 13 14 import java.lang.reflect.Constructor ; 15 import java.lang.reflect.InvocationTargetException ; 16 import java.util.ArrayList ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.StringTokenizer ; 20 21 import org.eclipse.core.runtime.IProgressMonitor; 22 23 import org.eclipse.jdt.core.IClassFile; 24 import org.eclipse.jdt.core.ICompilationUnit; 25 import org.eclipse.jdt.core.IJavaProject; 26 import org.eclipse.jdt.core.JavaCore; 27 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; 28 import org.eclipse.jdt.internal.compiler.parser.Scanner; 29 import org.eclipse.jface.text.IDocument; 30 import org.eclipse.text.edits.TextEdit; 31 32 95 public final class AST { 96 109 public static final int JLS2 = 2; 110 111 116 static final int JLS2_INTERNAL = JLS2; 117 118 130 public static final int JLS3 = 3; 131 132 136 private BindingResolver resolver = new BindingResolver(); 137 138 143 private NodeEventHandler eventHandler = new NodeEventHandler(); 144 145 149 int apiLevel; 150 151 155 private long modificationCount = 0; 156 157 163 private long originalModificationCount = 0; 164 165 175 private int disableEvents = 0; 176 177 182 private final Object internalASTLock = new Object (); 183 184 188 Scanner scanner; 189 190 193 InternalASTRewrite rewriter; 194 195 198 private int defaultNodeFlag = 0; 199 200 207 private AST(int level) { 208 if ((level != AST.JLS2) 209 && (level != AST.JLS3)) { 210 throw new IllegalArgumentException (); 211 } 212 this.apiLevel = level; 213 this.scanner = new Scanner( 215 true , 216 true , 217 false , 218 ClassFileConstants.JDK1_3 , 219 ClassFileConstants.JDK1_5 , 220 null, 221 null, 222 true); 223 } 224 225 232 public AST() { 233 this(JavaCore.getDefaultOptions()); 234 } 235 236 253 public static CompilationUnit convertCompilationUnit( 254 int level, 255 org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration compilationUnitDeclaration, 256 char[] source, 257 Map options, 258 boolean isResolved, 259 org.eclipse.jdt.internal.core.CompilationUnit workingCopy, 260 int reconcileFlags, 261 IProgressMonitor monitor) { 262 263 ASTConverter converter = new ASTConverter(options, isResolved, monitor); 264 AST ast = AST.newAST(level); 265 int savedDefaultNodeFlag = ast.getDefaultNodeFlag(); 266 ast.setDefaultNodeFlag(ASTNode.ORIGINAL); 267 BindingResolver resolver = null; 268 if (isResolved) { 269 resolver = new DefaultBindingResolver(compilationUnitDeclaration.scope, workingCopy.owner, new DefaultBindingResolver.BindingTables(), false); 270 ast.setFlag(AST.RESOLVED_BINDINGS); 271 } else { 272 resolver = new BindingResolver(); 273 } 274 ast.setFlag(reconcileFlags); 275 ast.setBindingResolver(resolver); 276 converter.setAST(ast); 277 278 CompilationUnit unit = converter.convert(compilationUnitDeclaration, source); 279 unit.setLineEndTable(compilationUnitDeclaration.compilationResult.getLineSeparatorPositions()); 280 unit.setTypeRoot(workingCopy); 281 ast.setDefaultNodeFlag(savedDefaultNodeFlag); 282 return unit; 283 } 284 285 308 public AST(Map options) { 309 this(JLS2); 310 Object sourceLevelOption = options.get(JavaCore.COMPILER_SOURCE); 311 long sourceLevel = ClassFileConstants.JDK1_3; 312 if (JavaCore.VERSION_1_4.equals(sourceLevelOption)) { 313 sourceLevel = ClassFileConstants.JDK1_4; 314 } else if (JavaCore.VERSION_1_5.equals(sourceLevelOption)) { 315 sourceLevel = ClassFileConstants.JDK1_5; 316 } 317 Object complianceLevelOption = options.get(JavaCore.COMPILER_COMPLIANCE); 318 long complianceLevel = ClassFileConstants.JDK1_3; 319 if (JavaCore.VERSION_1_4.equals(complianceLevelOption)) { 320 complianceLevel = ClassFileConstants.JDK1_4; 321 } else if (JavaCore.VERSION_1_5.equals(complianceLevelOption)) { 322 complianceLevel = ClassFileConstants.JDK1_5; 323 } 324 this.scanner = new Scanner( 326 true , 327 true , 328 false , 329 sourceLevel , 330 complianceLevel , 331 null, 332 null, 333 true); 334 } 335 336 352 public static AST newAST(int level) { 353 if ((level != AST.JLS2) 354 && (level != AST.JLS3)) { 355 throw new IllegalArgumentException (); 356 } 357 return new AST(level); 358 } 359 360 385 public long modificationCount() { 386 return this.modificationCount; 387 } 388 389 396 public int apiLevel() { 397 return this.apiLevel; 398 } 399 400 416 void modifying() { 417 if (this.disableEvents > 0) { 420 return; 421 } 422 this.modificationCount++; 424 } 425 426 433 final void disableEvents() { 434 synchronized (this.internalASTLock) { 435 this.disableEvents++; 437 } 438 } 440 441 448 final void reenableEvents() { 449 synchronized (this.internalASTLock) { 450 this.disableEvents--; 452 } 453 } 454 455 463 void preRemoveChildEvent(ASTNode node, ASTNode child, StructuralPropertyDescriptor property) { 464 synchronized (this.internalASTLock) { 466 if (this.disableEvents > 0) { 468 return; 471 } else { 472 disableEvents(); 473 } 474 } 475 try { 476 this.eventHandler.preRemoveChildEvent(node, child, property); 477 } finally { 480 reenableEvents(); 481 } 482 } 483 484 492 void postRemoveChildEvent(ASTNode node, ASTNode child, StructuralPropertyDescriptor property) { 493 synchronized (this.internalASTLock) { 495 if (this.disableEvents > 0) { 497 return; 500 } else { 501 disableEvents(); 502 } 503 } 504 try { 505 this.eventHandler.postRemoveChildEvent(node, child, property); 506 } finally { 509 reenableEvents(); 510 } 511 } 512 513 522 void preReplaceChildEvent(ASTNode node, ASTNode child, ASTNode newChild, StructuralPropertyDescriptor property) { 523 synchronized (this.internalASTLock) { 525 if (this.disableEvents > 0) { 527 return; 530 } else { 531 disableEvents(); 532 } 533 } 534 try { 535 this.eventHandler.preReplaceChildEvent(node, child, newChild, property); 536 } finally { 539 reenableEvents(); 540 } 541 } 542 543 552 void postReplaceChildEvent(ASTNode node, ASTNode child, ASTNode newChild, StructuralPropertyDescriptor property) { 553 synchronized (this.internalASTLock) { 555 if (this.disableEvents > 0) { 557 return; 560 } else { 561 disableEvents(); 562 } 563 } 564 try { 565 this.eventHandler.postReplaceChildEvent(node, child, newChild, property); 566 } finally { 569 reenableEvents(); 570 } 571 } 572 573 581 void preAddChildEvent(ASTNode node, ASTNode child, StructuralPropertyDescriptor property) { 582 synchronized (this.internalASTLock) { 584 if (this.disableEvents > 0) { 586 return; 589 } else { 590 disableEvents(); 591 } 592 } 593 try { 594 this.eventHandler.preAddChildEvent(node, child, property); 595 } finally { 598 reenableEvents(); 599 } 600 } 601 602 610 void postAddChildEvent(ASTNode node, ASTNode child, StructuralPropertyDescriptor property) { 611 synchronized (this.internalASTLock) { 613 if (this.disableEvents > 0) { 615 return; 618 } else { 619 disableEvents(); 620 } 621 } 622 try { 623 this.eventHandler.postAddChildEvent(node, child, property); 624 } finally { 627 reenableEvents(); 628 } 629 } 630 631 639 void preValueChangeEvent(ASTNode node, SimplePropertyDescriptor property) { 640 synchronized (this.internalASTLock) { 642 if (this.disableEvents > 0) { 644 return; 647 } else { 648 disableEvents(); 649 } 650 } 651 try { 652 this.eventHandler.preValueChangeEvent(node, property); 653 } finally { 656 reenableEvents(); 657 } 658 } 659 660 668 void postValueChangeEvent(ASTNode node, SimplePropertyDescriptor property) { 669 synchronized (this.internalASTLock) { 671 if (this.disableEvents > 0) { 673 return; 676 } else { 677 disableEvents(); 678 } 679 } 680 try { 681 this.eventHandler.postValueChangeEvent(node, property); 682 } finally { 685 reenableEvents(); 686 } 687 } 688 689 695 void preCloneNodeEvent(ASTNode node) { 696 synchronized (this.internalASTLock) { 697 if (this.disableEvents > 0) { 699 return; 702 } else { 703 disableEvents(); 704 } 705 } 706 try { 707 this.eventHandler.preCloneNodeEvent(node); 708 } finally { 711 reenableEvents(); 712 } 713 } 714 715 722 void postCloneNodeEvent(ASTNode node, ASTNode clone) { 723 synchronized (this.internalASTLock) { 724 if (this.disableEvents > 0) { 726 return; 729 } else { 730 disableEvents(); 731 } 732 } 733 try { 734 this.eventHandler.postCloneNodeEvent(node, clone); 735 } finally { 738 reenableEvents(); 739 } 740 } 741 742 800 public static CompilationUnit parseCompilationUnit( 801 ICompilationUnit unit, 802 boolean resolveBindings) { 803 804 try { 805 ASTParser c = ASTParser.newParser(AST.JLS2); 806 c.setSource(unit); 807 c.setResolveBindings(resolveBindings); 808 ASTNode result = c.createAST(null); 809 return (CompilationUnit) result; 810 } catch (IllegalStateException e) { 811 throw new IllegalArgumentException (); 813 } 814 } 815 816 875 public static CompilationUnit parseCompilationUnit( 876 IClassFile classFile, 877 boolean resolveBindings) { 878 879 if (classFile == null) { 880 throw new IllegalArgumentException (); 881 } 882 try { 883 ASTParser c = ASTParser.newParser(AST.JLS2); 884 c.setSource(classFile); 885 c.setResolveBindings(resolveBindings); 886 ASTNode result = c.createAST(null); 887 return (CompilationUnit) result; 888 } catch (IllegalStateException e) { 889 throw new IllegalArgumentException (); 891 } 892 } 893 894 |