1 11 12 package org.eclipse.jdt.core.dom; 13 14 import java.util.AbstractList ; 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 121 public abstract class ASTNode { 122 156 157 162 public static final int ANONYMOUS_CLASS_DECLARATION = 1; 163 164 169 public static final int ARRAY_ACCESS = 2; 170 171 176 public static final int ARRAY_CREATION = 3; 177 178 183 public static final int ARRAY_INITIALIZER = 4; 184 185 190 public static final int ARRAY_TYPE = 5; 191 192 197 public static final int ASSERT_STATEMENT = 6; 198 199 204 public static final int ASSIGNMENT = 7; 205 206 211 public static final int BLOCK = 8; 212 213 218 public static final int BOOLEAN_LITERAL = 9; 219 220 225 public static final int BREAK_STATEMENT = 10; 226 227 232 public static final int CAST_EXPRESSION = 11; 233 234 239 public static final int CATCH_CLAUSE = 12; 240 241 246 public static final int CHARACTER_LITERAL = 13; 247 248 253 public static final int CLASS_INSTANCE_CREATION = 14; 254 255 260 public static final int COMPILATION_UNIT = 15; 261 262 267 public static final int CONDITIONAL_EXPRESSION = 16; 268 269 274 public static final int CONSTRUCTOR_INVOCATION = 17; 275 276 281 public static final int CONTINUE_STATEMENT = 18; 282 283 288 public static final int DO_STATEMENT = 19; 289 290 295 public static final int EMPTY_STATEMENT = 20; 296 297 302 public static final int EXPRESSION_STATEMENT = 21; 303 304 309 public static final int FIELD_ACCESS = 22; 310 311 316 public static final int FIELD_DECLARATION = 23; 317 318 323 public static final int FOR_STATEMENT = 24; 324 325 330 public static final int IF_STATEMENT = 25; 331 332 337 public static final int IMPORT_DECLARATION = 26; 338 339 344 public static final int INFIX_EXPRESSION = 27; 345 346 351 public static final int INITIALIZER = 28; 352 353 358 public static final int JAVADOC = 29; 359 360 365 public static final int LABELED_STATEMENT = 30; 366 367 372 public static final int METHOD_DECLARATION = 31; 373 374 379 public static final int METHOD_INVOCATION = 32; 380 381 386 public static final int NULL_LITERAL = 33; 387 388 393 public static final int NUMBER_LITERAL = 34; 394 395 400 public static final int PACKAGE_DECLARATION = 35; 401 402 407 public static final int PARENTHESIZED_EXPRESSION = 36; 408 409 414 public static final int POSTFIX_EXPRESSION = 37; 415 416 421 public static final int PREFIX_EXPRESSION = 38; 422 423 428 public static final int PRIMITIVE_TYPE = 39; 429 430 435 public static final int QUALIFIED_NAME = 40; 436 437 442 public static final int RETURN_STATEMENT = 41; 443 444 449 public static final int SIMPLE_NAME = 42; 450 451 456 public static final int SIMPLE_TYPE = 43; 457 458 463 public static final int SINGLE_VARIABLE_DECLARATION = 44; 464 465 470 public static final int STRING_LITERAL = 45; 471 472 477 public static final int SUPER_CONSTRUCTOR_INVOCATION = 46; 478 479 484 public static final int SUPER_FIELD_ACCESS = 47; 485 486 491 public static final int SUPER_METHOD_INVOCATION = 48; 492 493 498 public static final int SWITCH_CASE = 49; 499 500 505 public static final int SWITCH_STATEMENT = 50; 506 507 512 public static final int SYNCHRONIZED_STATEMENT = 51; 513 514 519 public static final int THIS_EXPRESSION = 52; 520 521 526 public static final int THROW_STATEMENT = 53; 527 528 533 public static final int TRY_STATEMENT = 54; 534 535 540 public static final int TYPE_DECLARATION = 55; 541 542 547 public static final int TYPE_DECLARATION_STATEMENT = 56; 548 549 554 public static final int TYPE_LITERAL = 57; 555 556 561 public static final int VARIABLE_DECLARATION_EXPRESSION = 58; 562 563 568 public static final int VARIABLE_DECLARATION_FRAGMENT = 59; 569 570 575 public static final int VARIABLE_DECLARATION_STATEMENT = 60; 576 577 582 public static final int WHILE_STATEMENT = 61; 583 584 589 public static final int INSTANCEOF_EXPRESSION = 62; 590 591 597 public static final int LINE_COMMENT = 63; 598 599 605 public static final int BLOCK_COMMENT = 64; 606 607 613 public static final int TAG_ELEMENT = 65; 614 615 621 public static final int TEXT_ELEMENT = 66; 622 623 629 public static final int MEMBER_REF = 67; 630 631 637 public static final int METHOD_REF = 68; 638 639 645 public static final int METHOD_REF_PARAMETER = 69; 646 647 653 public static final int ENHANCED_FOR_STATEMENT = 70; 654 655 661 public static final int ENUM_DECLARATION = 71; 662 663 669 public static final int ENUM_CONSTANT_DECLARATION = 72; 670 671 677 public static final int TYPE_PARAMETER = 73; 678 679 685 public static final int PARAMETERIZED_TYPE = 74; 686 687 693 public static final int QUALIFIED_TYPE = 75; 694 695 701 public static final int WILDCARD_TYPE = 76; 702 703 709 public static final int NORMAL_ANNOTATION = 77; 710 711 717 public static final int MARKER_ANNOTATION = 78; 718 719 725 public static final int SINGLE_MEMBER_ANNOTATION = 79; 726 727 733 public static final int MEMBER_VALUE_PAIR = 80; 734 735 741 public static final int ANNOTATION_TYPE_DECLARATION = 81; 742 743 749 public static final int ANNOTATION_TYPE_MEMBER_DECLARATION = 82; 750 751 757 public static final int MODIFIER = 83; 758 759 769 public static Class nodeClassForType(int nodeType) { 770 switch (nodeType) { 771 case ANNOTATION_TYPE_DECLARATION : 772 return AnnotationTypeDeclaration.class; 773 case ANNOTATION_TYPE_MEMBER_DECLARATION : 774 return AnnotationTypeMemberDeclaration.class; 775 case ANONYMOUS_CLASS_DECLARATION : 776 return AnonymousClassDeclaration.class; 777 case ARRAY_ACCESS : 778 return ArrayAccess.class; 779 case ARRAY_CREATION : 780 return ArrayCreation.class; 781 case ARRAY_INITIALIZER : 782 return ArrayInitializer.class; 783 case ARRAY_TYPE : 784 return ArrayType.class; 785 case ASSERT_STATEMENT : 786 return AssertStatement.class; 787 case ASSIGNMENT : 788 return Assignment.class; 789 case BLOCK : 790 return Block.class; 791 case BLOCK_COMMENT : 792 return BlockComment.class; 793 case BOOLEAN_LITERAL : 794 return BooleanLiteral.class; 795 case BREAK_STATEMENT : 796 return BreakStatement.class; 797 case CAST_EXPRESSION : 798 return CastExpression.class; 799 case CATCH_CLAUSE : 800 return CatchClause.class; 801 case CHARACTER_LITERAL : 802 return CharacterLiteral.class; 803 case CLASS_INSTANCE_CREATION : 804 return ClassInstanceCreation.class; 805 case COMPILATION_UNIT : 806 return CompilationUnit.class; 807 case CONDITIONAL_EXPRESSION : 808 return ConditionalExpression.class; 809 case CONSTRUCTOR_INVOCATION : 810 return ConstructorInvocation.class; 811 case CONTINUE_STATEMENT : 812 return ContinueStatement.class; 813 case DO_STATEMENT : 814 return DoStatement.class; 815 case EMPTY_STATEMENT : 816 return EmptyStatement.class; 817 case ENHANCED_FOR_STATEMENT : 818 return EnhancedForStatement.class; 819 case ENUM_CONSTANT_DECLARATION : 820 return EnumConstantDeclaration.class; 821 case ENUM_DECLARATION : 822 return EnumDeclaration.class; 823 case EXPRESSION_STATEMENT : 824 return ExpressionStatement.class; 825 case FIELD_ACCESS : 826 return FieldAccess.class; 827 case FIELD_DECLARATION : 828 return FieldDeclaration.class; 829 case FOR_STATEMENT : 830 return ForStatement.class; 831 case IF_STATEMENT : 832 return IfStatement.class; 833 case IMPORT_DECLARATION : 834 return ImportDeclaration.class; 835 case INFIX_EXPRESSION : 836 return InfixExpression.class; 837 case INITIALIZER : 838 return Initializer.class; 839 case INSTANCEOF_EXPRESSION : 840 return InstanceofExpression.class; 841 case JAVADOC : 842 return Javadoc.class; 843 case LABELED_STATEMENT : 844 return LabeledStatement.class; 845 case LINE_COMMENT : 846 return LineComment.class; 847 case MARKER_ANNOTATION : 848 return MarkerAnnotation.class; 849 case MEMBER_REF : 850 return MemberRef.class; 851 case MEMBER_VALUE_PAIR : 852 return MemberValuePair.class; 853 case METHOD_DECLARATION : 854 return MethodDeclaration.class; 855 case METHOD_INVOCATION : 856 return MethodInvocation.class; 857 case METHOD_REF : 858 return MethodRef.class; 859 case METHOD_REF_PARAMETER : 860 return MethodRefParameter.class; 861 case MODIFIER : 862 return Modifier.class; 863 case NORMAL_ANNOTATION : 864 return NormalAnnotation.class; 865 case NULL_LITERAL : 866 return NullLiteral.class; 867 case NUMBER_LITERAL : 868 return NumberLiteral.class; 869 case PACKAGE_DECLARATION : 870 return PackageDeclaration.class; 871 case PARAMETERIZED_TYPE : 872 return ParameterizedType.class; 873 case PARENTHESIZED_EXPRESSION : 874 return ParenthesizedExpression.class; 875 case POSTFIX_EXPRESSION : 876 return PostfixExpression.class; 877 case PREFIX_EXPRESSION : 878 return PrefixExpression.class; 879 case PRIMITIVE_TYPE : 880 return PrimitiveType.class; 881 case QUALIFIED_NAME : 882 return QualifiedName.class; 883 case QUALIFIED_TYPE : 884 return QualifiedType.class; 885 case RETURN_STATEMENT : 886 return ReturnStatement.class; 887 case SIMPLE_NAME : 888 return SimpleName.class; 889 case SIMPLE_TYPE : 890 return SimpleType.class; 891 case SINGLE_MEMBER_ANNOTATION : 892 return SingleMemberAnnotation.class; 893 case SINGLE_VARIABLE_DECLARATION : 894 return SingleVariableDeclaration.class; 895 case STRING_LITERAL : 896 return StringLiteral.class; 897 case SUPER_CONSTRUCTOR_INVOCATION : 898 return SuperConstructorInvocation.class; 899 case SUPER_FIELD_ACCESS : 900 return SuperFieldAccess.class; 901 case SUPER_METHOD_INVOCATION : 902 return SuperMethodInvocation.class; 903 case SWITCH_CASE: 904 return SwitchCase.class; 905 case SWITCH_STATEMENT : 906 return SwitchStatement.class; 907 case SYNCHRONIZED_STATEMENT : 908 return SynchronizedStatement.class; 909 case TAG_ELEMENT : 910 return TagElement.class; 911 case TEXT_ELEMENT : 912 return TextElement.class; 913 case THIS_EXPRESSION : 914 return ThisExpression.class; 915 case THROW_STATEMENT : 916 return ThrowStatement.class; 917 case TRY_STATEMENT : 918 return TryStatement.class; 919 case TYPE_DECLARATION : 920 return TypeDeclaration.class; 921 case TYPE_DECLARATION_STATEMENT : 922 return TypeDeclarationStatement.class; 923 case TYPE_LITERAL : 924 return TypeLiteral.class; 925 case TYPE_PARAMETER : 926 return TypeParameter.class; 927 case VARIABLE_DECLARATION_EXPRESSION : 928 return VariableDeclarationExpression.class; 929 case VARIABLE_DECLARATION_FRAGMENT : 930 return VariableDeclarationFragment.class; 931 case VARIABLE_DECLARATION_STATEMENT : 932 return VariableDeclarationStatement.class; 933 case WHILE_STATEMENT : 934 return WhileStatement.class; 935 case WILDCARD_TYPE : 936 return WildcardType.class; 937 } 938 throw new IllegalArgumentException (); 939 } 940 941 948 final AST ast; 949 950 954 private ASTNode parent = null; 955 956 959 private static final Map UNMODIFIABLE_EMPTY_MAP 960 = Collections.unmodifiableMap(new HashMap (1)); 961 962 974 private Object property1 = null; 975 976 981 private Object property2 = null; 982 983 988 private int startPosition = -1; 989 990 994 private int length = 0; 995 996 1004 public static final int MALFORMED = 1; 1005 1006 1016 public static final int ORIGINAL = 2; 1017 1018 1035 public static final int PROTECT = 4; 1036 1037 1047 public static final int RECOVERED = 8; 1048 1049 1059 int typeAndFlags = 0; 1060 1061 1068 private StructuralPropertyDescriptor location = null; 1069 1070 1073 static final boolean CYCLE_RISK = true; 1074 1075 1078 static final boolean NO_CYCLE_RISK = false; 1079 1080 1083 static final boolean MANDATORY = true; 1084 1085 1088 static final boolean OPTIONAL = false; 1089 1090 1094 class NodeList extends AbstractList { 1095 1096 1108 ArrayList store = new ArrayList (0); 1109 1110 1113 ChildListPropertyDescriptor propertyDescriptor; 1114 1115 1120 class Cursor implements Iterator { 1121 1127 private int position = 0; 1128 1129 1132 public boolean hasNext() { 1133 return this.position < NodeList.this.store.size(); 1134 } 1135 1136 1139 public Object next() { 1140 Object result = NodeList.this.store.get(this.position); 1141 this.position++; 1142 return result; 1143 } 1144 1145 1148 public void remove() { 1149 throw new UnsupportedOperationException (); 1150 } 1151 1152 1160 void update(int index, int delta) { 1161 if (this.position > index) { 1162 this.position += delta; 1164 } 1165 } 1166 } 1167 1168 1181 private List cursors = null; 1182 1183 1191 NodeList(ChildListPropertyDescriptor property) { 1192 super(); 1193 this.propertyDescriptor = property; 1194 } 1195 1196 1199 public int size() { 1200 return this.store.size(); 1201 } 1202 1203 1206 public Object get(int index) { 1207 return this.store.get(index); 1208 } 1209 1210 1213 public Object set(int index, Object element) { 1214 if (element == null) { 1215 throw new IllegalArgumentException (); 1216 } 1217 if ((ASTNode.this.typeAndFlags & PROTECT) != 0) { 1218 throw new IllegalArgumentException ("AST node cannot be modified"); } 1221 ASTNode newChild = (ASTNode) element; 1223 ASTNode oldChild = (ASTNode) this.store.get(index); 1224 if (oldChild == newChild) { 1225 return oldChild; 1226 } 1227 if ((oldChild.typeAndFlags & PROTECT) != 0) { 1228 throw new IllegalArgumentException ("AST node cannot be modified"); } 1231 ASTNode.checkNewChild(ASTNode.this, newChild, this.propertyDescriptor.cycleRisk, this.propertyDescriptor.elementType); 1232 ASTNode.this.ast.preReplaceChildEvent(ASTNode.this, oldChild, newChild, this.propertyDescriptor); 1233 1234 Object result = this.store.set(index, newChild); 1235 oldChild.setParent(null, null); 1237 newChild.setParent(ASTNode.this, this.propertyDescriptor); 1238 ASTNode.this.ast.postReplaceChildEvent(ASTNode.this, oldChild, newChild, this.propertyDescriptor); 1239 return result; 1240 } 1241 1242 1245 public void add(int index, Object element) { 1246 if (element == null) { 1247 throw new IllegalArgumentException (); 1248 } 1249 if ((ASTNode.this.typeAndFlags & PROTECT) != 0) { 1250 throw new IllegalArgumentException ("AST node cannot be modified"); } 1253 ASTNode newChild = (ASTNode) element; 1255 ASTNode.checkNewChild(ASTNode.this, newChild, this.propertyDescriptor.cycleRisk, this.propertyDescriptor.elementType); 1256 ASTNode.this.ast.preAddChildEvent(ASTNode.this, newChild, this.propertyDescriptor); 1257 1258 1259 this.store.add(index, element); 1260 updateCursors(index, +1); 1261 newChild.setParent(ASTNode.this, this.propertyDescriptor); 1263 ASTNode.this.ast.postAddChildEvent(ASTNode.this, newChild, this.propertyDescriptor); 1264 } 1265 1266 1269 public Object remove(int index) { 1270 if ((ASTNode.this.typeAndFlags & PROTECT) != 0) { 1271 throw new IllegalArgumentException ("AST node cannot be modified"); } 1274 ASTNode oldChild = (ASTNode) this.store.get(index); 1276 if ((oldChild.typeAndFlags & PROTECT) != 0) { 1277 throw new IllegalArgumentException ("AST node cannot be modified"); } 1280 1281 ASTNode.this.ast.preRemoveChildEvent(ASTNode.this, oldChild, this.propertyDescriptor); 1282 oldChild.setParent(null, null); 1284 Object result = this.store.remove(index); 1285 updateCursors(index, -1); 1286 ASTNode.this.ast.postRemoveChildEvent(ASTNode.this, oldChild, this.propertyDescriptor); 1287 return result; 1288 1289 } 1290 1291 1302 Cursor newCursor() { 1303 synchronized (this) { 1304 if (this.cursors == null) { 1306 this.cursors = new ArrayList (1); 1308 } 1309 Cursor result = new Cursor(); 1310 this.cursors.add(result); 1311 return result; 1312 } 1313 } 1314 1315 1324 void releaseCursor(Cursor cursor) { 1325 synchronized (this) { 1326 this.cursors.remove(cursor); 1328 if (this.cursors.isEmpty()) { 1329 this.cursors = null; 1332 } 1333 } 1334 } 1335 1336 1348 private void updateCursors(int index, int delta) { 1349 if (this.cursors == null) { 1350 return; 1352 } 1353 for (Iterator it = this.cursors.iterator(); it.hasNext(); ) { 1354 Cursor c = (Cursor) it.next(); 1355 c.update(index, delta); 1356 } 1357 } 1358 1359 1374 int memSize() { 1375 int result = HEADERS + 5 * 4; 1376 result += HEADERS + 2 * 4; 1377 result += HEADERS + 4 * size(); 1378 return result; 1379 } 1380 1381 1387 int listSize() { 1388 int result = memSize(); 1389 for (Iterator it = iterator(); it.hasNext(); ) { 1390 ASTNode child = (ASTNode) it.next(); 1391 result += child.treeSize(); 1392 } 1393 return result; 1394 } 1395 } 1396 1397 1410 ASTNode(AST ast) { 1411 if (ast == null) { 1412 throw new IllegalArgumentException (); 1413 } 1414 1415 this.ast = ast; 1416 setNodeType(getNodeType0()); 1417 setFlags(ast.getDefaultNodeFlag()); 1418 } 1420 1421 1430 public final AST getAST() { 1431 return this.ast; 1432 } 1433 1434 1444 public final ASTNode getParent() { 1445 return this.parent; 1446 } 1447 1448 1472 public final StructuralPropertyDescriptor getLocationInParent() { 1473 return this.location; 1474 } 1475 1476 1482 public final ASTNode getRoot() { 1483 ASTNode candidate = this; 1484 while (true) { 1485 ASTNode p = candidate.getParent(); 1486 if (p == null) { 1487 return candidate; 1489 } 1490 candidate = p; 1491 } 1492 } 1493 1494 1510 public final Object getStructuralProperty(StructuralPropertyDescriptor property) { 1511 if (property instanceof SimplePropertyDescriptor) { 1512 SimplePropertyDescriptor p = (SimplePropertyDescriptor) property; 1513 if (p.getValueType() == int.class) { 1514 int result = internalGetSetIntProperty(p, true, 0); 1515 return new Integer (result); 1516 } else if (p.getValueType() == boolean.class) { 1517 boolean result = internalGetSetBooleanProperty(p, true, false); 1518 return Boolean.valueOf(result); 1519 } else { 1520 return internalGetSetObjectProperty(p, true, null); 1521 } 1522 } 1523 if (property instanceof ChildPropertyDescriptor) { 1524 return internalGetSetChildProperty((ChildPropertyDescriptor) property, true, null); 1525 } 1526 if (property instanceof ChildListPropertyDescriptor) { 1527 return internalGetChildListProperty((ChildListPropertyDescriptor) property); 1528 } 1529 throw new IllegalArgumentException (); 1530 } 1531 1532 1549 public final void setStructuralProperty(StructuralPropertyDescriptor property, Object value) { 1550 if (property instanceof SimplePropertyDescriptor) { 1551 SimplePropertyDescriptor p = (SimplePropertyDescriptor) property; 1552 if (p.getValueType() == int.class) { 1553 int arg = ((Integer ) value).intValue(); 1554 internalGetSetIntProperty(p, false, arg); 1555 return; 1556 } else if (p.getValueType() == boolean.class) { 1557 boolean arg = ((Boolean ) value).booleanValue(); 1558 internalGetSetBooleanProperty(p, false, arg); 1559 return; 1560 } else { 1561 if (value == null && p.isMandatory()) { 1562 throw new IllegalArgumentException (); 1563 } 1564 internalGetSetObjectProperty(p, false, value); 1565 return; 1566 } 1567 } 1568 if (property instanceof ChildPropertyDescriptor) { 1569 ChildPropertyDescriptor p = (ChildPropertyDescriptor) property; 1570 ASTNode child = (ASTNode) value; 1571 if (child == null && p.isMandatory()) { 1572 throw new IllegalArgumentException (); 1573 } 1574 internalGetSetChildProperty(p, false, child); 1575 return; 1576 } 1577 if (property instanceof ChildListPropertyDescriptor) { 1578 throw new IllegalArgumentException ("Cannot set the list of child list property"); } 1580 } 1581 1582 1599 int internalGetSetIntProperty(SimplePropertyDescriptor property, boolean get, int value) { 1600 throw new RuntimeException ("Node does not have this property"); } 1602 1603 1620 boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) { 1621 throw new RuntimeException ("Node does not have this property"); } 1623 1624 1642 Object internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object value) { 1643 throw new RuntimeException ("Node does not have this property"); } 1645 1646 1663 ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { 1664 throw new RuntimeException ("Node does not have this property"); } 1666 1667 1679 List internalGetChildListProperty(ChildListPropertyDescriptor property) { 1680 throw new RuntimeException ("Node does not have this property"); } 1682 1683 1696 public final List structuralPropertiesForType() { 1697 return internalStructuralPropertiesForType(this.ast.apiLevel); 1698 } 1699 1700 1715 abstract List internalStructuralPropertiesForType(int apiLevel); 1716 1717 1724 static void createPropertyList(Class nodeClass, List propertyList) { 1725 propertyList.add(nodeClass); 1727 } 1728 1729 1736 static void addProperty(StructuralPropertyDescriptor property, List propertyList) { 1737 Class nodeClass = (Class ) propertyList.get(0); 1738 if (property.getNodeClass() != nodeClass) { 1739 throw new RuntimeException ("Structural property descriptor has wrong node class!"); } 1742 propertyList.add(property); 1743 } 1744 1745 1754 static List reapPropertyList(List propertyList) { 1755 propertyList.remove(0); ArrayList a = new ArrayList (propertyList.size()); 1758 a.addAll(propertyList); 1759 return Collections.unmodifiableList(a); 1760 } 1761 1762 1769 final void unsupportedIn2() { 1770 if (this.ast.apiLevel == AST.JLS2_INTERNAL) { 1771 throw new UnsupportedOperationException ("Operation not supported in JLS2 AST"); } 1773 } 1774 1775 1782 final void supportedOnlyIn2() { 1783 if (this.ast.apiLevel != AST.JLS2_INTERNAL) { 1784 throw new UnsupportedOperationException ("Operation only supported in JLS2 AST"); } 1786 } 1787 1788 1804 final void setParent(ASTNode parent, StructuralPropertyDescriptor property) { 1805 this.ast.modifying(); 1806 this.parent = parent; 1807 this.location = property; 1808 } 1809 1810 1822 public final void delete() { 1823 StructuralPropertyDescriptor p = getLocationInParent(); 1824 if (p == null) { 1825 return; 1827 } 1828 if (p.isChildProperty()) { 1829 getParent().setStructuralProperty(this.location, null); 1830 return; 1831 } 1832 if (p.isChildListProperty()) { 1833 List l = (List ) getParent().getStructuralProperty(this.location); 1834 l.remove(this); 1835 } 1836 } 1837 1838 1861 static void checkNewChild(ASTNode node, ASTNode newChild, 1862 boolean cycleCheck, Class nodeType) { 1863 if (newChild.ast != node.ast) { 1864 throw new IllegalArgumentException (); 1866 } 1867 if (newChild.getParent() != null) { 1868 throw new IllegalArgumentException (); 1870 } 1871 if (cycleCheck && newChild == node.getRoot()) { 1872 throw new IllegalArgumentException (); 1874 } 1875 Class childClass = newChild.getClass(); 1876 if (nodeType != null && !nodeType.isAssignableFrom(childClass)) { 1877 throw new ClassCastException (); 1879 } 1880 if ((newChild.typeAndFlags & PROTECT) != 0) { 1881 throw new IllegalArgumentException ("AST node cannot be modified"); } 1884 } 1885 1886 1925 final void preReplaceChild(ASTNode oldChild, ASTNode newChild, ChildPropertyDescriptor property) { 1926 if ((this.typeAndFlags & PROTECT) != 0) { 1927 throw new IllegalArgumentException ("AST node cannot be modified"); } 1930 if (newChild != null) { 1931 checkNewChild(this, newChild, property.cycleRisk, null); 1932 } 1933 if (oldChild != null) { 1935 if ((oldChild.typeAndFlags & PROTECT) != 0) { 1936 throw new IllegalArgumentException ("AST node cannot be modified"); } 1939 if (newChild != null) { 1940 this.ast.preReplaceChildEvent(this, oldChild, newChild, property); 1941 } else { 1942 this.ast.preRemoveChildEvent(this, oldChild, property); 1943 } 1944 oldChild.setParent(null, null); 1945 } else { 1946 if(newChild != null) { 1947 this.ast.preAddChildEvent(this, newChild, property); 1948 } 1949 } 1950 if (newChild != null) { 1952 newChild.setParent(this, property); 1953 } 1955 } 1956 1957 1964 final void postReplaceChild(ASTNode oldChild, ASTNode newChild, ChildPropertyDescriptor property) { 1965 if (newChild != null) { 1967 if (oldChild != null) { 1968 this.ast.postReplaceChildEvent(this, oldChild, newChild, property); 1969 } else { 1970 this.ast.postAddChildEvent(this, newChild, property); 1971 } 1972 } else { 1973 this.ast.postRemoveChildEvent(this, oldChild, property); 1974 } 1975 } 1976 1977 2003 final void preValueChange(SimplePropertyDescriptor property) { 2004 if ((this.typeAndFlags & PROTECT) != 0) { 2005 throw new IllegalArgumentException ("AST node cannot be modified"); } 2008 this.ast.preValueChangeEvent(this, property); 2009 this.ast.modifying(); 2010 } 2011 2012 2018 final void postValueChange(SimplePropertyDescriptor property) { 2019 this.ast.postValueChangeEvent(this, property); 2020 } 2021 2022 2027 final void checkModifiable() { 2028 if ((this.typeAndFlags & PROTECT) != 0) { 2029 throw new IllegalArgumentException ("AST node cannot be modified"); } 2031 this.ast.modifying(); 2032 } 2033 2034 2052 final void preLazyInit() { 2053 this.ast.disableEvents(); 2056 } 2058 2059 2068 final void postLazyInit(ASTNode newChild, ChildPropertyDescriptor property) { 2069 newChild.setParent(this, property); 2073 this.ast.reenableEvents(); 2075 } 2076 2077 2084 public final Object getProperty(String propertyName) { 2085 if (propertyName == null) { 2086 throw new IllegalArgumentException (); 2087 } 2088 if (this.property1 == null) { 2089 return null; 2091 } 2092 if (this.property1 instanceof String ) { 2093 if (propertyName.equals(this.property1)) { 2095 return this.property2; 2096 } else { 2097 return null; 2098 } 2099 } 2100 Map m = (Map ) this.property1; 2102 return m.get(propertyName); 2103 } 2104 2105 2124 public final void setProperty(String propertyName, Object data) { 2125 if (propertyName == null) { 2126 throw new IllegalArgumentException (); 2127 } 2128 2130 if (this.property1 == null) { 2131 if (data == null) { 2133 return; 2135 } 2136 this.property1 = propertyName; 2138 this.property2 = data; 2139 return; 2140 } 2141 2142 if (this.property1 instanceof String ) { 2143 if (propertyName.equals(this.property1)) { 2145 this.property2 = data; 2147 if (data == null) { 2148 this.property1 = null; 2150 this.property2 = null; 2151 } 2152 return; 2153 } 2154 if (data == null) { 2155 return; 2157 } 2158 HashMap m = new HashMap (2); 2161 m.put(this.property1, this.property2); 2162 m.put(propertyName, data); 2163 this.property1 = m; 2164 this.property2 = null; 2165 return; 2166 } 2167 2168 HashMap m = (HashMap ) this.property1; 2170 if (data == null) { 2171 m.remove(propertyName); 2172 if (m.size() == 1) { 2174 Map.Entry [] entries = (Map.Entry []) m.entrySet().toArray(new Map.Entry [1]); 2176 this.property1 = entries[0].getKey(); 2177 this.property2 = entries[0].getValue(); 2178 } 2179 return; 2180 } else { 2181 m.put(propertyName, data); 2182 return; 2184 } 2185 } 2186 2187 2194 public final Map properties() { 2195 if (this.property1 == null) { 2196 return UNMODIFIABLE_EMPTY_MAP; 2198 } 2199 if (this.property1 instanceof String ) { 2200 return Collections.singletonMap(this.property1, this.property2); 2202 } 2203 2204 if (this.property2 == null) { 2206 this.property2 = Collections.unmodifiableMap((Map ) this.property1); 2207 } 2208 return (Map ) this.property2; 2210 } 2211 2212 2236 public final int getFlags() { 2237 return this.typeAndFlags & 0xFFFF; 2238 } 2239 2240 2266 public final void setFlags(int flags) { 2267 this.ast.modifying(); 2268 int old = this.typeAndFlags & 0xFFFF0000; 2269 this.typeAndFlags = old | (flags & 0xFFFF); 2270 } 2271 2272 2283 public final int getNodeType() { 2284 return this.typeAndFlags >>> 16; 2285 } 2286 2287 2293 private void setNodeType(int nodeType) { 2294 int old = this.typeAndFlags & 0xFFFF0000; 2295 this.typeAndFlags = old | (nodeType << 16); 2296 } 2297 2298 2307 abstract int getNodeType0(); 2308 2309 2318 public final boolean equals(Object obj) { 2319 return this == obj; } 2321 2322 2327 public final int hashCode() { 2328 return super.hashCode(); 2329 } 2330 2331 2340 public final boolean subtreeMatch(ASTMatcher matcher, Object other) { 2341 return subtreeMatch0(matcher, other); 2342 } 2343 2344 2357 abstract boolean subtreeMatch0(ASTMatcher matcher, Object other); 2358 2359 2378 public static ASTNode copySubtree(AST target, ASTNode node) { 2379 if (node == null) { 2380 return null; 2381 } 2382 if (target == null) { 2383 throw new IllegalArgumentException (); 2384 } 2385 if (target.apiLevel() != node.getAST().apiLevel()) { 2386 throw new UnsupportedOperationException (); 2387 } 2388 ASTNode newNode = node.clone(target); 2389 return newNode; 2390 } 2391 2392 2409 public static List copySubtrees(AST target, List nodes) { 2410 List result = new ArrayList (nodes.size()); 2411 for (Iterator it = nodes.iterator(); it.hasNext(); ) { 2412 ASTNode oldNode = (ASTNode) it.next(); 2413 ASTNode newNode = oldNode.clone(target); 2414 result.add(newNode); 2415 } 2416 return result; 2417 } 2418 2419 2432 final ASTNode clone(AST target) { 2433 this.ast.preCloneNodeEvent(this); 2434 ASTNode c = this.clone0(target); 2435 this.ast.postCloneNodeEvent(this, c); 2436 return c; 2437 } 2438 2439 2461 abstract ASTNode clone0(AST target); 2462 2463 2469 public final void accept(ASTVisitor visitor) { 2470 if (visitor == null) { 2471 throw new IllegalArgumentException (); 2472 } 2473 visitor.preVisit(this); 2475 accept0(visitor); 2477 visitor.postVisit(this); 2479 } 2480 2481 2504 abstract void accept0(ASTVisitor visitor); 2505 2506 2519 final void acceptChild(ASTVisitor visitor, ASTNode child) { 2520 if (child == null) { 2521 return; 2522 } 2523 child.accept(visitor); 2524 } 2525 2526 2539 final void acceptChildren(ASTVisitor visitor, ASTNode.NodeList children) { 2540 NodeList.Cursor cursor = children.newCursor(); 2543 try { 2544 while (cursor.hasNext()) { 2545 ASTNode child = (ASTNode) cursor.next(); 2546 child.accept(visitor); 2547 } 2548 } finally { 2549 children.releaseCursor(cursor); 2550 } 2551 } 2552 2553 2567 public final int getStartPosition() { 2568 return this.startPosition; 2569 } 2570 2571 2585 public final int getLength() { 2586 return this.length; 2587 } 2588 2589 2607 public final void setSourceRange(int startPosition, int length) { 2608 if (startPosition >= 0 && length < 0) { 2609 throw new IllegalArgumentException (); 2610 } 2611 if (startPosition < 0 && length != 0) { 2612 throw new IllegalArgumentException (); 2613 } 2614 checkModifiable(); 2617 this.startPosition = startPosition; 2618 this.length = length; 2619 } 2620 2621 2627 public final String toString() { 2628 StringBuffer buffer = new StringBuffer (); 2629 int p = buffer.length(); 2630 try { 2631 appendDebugString(buffer); 2632 } catch (RuntimeException e) { 2633 buffer.setLength(p); 2636 buffer.append("!"); buffer.append(standardToString()); 2638 } 2639 return buffer.toString(); 2640 } 2641 2642 2648 final String standardToString() { 2649 return super.toString(); 2650 } 2651 2652 2661 void appendDebugString(StringBuffer buffer) { 2662 appendPrintString(buffer); 2664 } 2665 2666 2672 final void appendPrintString(StringBuffer buffer) { 2673 NaiveASTFlattener printer = new NaiveASTFlattener(); 2674 this.accept(printer); 2675 buffer.append(printer.getResult()); 2676 } 2677 2678 2681 static final int HEADERS = 12; 2682 2683 2688 static final int BASE_NODE_SIZE = HEADERS + 7 * 4; 2689 2690 2699 static int stringSize(String string) { 2700 int size = 0; 2701 if (string != null) { 2702 size += HEADERS + 4 * 4; 2704 size += HEADERS + 2 * string.length(); 2706 } 2707 return size; 2708 } 2709 2710 2716 public final int subtreeBytes() { 2717 return treeSize(); 2718 } 2719 2720 2731 abstract int treeSize(); 2732 2733 2739 abstract int memSize(); 2740} 2741 | Popular Tags |