1 33 34 package edu.rice.cs.drjava.model.repl; 35 36 import java.util.List ; 37 import java.io.StringReader ; 38 import java.io.Reader ; 39 import java.net.URL ; 40 import edu.rice.cs.drjava.model.repl.newjvm.ClassPathManager; 41 import koala.dynamicjava.interpreter.*; 42 import koala.dynamicjava.interpreter.context.*; 43 import koala.dynamicjava.interpreter.error.*; 44 import koala.dynamicjava.interpreter.throwable.*; 45 import koala.dynamicjava.parser.wrapper.*; 46 import koala.dynamicjava.tree.*; 47 48 import edu.rice.cs.util.classloader.StickyClassLoader; 49 import edu.rice.cs.util.*; 50 51 54 55 62 public class DynamicJavaAdapter implements JavaInterpreter { 63 private static Log _log = new Log("MasterSlave.txt", false); 64 private InterpreterExtension _djInterpreter; 65 66 ClassPathManager cpm; 67 68 69 public DynamicJavaAdapter(ClassPathManager c) { 70 cpm = c; 71 _djInterpreter = new InterpreterExtension(c); 72 } 73 74 78 public Object interpret(String s) throws ExceptionReturnedException { 79 boolean print = false; 80 81 85 s = s.trim(); 86 if (!s.endsWith(";")) { 87 print = true; 89 } 90 91 StringReader reader = new StringReader (s); 92 try { 93 Object result = _djInterpreter.interpret(reader, "DrJava"); 94 if (print) return result; 95 else return JavaInterpreter.NO_RESULT; 96 } 97 catch (InterpreterException ie) { 98 Throwable cause = ie.getException(); 99 if (cause instanceof ThrownException) cause = ((ThrownException) cause).getException(); 100 else if (cause instanceof CatchedExceptionError) cause = ((CatchedExceptionError) cause).getException(); 101 102 throw new ExceptionReturnedException(cause); 103 } 104 catch (CatchedExceptionError cee) { 105 throw new ExceptionReturnedException(cee.getException()); 106 } 107 catch (InterpreterInterruptedException iie) { 108 return JavaInterpreter.NO_RESULT; 109 } 110 catch (ExitingNotAllowedException enae) { 111 return JavaInterpreter.NO_RESULT; 112 } 113 } 123 124 public List <Node> parse(String input) { return _djInterpreter.parse(input); } 125 126 129 134 public void addProjectClassPath(URL path) { cpm.addProjectCP(path); } 135 136 public void addBuildDirectoryClassPath(URL path) { cpm.addBuildDirectoryCP(path); } 137 138 public void addProjectFilesClassPath(URL path) { cpm.addProjectFilesCP(path); } 139 140 public void addExternalFilesClassPath(URL path) { cpm.addExternalFilesCP(path); } 141 142 public void addExtraClassPath(URL path) { cpm.addExtraCP(path); } 143 144 147 public void setPackageScope(String packageName) { 148 StringReader reader = new StringReader ("package " + packageName + ";"); 149 _djInterpreter.interpret(reader, "DrJava"); 150 } 151 152 156 public Object getVariable(String name) { return _djInterpreter.getVariable(name); } 157 158 162 public Class <?> getVariableClass(String name) { return _djInterpreter.getVariableClass(name); } 163 164 170 public void defineVariable(String name, Object value, Class <?> type) { 171 if (type == null) type = java.lang.Object .class; 172 ((TreeInterpreter)_djInterpreter).defineVariable(name, value, type); 173 } 174 175 179 public void defineVariable(String name, Object value) { 180 ((TreeInterpreter)_djInterpreter).defineVariable(name, value); 181 } 182 183 187 public void defineVariable(String name, boolean value) { 188 ((TreeInterpreter)_djInterpreter).defineVariable(name, value); 189 } 190 191 195 public void defineVariable(String name, byte value) { 196 ((TreeInterpreter)_djInterpreter).defineVariable(name, value); 197 } 198 199 203 public void defineVariable(String name, char value) { 204 ((TreeInterpreter)_djInterpreter).defineVariable(name, value); 205 } 206 207 211 public void defineVariable(String name, double value) { 212 ((TreeInterpreter)_djInterpreter).defineVariable(name, value); 213 } 214 215 219 public void defineVariable(String name, float value) { 220 ((TreeInterpreter)_djInterpreter).defineVariable(name, value); 221 } 222 223 224 228 public void defineVariable(String name, int value) { 229 ((TreeInterpreter)_djInterpreter).defineVariable(name, value); 230 } 231 232 236 public void defineVariable(String name, long value) { 237 ((TreeInterpreter)_djInterpreter).defineVariable(name, value); 238 } 239 240 244 public void defineVariable(String name, short value) { 245 ((TreeInterpreter)_djInterpreter).defineVariable(name, value); 246 } 247 248 252 public void defineConstant(String name, Object value) { 253 _djInterpreter.defineConstant(name, value); 254 } 255 256 260 public void defineConstant(String name, boolean value) { 261 _djInterpreter.defineConstant(name, value); 262 } 263 264 268 public void defineConstant(String name, byte value) { 269 _djInterpreter.defineConstant(name, value); 270 } 271 272 276 public void defineConstant(String name, char value) { 277 _djInterpreter.defineConstant(name, value); 278 } 279 280 284 public void defineConstant(String name, double value) { 285 _djInterpreter.defineConstant(name, value); 286 } 287 288 292 public void defineConstant(String name, float value) { 293 _djInterpreter.defineConstant(name, value); 294 } 295 296 300 public void defineConstant(String name, int value) { 301 _djInterpreter.defineConstant(name, value); 302 } 303 304 308 public void defineConstant(String name, long value) { 309 _djInterpreter.defineConstant(name, value); 310 } 311 315 public void defineConstant(String name, short value) { 316 _djInterpreter.defineConstant(name, value); 317 } 318 319 322 public void setPrivateAccessible(boolean accessible) { 323 _djInterpreter.setAccessible(accessible); 324 } 325 326 327 public boolean getPrivateAccessible() { return _djInterpreter.getAccessible(); } 328 329 333 public NameVisitor makeNameVisitor(Context nameContext) { return new NameVisitor(nameContext); } 334 335 341 347 351 public EvaluationVisitor makeEvaluationVisitor(Context context) { 352 return new EvaluationVisitorExtension(context); 353 } 354 355 358 public Node processTree(Node node) { return node; } 359 360 public GlobalContext makeGlobalContext(TreeInterpreter i) { return new GlobalContext(i); } 361 362 369 public class InterpreterExtension extends TreeInterpreter { 370 371 372 public InterpreterExtension(ClassPathManager cpm) { 373 super(new JavaCCParserFactory()); 374 375 classLoader = new ClassLoaderExtension(this, cpm); 376 nameVisitorContext = makeGlobalContext(this); 379 ClassLoaderContainer clc = new ClassLoaderContainer() { 380 public ClassLoader getClassLoader() { return classLoader; } 381 }; 382 nameVisitorContext.setAdditionalClassLoaderContainer(clc); 383 checkVisitorContext = makeGlobalContext(this); 384 checkVisitorContext.setAdditionalClassLoaderContainer(clc); 385 evalVisitorContext = makeGlobalContext(this); 386 evalVisitorContext.setAdditionalClassLoaderContainer(clc); 387 389 } 390 391 397 public Object interpret(Reader r, String fname) throws InterpreterException { 398 List <Node> statements; 399 try { 400 SourceCodeParser p = parserFactory.createParser(r, fname); 401 statements = p.parseStream(); 402 } 404 catch (ParseError e) { 405 throw new InterpreterException(e); 408 } 409 410 Object result = JavaInterpreter.NO_RESULT; 411 412 nameVisitorContext.setRevertPoint(); 413 checkVisitorContext.setRevertPoint(); 414 evalVisitorContext.setRevertPoint(); 415 416 try { 417 for (Node n : statements) { 418 n = processTree(n); 419 420 NameVisitor nv = makeNameVisitor(nameVisitorContext); 421 Node o = n.acceptVisitor(nv); 422 if (o != null) n = o; 423 424 AbstractTypeChecker tc = AbstractTypeChecker.makeTypeChecker(checkVisitorContext); 425 426 n.acceptVisitor(tc); 427 428 evalVisitorContext.defineVariables(checkVisitorContext.getCurrentScopeVariables()); 429 430 EvaluationVisitor ev = makeEvaluationVisitor(evalVisitorContext); 431 _log.log("Ready to interpret " + ev); 432 result = n.acceptVisitor(ev); 433 _log.log("Interpreted result is: " + result); 434 } 435 } 436 catch (ExecutionError e) { 437 nameVisitorContext.revert(); 440 checkVisitorContext.revert(); 441 evalVisitorContext.revert(); 442 443 throw new InterpreterException(e); 445 } 446 447 if (result instanceof String ) return "\"" + result + "\""; 448 if (result instanceof Character ) return "'" + result + "'"; 449 return result; 450 } 451 452 457 public void defineConstant(String name, Object value) { 458 Class <?> c = (value == null) ? null : value.getClass(); 459 nameVisitorContext.defineConstant(name, c); 460 checkVisitorContext.defineConstant(name, c); 461 evalVisitorContext.defineConstant(name, value); 462 } 463 464 468 public void defineConstant(String name, boolean value) { 469 Class <?> c = boolean.class; 470 nameVisitorContext.defineConstant(name, c); 471 checkVisitorContext.defineConstant(name, c); 472 evalVisitorContext.defineConstant(name, Boolean.valueOf(value)); 473 } 474 475 479 public void defineConstant(String name, byte value) { 480 Class <?> c = byte.class; 481 nameVisitorContext.defineConstant(name, c); 482 checkVisitorContext.defineConstant(name, c); 483 evalVisitorContext.defineConstant(name, new Byte (value)); 484 } 485 486 490 public void defineConstant(String name, char value) { 491 Class <?> c = char.class; 492 nameVisitorContext.defineConstant(name, c); 493 checkVisitorContext.defineConstant(name, c); 494 evalVisitorContext.defineConstant(name, new Character (value)); 495 } 496 497 501 public void defineConstant(String name, double value) { 502 Class <?> c = double.class; 503 nameVisitorContext.defineConstant(name, c); 504 checkVisitorContext.defineConstant(name, c); 505 evalVisitorContext.defineConstant(name, new Double (value)); 506 } 507 508 512 public void defineConstant(String name, float value) { 513 Class <?> c = float.class; 514 nameVisitorContext.defineConstant(name, c); 515 checkVisitorContext.defineConstant(name, c); 516 evalVisitorContext.defineConstant(name, new Float (value)); 517 } 518 519 523 public void defineConstant(String name, int value) { 524 Class <?> c = int.class; 525 nameVisitorContext.defineConstant(name, c); 526 checkVisitorContext.defineConstant(name, c); 527 evalVisitorContext.defineConstant(name, new Integer (value)); 528 } 529 530 534 public void defineConstant(String name, long value) { 535 Class <?> c = long.class; 536 nameVisitorContext.defineConstant(name, c); 537 checkVisitorContext.defineConstant(name, c); 538 evalVisitorContext.defineConstant(name, new Long (value)); 539 } 540 541 545 public void defineConstant(String name, short value) { 546 Class <?> c = short.class; 547 nameVisitorContext.defineConstant(name, c); 548 checkVisitorContext.defineConstant(name, c); 549 evalVisitorContext.defineConstant(name, new Short (value)); 550 } 551 } 552 553 554 public static class ClassLoaderExtension extends TreeClassLoader { 555 556 559 private static boolean classLoaderCreated = false; 560 561 private static StickyClassLoader _stickyLoader; 562 563 ClassPathManager cpm; 565 566 569 public ClassLoaderExtension(koala.dynamicjava.interpreter.Interpreter i, ClassPathManager c) { 570 super(i); 571 cpm = c; 572 classLoader = new WrapperClassLoader(getClass().getClassLoader()); 581 String [] excludes = { 584 "edu.rice.cs.drjava.model.repl.DynamicJavaAdapter$InterpreterExtension", 585 "edu.rice.cs.drjava.model.repl.DynamicJavaAdapter$ClassLoaderExtension" 586 }; 587 588 if (!classLoaderCreated) { 589 _stickyLoader = new StickyClassLoader(this, classLoader, excludes); 592 classLoaderCreated = true; 593 } 594 595 } 597 598 601 public URL getResource(String name) { 602 return cpm.getClassLoader().getResource(name); 604 } 606 607 protected Class <?> loadClass(String name, boolean resolve) throws ClassNotFoundException { 608 Class <?> clazz; 609 610 if (classes.containsKey(name)) clazz = (Class <?>) classes.get(name); 612 else { 613 try { 614 clazz = _stickyLoader.loadClass(name); 615 } 616 catch (ClassNotFoundException e) { 617 clazz = interpreter.loadClass(name); 621 } 622 } 623 624 if (resolve) resolveClass(clazz); 625 626 return clazz; 627 } 628 629 630 660 675 } 676 } 677 | Popular Tags |