1 33 34 package edu.rice.cs.drjava.model.repl; 35 36 import java.lang.reflect.*; 37 import koala.dynamicjava.interpreter.*; 38 import koala.dynamicjava.interpreter.context.*; 39 import koala.dynamicjava.interpreter.error.*; 40 import koala.dynamicjava.tree.*; 41 42 57 58 public class EvaluationVisitorExtension extends EvaluationVisitor { 59 private Context _context; 60 public EvaluationVisitorExtension(Context ctx) { 61 super(ctx); 62 _context = ctx; 63 } 64 65 private void _checkInterrupted(Node node) { 66 if (Thread.currentThread().interrupted()) { 70 throw new InterpreterInterruptedException(node.getBeginLine(), node.getBeginColumn(), node.getEndLine(), 71 node.getEndColumn()); 72 } 73 } 74 75 76 77 public Object visit(WhileStatement node) { 78 _checkInterrupted(node); 79 super.visit(node); 80 return Interpreter.NO_RESULT; 81 } 82 83 public Object visit(ForStatement node) { 84 _checkInterrupted(node); 85 super.visit(node); 86 return Interpreter.NO_RESULT; 87 } 88 89 public Object visit(ForEachStatement node) { 90 _checkInterrupted(node); 91 super.visit(node); 92 return Interpreter.NO_RESULT; 93 } 94 95 public Object visit(DoStatement node) { 96 _checkInterrupted(node); 97 super.visit(node); 98 return Interpreter.NO_RESULT; 99 } 100 101 public Object visit(SwitchStatement node) { 102 _checkInterrupted(node); 103 super.visit(node); 104 return Interpreter.NO_RESULT; 105 } 106 107 public Object visit(LabeledStatement node) { 108 _checkInterrupted(node); 109 super.visit(node); 110 return Interpreter.NO_RESULT; 111 } 112 113 public Object visit(SynchronizedStatement node) { 114 _checkInterrupted(node); 115 super.visit(node); 116 return Interpreter.NO_RESULT; 117 } 118 119 public Object visit(TryStatement node) { 120 _checkInterrupted(node); 121 super.visit(node); 122 return Interpreter.NO_RESULT; 123 } 124 125 public Object visit(IfThenStatement node) { 126 _checkInterrupted(node); 127 super.visit(node); 128 return Interpreter.NO_RESULT; 129 } 130 131 public Object visit(IfThenElseStatement node) { 132 _checkInterrupted(node); 133 super.visit(node); 134 return Interpreter.NO_RESULT; 135 } 136 137 public Object visit(AssertStatement node) { 138 _checkInterrupted(node); 139 super.visit(node); 140 return Interpreter.NO_RESULT; 141 } 142 143 public Object visit(BlockStatement node) { 144 _checkInterrupted(node); 145 super.visit(node); 146 return Interpreter.NO_RESULT; 147 } 148 149 public Object visit(Literal node) { 150 _checkInterrupted(node); 151 return super.visit(node); 152 } 153 154 157 public Object visit(VariableDeclaration node) { 158 _checkInterrupted(node); 159 Class <?> c = (Class <?>) NodeProperties.getType(node.getType()); 160 161 if (node.getInitializer() != null) { 162 Object o = performCast(c, node.getInitializer().acceptVisitor(this)); 163 164 String name = node.getName(); 166 167 if (!(c.isPrimitive() || o == null || c.isAssignableFrom(o.getClass()))) { 168 Exception e = new ClassCastException (name); 169 throw new CatchedExceptionError(e, node); 170 } 171 172 if (node.isFinal()) _context.setConstant(node.getName(), o); 173 else _context.set(node.getName(), o); 174 } 175 else if (node.isFinal()) _context.setConstant(node.getName(), UninitializedObject.INSTANCE); 176 else { 177 Object value = null; 182 if (!c.isPrimitive()) value = null; 183 else if (c == byte.class) value = new Byte ((byte) 0); 184 else if (c == short.class) value = new Short ((short) 0); 185 else if (c == int.class) value = new Integer (0); 186 else if (c == long.class) value = new Long (0L); 187 else if (c == float.class) value = new Float (0.0f); 188 else if (c == double.class) value = new Double (0.0d); 189 else if (c == char.class) value = new Character ('\u0000'); 190 else if (c == boolean.class) value = Boolean.valueOf(false); 191 _context.set(node.getName(), value); 192 } 193 return Interpreter.NO_RESULT; 194 } 195 196 public Object visit(ObjectFieldAccess node) { 197 _checkInterrupted(node); 198 return super.visit(node); 199 } 200 201 public Object visit(ObjectMethodCall node) { 202 _checkInterrupted(node); 203 Method m = (Method) node.getProperty(NodeProperties.METHOD); 204 Object ret = super.visit(node); 206 207 if (m != null && m.getReturnType().equals(Void.TYPE)) return Interpreter.NO_RESULT; 210 return ret; 211 } 212 213 public Object visit(StaticFieldAccess node) { 214 _checkInterrupted(node); 215 return super.visit(node); 216 } 217 218 public Object visit(SuperFieldAccess node) { 219 _checkInterrupted(node); 220 return super.visit(node); 221 } 222 223 public Object visit(SuperMethodCall node) { 224 _checkInterrupted(node); 225 return super.visit(node); 226 } 227 228 public Object visit(StaticMethodCall node) { 229 _checkInterrupted(node); 230 Method m = (Method) node.getProperty(NodeProperties.METHOD); 231 232 if (! Modifier.isStatic(m.getModifiers())) { 234 final StringBuilder buf = new StringBuilder (); 235 buf.append(m.getDeclaringClass()); 236 buf.append("."); 237 buf.append(m.getName()); 238 buf.append("("); 239 240 boolean first = true; 241 Class <?>[] params = m.getParameterTypes(); 242 for (int i = 0; i < params.length; i++) { 243 if (first) first = false; 244 else buf.append(", "); 245 buf.append(params[i].getName()); 246 } 247 248 buf.append(")"); 249 buf.append(" is not a static method."); 250 251 throw new InteractionsException(buf.toString()); 252 } 253 254 Object ret = super.visit(node); 255 256 if (m.getReturnType().equals(Void.TYPE)) return Interpreter.NO_RESULT; 258 else return ret; 259 } 260 261 public Object visit(SimpleAssignExpression node) { 262 _checkInterrupted(node); 263 return super.visit(node); 264 } 265 266 public Object visit(QualifiedName node) { 267 _checkInterrupted(node); 268 return super.visit(node); 269 } 270 271 public Object visit(TypeExpression node) { 272 _checkInterrupted(node); 273 return super.visit(node); 274 } 275 276 public Object visit(SimpleAllocation node) { 277 _checkInterrupted(node); 278 return super.visit(node); 279 } 280 281 public Object visit(ArrayAllocation node) { 282 _checkInterrupted(node); 283 return super.visit(node); 284 } 285 286 public Object visit(ArrayInitializer node) { 287 _checkInterrupted(node); 288 return super.visit(node); 289 } 290 291 public Object visit(ArrayAccess node) { 292 _checkInterrupted(node); 293 return super.visit(node); 294 } 295 296 public Object visit(InnerAllocation node) { 297 _checkInterrupted(node); 298 return super.visit(node); 299 } 300 301 public Object visit(ClassAllocation node) { 302 _checkInterrupted(node); 303 return super.visit(node); 304 } 305 306 public Object visit(NotExpression node) { 307 _checkInterrupted(node); 308 return super.visit(node); 309 } 310 311 public Object visit(ComplementExpression node) { 312 _checkInterrupted(node); 313 return super.visit(node); 314 } 315 316 public Object visit(PlusExpression node) { 317 _checkInterrupted(node); 318 return super.visit(node); 319 } 320 321 public Object visit(MinusExpression node) { 322 _checkInterrupted(node); 323 return super.visit(node); 324 } 325 326 public Object visit(AddExpression node) { 327 _checkInterrupted(node); 328 return super.visit(node); 329 } 330 331 public Object visit(AddAssignExpression node) { 332 _checkInterrupted(node); 333 return super.visit(node); 334 } 335 336 public Object visit(SubtractExpression node) { 337 _checkInterrupted(node); 338 return super.visit(node); 339 } 340 341 public Object visit(SubtractAssignExpression node) { 342 _checkInterrupted(node); 343 return super.visit(node); 344 } 345 346 public Object visit(MultiplyExpression node) { 347 _checkInterrupted(node); 348 return super.visit(node); 349 } 350 351 public Object visit(MultiplyAssignExpression node) { 352 _checkInterrupted(node); 353 return super.visit(node); 354 } 355 356 public Object visit(DivideExpression node) { 357 _checkInterrupted(node); 358 return super.visit(node); 359 } 360 361 public Object visit(DivideAssignExpression node) { 362 _checkInterrupted(node); 363 return super.visit(node); 364 } 365 366 public Object visit(RemainderExpression node) { 367 _checkInterrupted(node); 368 return super.visit(node); 369 } 370 371 public Object visit(RemainderAssignExpression node) { 372 _checkInterrupted(node); 373 return super.visit(node); 374 } 375 376 public Object visit(EqualExpression node) { 377 _checkInterrupted(node); 378 return super.visit(node); 379 } 380 381 public Object visit(NotEqualExpression node) { 382 _checkInterrupted(node); 383 return super.visit(node); 384 } 385 386 public Object visit(LessExpression node) { 387 _checkInterrupted(node); 388 return super.visit(node); 389 } 390 391 public Object visit(LessOrEqualExpression node) { 392 _checkInterrupted(node); 393 return super.visit(node); 394 } 395 396 public Object visit(GreaterExpression node) { 397 _checkInterrupted(node); 398 return super.visit(node); 399 } 400 401 public Object visit(GreaterOrEqualExpression node) { 402 _checkInterrupted(node); 403 return super.visit(node); 404 } 405 406 public Object visit(InstanceOfExpression node) { 407 _checkInterrupted(node); 408 return super.visit(node); 409 } 410 411 public Object visit(ConditionalExpression node) { 412 _checkInterrupted(node); 413 return super.visit(node); 414 } 415 416 public Object visit(PostIncrement node) { 417 _checkInterrupted(node); 418 return super.visit(node); 419 } 420 421 public Object visit(PreIncrement node) { 422 _checkInterrupted(node); 423 return super.visit(node); 424 } 425 426 public Object visit(PostDecrement node) { 427 _checkInterrupted(node); 428 return super.visit(node); 429 } 430 431 public Object visit(PreDecrement node) { 432 _checkInterrupted(node); 433 return super.visit(node); 434 } 435 436 public Object visit(CastExpression node) { 437 _checkInterrupted(node); 438 return super.visit(node); 439 } 440 441 public Object visit(BitAndExpression node) { 442 _checkInterrupted(node); 443 return super.visit(node); 444 } 445 446 public Object visit(BitAndAssignExpression node) { 447 _checkInterrupted(node); 448 return super.visit(node); 449 } 450 451 public Object visit(ExclusiveOrExpression node) { 452 _checkInterrupted(node); 453 return super.visit(node); 454 } 455 456 public Object visit(ExclusiveOrAssignExpression node) { 457 _checkInterrupted(node); 458 return super.visit(node); 459 } 460 461 public Object visit(BitOrExpression node) { 462 _checkInterrupted(node); 463 return super.visit(node); 464 } 465 466 public Object visit(BitOrAssignExpression node) { 467 _checkInterrupted(node); 468 return super.visit(node); 469 } 470 471 public Object visit(ShiftLeftExpression node) { 472 _checkInterrupted(node); 473 return super.visit(node); 474 } 475 476 public Object visit(ShiftLeftAssignExpression node) { 477 _checkInterrupted(node); 478 return super.visit(node); 479 } 480 481 public Object visit(ShiftRightExpression node) { 482 _checkInterrupted(node); 483 return super.visit(node); 484 } 485 486 public Object visit(ShiftRightAssignExpression node) { 487 _checkInterrupted(node); 488 return super.visit(node); 489 } 490 491 public Object visit(UnsignedShiftRightExpression node) { 492 _checkInterrupted(node); 493 return super.visit(node); 494 } 495 496 public Object visit(UnsignedShiftRightAssignExpression node) { 497 _checkInterrupted(node); 498 return super.visit(node); 499 } 500 501 public Object visit(AndExpression node) { 502 _checkInterrupted(node); 503 return super.visit(node); 504 } 505 506 public Object visit(OrExpression node) { 507 _checkInterrupted(node); 508 return super.visit(node); 509 } 510 511 public Object visit(FunctionCall node) { 512 _checkInterrupted(node); 513 Object ret = super.visit(node); 515 516 if (Void.TYPE.equals(node.getProperty(NodeProperties.TYPE))) return Interpreter.NO_RESULT; 518 else return ret; 519 } 520 521 public Object visit(PackageDeclaration node) { return Interpreter.NO_RESULT; } 522 523 public Object visit(ImportDeclaration node) { return Interpreter.NO_RESULT; } 524 525 public Object visit(EmptyStatement node) { return Interpreter.NO_RESULT; } 526 527 public Object visit(ClassDeclaration node) { return Interpreter.NO_RESULT; } 528 529 public Object visit(InterfaceDeclaration node) { return Interpreter.NO_RESULT; } 530 531 public Object visit(MethodDeclaration node) { return Interpreter.NO_RESULT; } 532 } 533 | Popular Tags |