1 11 12 package org.eclipse.jdt.core.dom; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 28 public class InfixExpression extends Expression { 29 30 55 public static class Operator { 56 57 60 private String token; 61 62 71 private Operator(String token) { 72 this.token = token; 73 } 74 75 80 public String toString() { 81 return token; 82 } 83 84 85 public static final Operator TIMES = new Operator("*"); 87 public static final Operator DIVIDE = new Operator("/"); 89 public static final Operator REMAINDER = new Operator("%"); 91 public static final Operator PLUS = new Operator("+"); 93 public static final Operator MINUS = new Operator("-"); 95 public static final Operator LEFT_SHIFT = new Operator("<<"); 97 public static final Operator RIGHT_SHIFT_SIGNED = new Operator(">>"); 99 public static final Operator RIGHT_SHIFT_UNSIGNED = 100 new Operator(">>>"); 102 public static final Operator LESS = new Operator("<"); 104 public static final Operator GREATER = new Operator(">"); 106 public static final Operator LESS_EQUALS = new Operator("<="); 108 public static final Operator GREATER_EQUALS = new Operator(">="); 110 public static final Operator EQUALS = new Operator("=="); 112 public static final Operator NOT_EQUALS = new Operator("!="); 114 public static final Operator XOR = new Operator("^"); 116 public static final Operator OR = new Operator("|"); 118 public static final Operator AND = new Operator("&"); 120 public static final Operator CONDITIONAL_OR = new Operator("||"); 122 public static final Operator CONDITIONAL_AND = new Operator("&&"); 124 128 private static final Map CODES; 129 static { 130 CODES = new HashMap (20); 131 Operator[] ops = { 132 TIMES, 133 DIVIDE, 134 REMAINDER, 135 PLUS, 136 MINUS, 137 LEFT_SHIFT, 138 RIGHT_SHIFT_SIGNED, 139 RIGHT_SHIFT_UNSIGNED, 140 LESS, 141 GREATER, 142 LESS_EQUALS, 143 GREATER_EQUALS, 144 EQUALS, 145 NOT_EQUALS, 146 XOR, 147 OR, 148 AND, 149 CONDITIONAL_OR, 150 CONDITIONAL_AND, 151 }; 152 for (int i = 0; i < ops.length; i++) { 153 CODES.put(ops[i].toString(), ops[i]); 154 } 155 } 156 157 169 public static Operator toOperator(String token) { 170 return (Operator) CODES.get(token); 171 } 172 173 } 174 175 179 public static final ChildPropertyDescriptor LEFT_OPERAND_PROPERTY = 180 new ChildPropertyDescriptor(InfixExpression.class, "leftOperand", Expression.class, MANDATORY, CYCLE_RISK); 182 186 public static final SimplePropertyDescriptor OPERATOR_PROPERTY = 187 new SimplePropertyDescriptor(InfixExpression.class, "operator", InfixExpression.Operator.class, MANDATORY); 189 193 public static final ChildPropertyDescriptor RIGHT_OPERAND_PROPERTY = 194 new ChildPropertyDescriptor(InfixExpression.class, "rightOperand", Expression.class, MANDATORY, CYCLE_RISK); 196 200 public static final ChildListPropertyDescriptor EXTENDED_OPERANDS_PROPERTY = 201 new ChildListPropertyDescriptor(InfixExpression.class, "extendedOperands", Expression.class, CYCLE_RISK); 203 208 private static final List PROPERTY_DESCRIPTORS; 209 210 static { 211 List properyList = new ArrayList (5); 212 createPropertyList(InfixExpression.class, properyList); 213 addProperty(LEFT_OPERAND_PROPERTY, properyList); 214 addProperty(OPERATOR_PROPERTY, properyList); 215 addProperty(RIGHT_OPERAND_PROPERTY, properyList); 216 addProperty(EXTENDED_OPERANDS_PROPERTY, properyList); 217 PROPERTY_DESCRIPTORS = reapPropertyList(properyList); 218 } 219 220 231 public static List propertyDescriptors(int apiLevel) { 232 return PROPERTY_DESCRIPTORS; 233 } 234 235 238 private InfixExpression.Operator operator = InfixExpression.Operator.PLUS; 239 240 244 private Expression leftOperand = null; 245 246 250 private Expression rightOperand = null; 251 252 256 private ASTNode.NodeList extendedOperands = null; 257 258 265 InfixExpression(AST ast) { 266 super(ast); 267 } 268 269 272 final List internalStructuralPropertiesForType(int apiLevel) { 273 return propertyDescriptors(apiLevel); 274 } 275 276 279 final Object internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object value) { 280 if (property == OPERATOR_PROPERTY) { 281 if (get) { 282 return getOperator(); 283 } else { 284 setOperator((Operator) value); 285 return null; 286 } 287 } 288 return super.internalGetSetObjectProperty(property, get, value); 290 } 291 292 295 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { 296 if (property == LEFT_OPERAND_PROPERTY) { 297 if (get) { 298 return getLeftOperand(); 299 } else { 300 setLeftOperand((Expression) child); 301 return null; 302 } 303 } 304 if (property == RIGHT_OPERAND_PROPERTY) { 305 if (get) { 306 return getRightOperand(); 307 } else { 308 setRightOperand((Expression) child); 309 return null; 310 } 311 } 312 return super.internalGetSetChildProperty(property, get, child); 314 } 315 316 319 final List internalGetChildListProperty(ChildListPropertyDescriptor property) { 320 if (property == EXTENDED_OPERANDS_PROPERTY) { 321 return extendedOperands(); 322 } 323 return super.internalGetChildListProperty(property); 325 } 326 327 330 final int getNodeType0() { 331 return INFIX_EXPRESSION; 332 } 333 334 337 ASTNode clone0(AST target) { 338 InfixExpression result = new InfixExpression(target); 339 result.setSourceRange(this.getStartPosition(), this.getLength()); 340 result.setOperator(getOperator()); 341 result.setLeftOperand((Expression) getLeftOperand().clone(target)); 342 result.setRightOperand((Expression) getRightOperand().clone(target)); 343 if (this.extendedOperands != null) { 344 result.extendedOperands().addAll( 346 ASTNode.copySubtrees(target, this.extendedOperands())); 347 } 348 return result; 349 } 350 351 354 final boolean subtreeMatch0(ASTMatcher matcher, Object other) { 355 return matcher.match(this, other); 357 } 358 359 362 void accept0(ASTVisitor visitor) { 363 boolean visitChildren = visitor.visit(this); 364 if (visitChildren) { 365 acceptChild(visitor, getLeftOperand()); 367 acceptChild(visitor, getRightOperand()); 368 if (this.extendedOperands != null) { 369 acceptChildren(visitor, this.extendedOperands); 371 } 372 } 373 visitor.endVisit(this); 374 } 375 376 381 public InfixExpression.Operator getOperator() { 382 return this.operator; 383 } 384 385 391 public void setOperator(InfixExpression.Operator operator) { 392 if (operator == null) { 393 throw new IllegalArgumentException (); 394 } 395 preValueChange(OPERATOR_PROPERTY); 396 this.operator = operator; 397 postValueChange(OPERATOR_PROPERTY); 398 } 399 400 405 public Expression getLeftOperand() { 406 if (this.leftOperand == null) { 407 synchronized (this) { 409 if (this.leftOperand == null) { 410 preLazyInit(); 411 this.leftOperand= new SimpleName(this.ast); 412 postLazyInit(this.leftOperand, LEFT_OPERAND_PROPERTY); 413 } 414 } 415 } 416 return this.leftOperand; 417 } 418 419 430 public void setLeftOperand(Expression expression) { 431 if (expression == null) { 432 throw new IllegalArgumentException (); 433 } 434 ASTNode oldChild = this.leftOperand; 435 preReplaceChild(oldChild, expression, LEFT_OPERAND_PROPERTY); 436 this.leftOperand = expression; 437 postReplaceChild(oldChild, expression, LEFT_OPERAND_PROPERTY); 438 } 439 440 445 public Expression getRightOperand() { 446 if (this.rightOperand == null) { 447 synchronized (this) { 449 if (this.rightOperand == null) { 450 preLazyInit(); 451 this.rightOperand= new SimpleName(this.ast); 452 postLazyInit(this.rightOperand, RIGHT_OPERAND_PROPERTY); 453 } 454 } 455 } 456 return this.rightOperand; 457 } 458 459 470 public void setRightOperand(Expression expression) { 471 if (expression == null) { 472 throw new IllegalArgumentException (); 473 } 474 ASTNode oldChild = this.rightOperand; 475 preReplaceChild(oldChild, expression, RIGHT_OPERAND_PROPERTY); 476 this.rightOperand = expression; 477 postReplaceChild(oldChild, expression, RIGHT_OPERAND_PROPERTY); 478 } 479 480 486 public boolean hasExtendedOperands() { 487 return 488 (this.extendedOperands != null) && this.extendedOperands.size() > 0; 489 } 490 491 511 public List extendedOperands() { 512 if (this.extendedOperands == null) { 513 this.extendedOperands = new ASTNode.NodeList(EXTENDED_OPERANDS_PROPERTY); 515 } 516 return this.extendedOperands; 517 } 518 519 522 int memSize() { 523 return BASE_NODE_SIZE + 4 * 4; 525 } 526 527 530 int treeSize() { 531 return 532 memSize() 533 + (this.leftOperand == null ? 0 : getLeftOperand().treeSize()) 534 + (this.rightOperand == null ? 0 : getRightOperand().treeSize()) 535 + (this.extendedOperands == null ? 0 : extendedOperands.listSize()); 536 } 537 } 538 | Popular Tags |