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 961 public static CompilationUnit parseCompilationUnit( 962 char[] source, 963 String unitName, 964 IJavaProject project) { 965 966 if (source == null) { 967 throw new IllegalArgumentException (); 968 } 969 ASTParser astParser = ASTParser.newParser(AST.JLS2); 970 astParser.setSource(source); 971 astParser.setUnitName(unitName); 972 astParser.setProject(project); 973 astParser.setResolveBindings(project != null); 974 ASTNode result = astParser.createAST(null); 975 return (CompilationUnit) result; 976 } 977 978 1013 public static CompilationUnit parseCompilationUnit(char[] source) { 1014 if (source == null) { 1015 throw new IllegalArgumentException (); 1016 } 1017 ASTParser c = ASTParser.newParser(AST.JLS2); 1018 c.setSource(source); 1019 ASTNode result = c.createAST(null); 1020 return (CompilationUnit) result; 1021 } 1022 1023 1028 BindingResolver getBindingResolver() { 1029 return this.resolver; 1030 } 1031 1032 1038 NodeEventHandler getEventHandler() { 1039 return this.eventHandler; 1040 } 1041 1042 1048 void setEventHandler(NodeEventHandler eventHandler) { 1049 if (this.eventHandler == null) { 1050 throw new IllegalArgumentException (); 1051 } 1052 this.eventHandler = eventHandler; 1053 } 1054 1055 1061 int getDefaultNodeFlag() { 1062 return this.defaultNodeFlag; 1063 } 1064 1065 1071 void setDefaultNodeFlag(int flag) { 1072 this.defaultNodeFlag = flag; 1073 } 1074 1075 1080 void setOriginalModificationCount(long count) { 1081 this.originalModificationCount = count; 1082 } 1083 1084 1129 public ITypeBinding resolveWellKnownType(String name) { 1130 if (name == null) { 1131 return null; 1132 } 1133 return getBindingResolver().resolveWellKnownType(name); 1134 } 1135 1136 1141 void setBindingResolver(BindingResolver resolver) { 1142 if (resolver == null) { 1143 throw new IllegalArgumentException (); 1144 } 1145 this.resolver = resolver; 1146 } 1147 1148 1155 void unsupportedIn2() { 1156 if (this.apiLevel == AST.JLS2) { 1157 throw new UnsupportedOperationException ("Operation not supported in JLS2 AST"); } 1159 } 1160 1161 1168 void supportedOnlyIn2() { 1169 if (this.apiLevel != AST.JLS2) { 1170 throw new UnsupportedOperationException ("Operation not supported in JLS2 AST"); } 1172 } 1173 1174 1178 private static final Class [] AST_CLASS = new Class [] {AST.class}; 1179 1180 1184 private final Object [] THIS_AST= new Object [] {this}; 1185 1186 1189 static final int RESOLVED_BINDINGS = 0x80000000; 1190 1191 1194 private int bits; 1195 1196 1206 public ASTNode createInstance(Class nodeClass) { 1207 if (nodeClass == null) { 1208 throw new IllegalArgumentException (); 1209 } 1210 try { 1211 Constructor c = nodeClass.getDeclaredConstructor(AST_CLASS); 1213 Object result = c.newInstance(this.THIS_AST); 1214 return (ASTNode) result; 1215 } catch (NoSuchMethodException e) { 1216 throw new IllegalArgumentException (); 1219 } catch (InstantiationException e) { 1220 throw new IllegalArgumentException (); 1223 } catch (IllegalAccessException e) { 1224 throw new IllegalArgumentException (); 1227 } catch (InvocationTargetException e) { 1228 throw new IllegalArgumentException (); 1231 } 1232 } 1233 1234 1248 public ASTNode createInstance(int nodeType) { 1249 Class nodeClass = ASTNode.nodeClassForType(nodeType); 1251 return createInstance(nodeClass); 1252 } 1253 1254 1264 public SimpleName newSimpleName(String identifier) { 1265 if (identifier == null) { 1266 throw new IllegalArgumentException (); 1267 } 1268 SimpleName result = new SimpleName(this); 1269 result.setIdentifier(identifier); 1270 return result; 1271 } 1272 1273 1286 public QualifiedName newQualifiedName( 1287 Name qualifier, 1288 SimpleName name) { 1289 QualifiedName result = new QualifiedName(this); 1290 result.setQualifier(qualifier); 1291 result.setName(name); 1292 return result; 1293 1294 } 1295 1296 1312 public Name newName(String [] identifiers) { 1313 int count = identifiers.length; 1315 if (count == 0) { 1316 throw new IllegalArgumentException (); 1317 } 1318 Name result = newSimpleName(identifiers[0]); 1319 for (int i = 1; i < count; i++) { 1320 SimpleName name = newSimpleName(identifiers[i]); 1321 result = newQualifiedName(result, name); 1322 } 1323 return result; 1324 } 1325 1326 1329 Name internalNewName(String [] identifiers) { 1330 int count = identifiers.length; 1331 if (count == 0) { 1332 throw new IllegalArgumentException (); 1333 } 1334 final SimpleName simpleName = new SimpleName(this); 1335 simpleName.internalSetIdentifier(identifiers[0]); 1336 Name result = simpleName; 1337 for (int i = 1; i < count; i++) { 1338 SimpleName name = new SimpleName(this); 1339 name.internalSetIdentifier(identifiers[i]); 1340 result = newQualifiedName(result, name); 1341 } 1342 return result; 1343 } 1344 1345 1368 public Name newName(String qualifiedName) { 1369 StringTokenizer t = new StringTokenizer (qualifiedName, ".", true); Name result = null; 1371 int balance = 0; 1374 while(t.hasMoreTokens()) { 1375 String s = t.nextToken(); 1376 if (s.indexOf('.') >= 0) { 1377 if (s.length() > 1) { 1379 throw new IllegalArgumentException (); 1381 } 1382 balance--; 1383 if (balance < 0) { 1384 throw new IllegalArgumentException (); 1385 } 1386 } else { 1387 balance++; 1389 SimpleName name = newSimpleName(s); 1390 if (result == null) { 1391 result = name; 1392 } else { 1393 result = newQualifiedName(result, name); 1394 } 1395 } 1396 } 1397 if (balance != 1) { 1398 throw new IllegalArgumentException (); 1399 } 1400 return result; 1401 } 1402 1403 1420 public SimpleType newSimpleType(Name typeName) { 1421 SimpleType result = new SimpleType(this); 1422 result.setName(typeName); 1423 return result; 1424 } 1425 1426 1439 public ArrayType newArrayType(Type componentType) { 1440 ArrayType result = new ArrayType(this); 1441 result.setComponentType(componentType); 1442 return result; 1443 } 1444 1445 1467 public ArrayType newArrayType(Type elementType, int dimensions) { 1468 if (elementType == null || elementType.isArrayType()) { 1469 throw new IllegalArgumentException (); 1470 } 1471 if (dimensions < 1 || dimensions > 1000) { 1472 throw new IllegalArgumentException (); 1474 } 1475 ArrayType result = new ArrayType(this); 1476 result.setComponentType(elementType); 1477 for (int i = 2; i <= dimensions; i++) { 1478 result = newArrayType(result); 1479 } 1480 return result; 1481 1482 } 1483 1484 1493 public PrimitiveType newPrimitiveType(PrimitiveType.Code typeCode) { 1494 PrimitiveType result = new PrimitiveType(this); 1495 result.setPrimitiveTypeCode(typeCode); 1496 return result; 1497 } 1498 1499 1514 public ParameterizedType newParameterizedType(Type type) { 1515 ParameterizedType result = new ParameterizedType(this); 1516 result.setType(type); 1517 return result; 1518 } 1519 1520 1536 public QualifiedType newQualifiedType(Type qualifier, SimpleName name) { 1537 QualifiedType result = new QualifiedType(this); 1538 result.setQualifier(qualifier); 1539 result.setName(name); 1540 return result; 1541 } 1542 1543 1552 public WildcardType newWildcardType() { 1553 WildcardType result = new WildcardType(this); 1554 return result; 1555 } 1556 1557 1565 public CompilationUnit newCompilationUnit() { 1566 return new CompilationUnit(this); 1567 } 1568 1569 1576 public PackageDeclaration newPackageDeclaration() { 1577 PackageDeclaration result = new PackageDeclaration(this); 1578 return result; 1579 } 1580 1581 1588 public ImportDeclaration newImportDeclaration() { 1589 ImportDeclaration result = new ImportDeclaration(this); 1590 return result; 1591 } 1592 1593 1609 public TypeDeclaration newTypeDeclaration() { 1610 TypeDeclaration result = new TypeDeclaration(this); 1611 result.setInterface(false); 1612 return result; 1613 } 1614 1615 1629 public MethodDeclaration newMethodDeclaration() { 1630 MethodDeclaration result = new MethodDeclaration(this); 1631 result.setConstructor(false); 1632 return result; 1633 } 1634 1635 1643 public SingleVariableDeclaration newSingleVariableDeclaration() { 1644 SingleVariableDeclaration result = new SingleVariableDeclaration(this); 1645 return result; 1646 } 1647 1648 1655 public VariableDeclarationFragment newVariableDeclarationFragment() { 1656 VariableDeclarationFragment result = new VariableDeclarationFragment(this); 1657 return result; 1658 } 1659 1660 1667 public Initializer newInitializer() { 1668 Initializer result = new Initializer(this); 1669 return result; 1670 } 1671 1672 1683 public EnumConstantDeclaration newEnumConstantDeclaration() { 1684 EnumConstantDeclaration result = new EnumConstantDeclaration(this); 1685 return result; 1686 } 1687 1688 1700 public EnumDeclaration newEnumDeclaration() { 1701 EnumDeclaration result = new EnumDeclaration(this); 1702 return result; 1703 } 1704 1705 1714 public TypeParameter newTypeParameter() { 1715 TypeParameter result = new TypeParameter(this); 1716 return result; 1717 } 1718 1719 1729 public AnnotationTypeDeclaration newAnnotationTypeDeclaration() { 1730 AnnotationTypeDeclaration result = new AnnotationTypeDeclaration(this); 1731 return result; 1732 } 1733 1734 1745 public AnnotationTypeMemberDeclaration newAnnotationTypeMemberDeclaration() { 1746 AnnotationTypeMemberDeclaration result = new AnnotationTypeMemberDeclaration(this); 1747 return result; 1748 } 1749 1750 1761 public Modifier newModifier(Modifier.ModifierKeyword keyword) { 1762 Modifier result = new Modifier(this); 1763 result.setKeyword(keyword); 1764 return result; 1765 } 1766 1767 1782 public List newModifiers(int flags) { 1783 if (this.apiLevel == AST.JLS2) { 1784 unsupportedIn2(); 1785 } 1786 List result = new ArrayList (3); if (Modifier.isPublic(flags)) { 1788 result.add(newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); 1789 } 1790 if (Modifier.isProtected(flags)) { 1791 result.add(newModifier(Modifier.ModifierKeyword.PROTECTED_KEYWORD)); 1792 } 1793 if (Modifier.isPrivate(flags)) { 1794 result.add(newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD)); 1795 } 1796 if (Modifier.isAbstract(flags)) { 1797 result.add(newModifier(Modifier.ModifierKeyword.ABSTRACT_KEYWORD)); 1798 } 1799 if (Modifier.isStatic(flags)) { 1800 result.add(newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD)); 1801 } 1802 if (Modifier.isFinal(flags)) { 1803 result.add(newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD)); 1804 } 1805 if (Modifier.isSynchronized(flags)) { 1806 result.add(newModifier(Modifier.ModifierKeyword.SYNCHRONIZED_KEYWORD)); 1807 } 1808 if (Modifier.isNative(flags)) { 1809 result.add(newModifier(Modifier.ModifierKeyword.NATIVE_KEYWORD)); 1810 } 1811 if (Modifier.isStrictfp(flags)) { 1812 result.add(newModifier(Modifier.ModifierKeyword.STRICTFP_KEYWORD)); 1813 } 1814 if (Modifier.isTransient(flags)) { 1815 result.add(newModifier(Modifier.ModifierKeyword.TRANSIENT_KEYWORD)); 1816 } 1817 if (Modifier.isVolatile(flags)) { 1818 result.add(newModifier(Modifier.ModifierKeyword.VOLATILE_KEYWORD)); 1819 } 1820 return result; 1821 } 1822 1823 1825 1838 public BlockComment newBlockComment() { 1839 BlockComment result = new BlockComment(this); 1840 return result; 1841 } 1842 1843 1856 public LineComment newLineComment() { 1857 LineComment result = new LineComment(this); 1858 return result; 1859 } 1860 1861 1869 public Javadoc newJavadoc() { 1870 Javadoc result = new Javadoc(this); 1871 return result; 1872 } 1873 1874 1885 public TagElement newTagElement() { 1886 TagElement result = new TagElement(this); 1887 return result; 1888 } 1889 1890 1901 public TextElement newTextElement() { 1902 TextElement result = new TextElement(this); 1903 return result; 1904 } 1905 1906 1918 public MemberRef newMemberRef() { 1919 MemberRef result = new MemberRef(this); 1920 return result; 1921 } 1922 1923 1936 public MethodRef newMethodRef() { 1937 MethodRef result = new MethodRef(this); 1938 return result; 1939 } 1940 1941 1953 public MethodRefParameter newMethodRefParameter() { 1954 MethodRefParameter result = new MethodRefParameter(this); 1955 return result; 1956 } 1957 1958 1981 public VariableDeclarationStatement 1982 newVariableDeclarationStatement(VariableDeclarationFragment fragment) { 1983 if (fragment == null) { 1984 throw new IllegalArgumentException (); 1985 } 1986 VariableDeclarationStatement result = 1987 new VariableDeclarationStatement(this); 1988 result.fragments().add(fragment); 1989 return result; 1990 } 1991 1992 2010 public TypeDeclarationStatement 2011 newTypeDeclarationStatement(TypeDeclaration decl) { 2012 TypeDeclarationStatement result = new TypeDeclarationStatement(this); 2013 result.setDeclaration(decl); 2014 return result; 2015 } 2016 2017 2036 public TypeDeclarationStatement 2037 newTypeDeclarationStatement(AbstractTypeDeclaration decl) { 2038 TypeDeclarationStatement result = new TypeDeclarationStatement(this); 2039 if (this.apiLevel == AST.JLS2) { 2040 result.internalSetTypeDeclaration((TypeDeclaration) decl); 2041 } 2042 if (this.apiLevel >= AST.JLS3) { 2043 result.setDeclaration(decl); 2044 } 2045 return result; 2046 } 2047 2048 2054 public Block newBlock() { 2055 return new Block(this); 2056 } 2057 2058 2064 public ContinueStatement newContinueStatement() { 2065 return new ContinueStatement(this); 2066 } 2067 2068 2074 public BreakStatement newBreakStatement() { 2075 return new BreakStatement(this); 2076 } 2077 2078 2098 public ExpressionStatement newExpressionStatement(Expression expression) { 2099 ExpressionStatement result = new ExpressionStatement(this); 2100 result.setExpression(expression); 2101 return result; 2102 } 2103 2104 2111 public IfStatement newIfStatement() { 2112 return new IfStatement(this); 2113 } 2114 2115 2122 public WhileStatement newWhileStatement() { 2123 return new WhileStatement(this); 2124 } 2125 2126 2133 public DoStatement newDoStatement() { 2134 return new DoStatement(this); 2135 } 2136 2137 2144 public TryStatement newTryStatement() { 2145 return new TryStatement(this); 2146 } 2147 2148 2155 public CatchClause newCatchClause() { 2156 return new CatchClause(this); 2157 } 2158 2159 2165 public ReturnStatement newReturnStatement() { 2166 return new ReturnStatement(this); 2167 } 2168 2169 2175 public ThrowStatement newThrowStatement() { 2176 return new ThrowStatement(this); 2177 } 2178 2179 2186 public AssertStatement newAssertStatement() { 2187 return new AssertStatement(this); 2188 } 2189 2190 2195 public EmptyStatement newEmptyStatement() { 2196 return new EmptyStatement(this); 2197 } 2198 2199 2205 public LabeledStatement newLabeledStatement() { 2206 return new LabeledStatement(this); 2207 } 2208 2209 2216 public SwitchStatement newSwitchStatement() { 2217 return new SwitchStatement(this); 2218 } 2219 2220 2226 public SwitchCase newSwitchCase() { 2227 return new SwitchCase(this); 2228 } 2229 2230 2237 public SynchronizedStatement newSynchronizedStatement() { 2238 return new SynchronizedStatement(this); 2239 } 2240 2241 2248 public ForStatement newForStatement() { 2249 return new ForStatement(this); 2250 } 2251 2252 2262 public EnhancedForStatement newEnhancedForStatement() { 2263 return new EnhancedForStatement(this); 2264 } 2265 2266 2273 public StringLiteral newStringLiteral() { 2274 return new StringLiteral(this); 2275 } 2276 2277 2278 2284 public CharacterLiteral newCharacterLiteral() { 2285 return new CharacterLiteral(this); 2286 } 2287 2288 2296 public NumberLiteral newNumberLiteral(String literal) { 2297 if (literal == null) { 2298 throw new IllegalArgumentException (); 2299 } 2300 NumberLiteral result = new NumberLiteral(this); 2301 result.setToken(literal); 2302 return result; 2303 } 2304 2305 2311 public NumberLiteral newNumberLiteral() { 2312 NumberLiteral result = new NumberLiteral(this); 2313 return result; 2314 } 2315 2316 2321 public NullLiteral newNullLiteral() { 2322 return new NullLiteral(this); 2323 } 2324 2325 2342 public BooleanLiteral newBooleanLiteral(boolean value) { 2343 BooleanLiteral result = new BooleanLiteral(this); 2344 result.setBooleanValue(value); 2345 return result; 2346 } 2347 2348 2356 public Assignment newAssignment() { 2357 Assignment result = new Assignment(this); 2358 return result; 2359 } 2360 2361 2369 public MethodInvocation newMethodInvocation() { 2370 MethodInvocation result = new MethodInvocation(this); 2371 return result; 2372 } 2373 2374 2382 public SuperMethodInvocation newSuperMethodInvocation() { 2383 SuperMethodInvocation result = new SuperMethodInvocation(this); 2384 return result; 2385 } 2386 2387 2399 public ConstructorInvocation newConstructorInvocation() { 2400 ConstructorInvocation result = new ConstructorInvocation(this); 2401 return result; 2402 } 2403 2404 2416 public SuperConstructorInvocation newSuperConstructorInvocation() { 2417 SuperConstructorInvocation result = 2418 new SuperConstructorInvocation(this); 2419 return result; 2420 } 2421 2422 2445 public VariableDeclarationExpression 2446 newVariableDeclarationExpression(VariableDeclarationFragment fragment) { 2447 if (fragment == null) { 2448 throw new IllegalArgumentException (); 2449 } 2450 VariableDeclarationExpression result = 2451 new VariableDeclarationExpression(this); 2452 result.fragments().add(fragment); 2453 return result; 2454 } 2455 2456 2479 public FieldDeclaration newFieldDeclaration(VariableDeclarationFragment fragment) { 2480 if (fragment == null) { 2481 throw new IllegalArgumentException (); 2482 } 2483 FieldDeclaration result = new FieldDeclaration(this); 2484 result.fragments().add(fragment); 2485 return result; 2486 } 2487 2488 2494 public ThisExpression newThisExpression() { 2495 ThisExpression result = new ThisExpression(this); 2496 return result; 2497 } 2498 2499 2506 public FieldAccess newFieldAccess() { 2507 FieldAccess result = new FieldAccess(this); 2508 return result; 2509 } 2510 2511 2518 public SuperFieldAccess newSuperFieldAccess() { 2519 SuperFieldAccess result = new SuperFieldAccess(this); 2520 return result; 2521 } 2522 2523 2529 public TypeLiteral newTypeLiteral() { 2530 TypeLiteral result = new TypeLiteral(this); 2531 return result; 2532 } 2533 2534 2541 public CastExpression newCastExpression() { 2542 CastExpression result = new CastExpression(this); 2543 return result; 2544 } 2545 2546 2552 public ParenthesizedExpression newParenthesizedExpression() { 2553 ParenthesizedExpression result = new ParenthesizedExpression(this); 2554 return result; 2555 } 2556 2557 2565 public InfixExpression newInfixExpression() { 2566 InfixExpression result = new InfixExpression(this); 2567 return result; 2568 } 2569 2570 2577 public InstanceofExpression newInstanceofExpression() { 2578 InstanceofExpression result = new InstanceofExpression(this); 2579 return result; 2580 } 2581 2582 2589 public PostfixExpression newPostfixExpression() { 2590 PostfixExpression result = new PostfixExpression(this); 2591 return result; 2592 } 2593 2594 2601 public PrefixExpression newPrefixExpression() { 2602 PrefixExpression result = new PrefixExpression(this); 2603 return result; 2604 } 2605 2606 2613 public ArrayAccess newArrayAccess() { 2614 ArrayAccess result = new ArrayAccess(this); 2615 return result; 2616 } 2617 2618 2657 public ArrayCreation newArrayCreation() { 2658 ArrayCreation result = new ArrayCreation(this); 2659 return result; 2660 } 2661 2662 2671 public ClassInstanceCreation newClassInstanceCreation() { 2672 ClassInstanceCreation result = new ClassInstanceCreation(this); 2673 return result; 2674 } 2675 2676 2682 public AnonymousClassDeclaration newAnonymousClassDeclaration() { 2683 AnonymousClassDeclaration result = new AnonymousClassDeclaration(this); 2684 return result; 2685 } 2686 2687 2693 public ArrayInitializer newArrayInitializer() { 2694 ArrayInitializer result = new ArrayInitializer(this); 2695 return result; 2696 } 2697 2698 2705 public ConditionalExpression newConditionalExpression() { 2706 ConditionalExpression result = new ConditionalExpression(this); 2707 return result; 2708 } 2709 2710 2712 2722 public NormalAnnotation newNormalAnnotation() { 2723 NormalAnnotation result = new NormalAnnotation(this); 2724 return result; 2725 } 2726 2727 2736 public MarkerAnnotation newMarkerAnnotation() { 2737 MarkerAnnotation result = new MarkerAnnotation(this); 2738 return result; 2739 } 2740 2741 2750 public SingleMemberAnnotation newSingleMemberAnnotation() { 2751 SingleMemberAnnotation result = new SingleMemberAnnotation(this); 2752 return result; 2753 } 2754 2755 2764 public MemberValuePair newMemberValuePair() { 2765 MemberValuePair result = new MemberValuePair(this); 2766 return result; 2767 } 2768 2769 2787 void recordModifications(CompilationUnit root) { 2788 if(this.modificationCount != this.originalModificationCount) { 2789 throw new IllegalArgumentException ("AST is already modified"); } else if(this.rewriter != null) { 2791 throw new IllegalArgumentException ("AST modifications are already recorded"); } else if((root.getFlags() & ASTNode.PROTECT) != 0) { 2793 throw new IllegalArgumentException ("Root node is unmodifiable"); } else if(root.getAST() != this) { 2795 throw new IllegalArgumentException ("Root node is not owned by this ast"); } 2797 2798 this.rewriter = new InternalASTRewrite(root); 2799 this.setEventHandler(this.rewriter); 2800 } 2801 2802 2824 TextEdit rewrite(IDocument document, Map options) { 2825 if (document == null) { 2826 throw new IllegalArgumentException (); 2827 } 2828 if (this.rewriter == null) { 2829 throw new IllegalStateException ("Modifications record is not enabled"); } 2831 return this.rewriter.rewriteAST(document, options); 2832 } 2833 2834 2840 public boolean hasResolvedBindings() { 2841 return (this.bits & RESOLVED_BINDINGS) != 0; 2842 } 2843 2844 2850 public boolean hasStatementsRecovery() { 2851 return (this.bits & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0; 2852 } 2853 2854 2860 public boolean hasBindingsRecovery() { 2861 return (this.bits & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0; 2862 } 2863 2864 void setFlag(int newValue) { 2865 this.bits |= newValue; 2866 } 2867} 2868 2869 | Popular Tags |