KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > repl > EvaluationVisitorExtension


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

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 /**
43  * A subclass of EvaluationVisitor to do two new things.
44  * <OL>
45  * <LI>Check thread interrupted status and throw InterruptedException
46  * if the thread was interrupted.</LI>
47  * <LI>Returns Interpreter.NO_RESULT if the computation
48  * had no result. (This is instead of returning null, which
49  * DynamicJava does.</LI>
50  * </OL>
51  *
52  * This class is loaded in the Interpreter JVM, not the Main JVM.
53  * (Do not use DrJava's config framework here.)
54  *
55  * @version $Id: EvaluationVisitorExtension.java 3903 2006-07-05 20:03:06Z rcartwright $
56  */

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     // An interesting and arcane Thread fact: There are two different methods to check if a Thread is interrupted.
67
// (See the javadocs.) Thread.isInterrupted() gets the status but doesn't reset it, while Thread.interrupted()
68
// gets the status and resets it. This code did not work when I used isInterrupted.
69
if (Thread.currentThread().interrupted()) {
70       throw new InterpreterInterruptedException(node.getBeginLine(), node.getBeginColumn(), node.getEndLine(),
71                                                 node.getEndColumn());
72     }
73   }
74
75   /* Note: protected static Object performCast(Class<?> tc, Object o) is inherited from EvaluationVisitor */
76   
77   public Object JavaDoc visit(WhileStatement node) {
78     _checkInterrupted(node);
79     super.visit(node);
80     return Interpreter.NO_RESULT;
81   }
82
83   public Object JavaDoc visit(ForStatement node) {
84     _checkInterrupted(node);
85     super.visit(node);
86     return Interpreter.NO_RESULT;
87   }
88
89   public Object JavaDoc visit(ForEachStatement node) {
90     _checkInterrupted(node);
91     super.visit(node);
92     return Interpreter.NO_RESULT;
93   }
94
95   public Object JavaDoc visit(DoStatement node) {
96     _checkInterrupted(node);
97     super.visit(node);
98     return Interpreter.NO_RESULT;
99   }
100
101   public Object JavaDoc visit(SwitchStatement node) {
102     _checkInterrupted(node);
103     super.visit(node);
104     return Interpreter.NO_RESULT;
105   }
106
107   public Object JavaDoc visit(LabeledStatement node) {
108     _checkInterrupted(node);
109     super.visit(node);
110     return Interpreter.NO_RESULT;
111   }
112
113   public Object JavaDoc visit(SynchronizedStatement node) {
114     _checkInterrupted(node);
115     super.visit(node);
116     return Interpreter.NO_RESULT;
117   }
118
119   public Object JavaDoc visit(TryStatement node) {
120     _checkInterrupted(node);
121     super.visit(node);
122     return Interpreter.NO_RESULT;
123   }
124
125   public Object JavaDoc visit(IfThenStatement node) {
126     _checkInterrupted(node);
127     super.visit(node);
128     return Interpreter.NO_RESULT;
129   }
130
131   public Object JavaDoc visit(IfThenElseStatement node) {
132     _checkInterrupted(node);
133     super.visit(node);
134     return Interpreter.NO_RESULT;
135   }
136   
137   public Object JavaDoc visit(AssertStatement node) {
138     _checkInterrupted(node);
139     super.visit(node);
140     return Interpreter.NO_RESULT;
141   }
142
143   public Object JavaDoc visit(BlockStatement node) {
144     _checkInterrupted(node);
145     super.visit(node);
146     return Interpreter.NO_RESULT;
147   }
148
149   public Object JavaDoc visit(Literal node) {
150     _checkInterrupted(node);
151     return super.visit(node);
152   }
153
154   /** Overrides EvaluationVisitor to enforce a proper type check at runtime. It combines code from the actual visit
155    * code in EvaluationVisitor as well as code from the modify method in VariableModifier.
156    */

157   public Object JavaDoc visit(VariableDeclaration node) {
158     _checkInterrupted(node);
159     Class JavaDoc<?> c = (Class JavaDoc<?>) NodeProperties.getType(node.getType());
160
161     if (node.getInitializer() != null) {
162       Object JavaDoc o = performCast(c, node.getInitializer().acceptVisitor(this));
163
164       // Forces a runtime type-check on the cast.
165
String JavaDoc name = node.getName();
166
167       if (!(c.isPrimitive() || o == null || c.isAssignableFrom(o.getClass()))) {
168         Exception JavaDoc e = new ClassCastException JavaDoc(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       // Non-final variables have default values, and are not uninitialized.
178
// Primitive variables have special default values, Objects default to null.
179
// Fixes bug #797515.
180
// _context.set(node.getName(), UninitializedObject.INSTANCE);
181
Object JavaDoc value = null;
182       if (!c.isPrimitive()) value = null;
183       else if (c == byte.class) value = new Byte JavaDoc((byte) 0);
184       else if (c == short.class) value = new Short JavaDoc((short) 0);
185       else if (c == int.class) value = new Integer JavaDoc(0);
186       else if (c == long.class) value = new Long JavaDoc(0L);
187       else if (c == float.class) value = new Float JavaDoc(0.0f);
188       else if (c == double.class) value = new Double JavaDoc(0.0d);
189       else if (c == char.class) value = new Character JavaDoc('\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 JavaDoc visit(ObjectFieldAccess node) {
197     _checkInterrupted(node);
198     return super.visit(node);
199   }
200
201   public Object JavaDoc visit(ObjectMethodCall node) {
202     _checkInterrupted(node);
203     Method m = (Method) node.getProperty(NodeProperties.METHOD);
204 // m.setAccessible(true);
205
Object JavaDoc ret = super.visit(node);
206     
207     // this workaround avoids returning null for void returns; null test intercepts array clone invocation which has
208
// no METHOD property
209
if (m != null && m.getReturnType().equals(Void.TYPE)) return Interpreter.NO_RESULT;
210     return ret;
211   }
212
213   public Object JavaDoc visit(StaticFieldAccess node) {
214     _checkInterrupted(node);
215     return super.visit(node);
216   }
217
218   public Object JavaDoc visit(SuperFieldAccess node) {
219     _checkInterrupted(node);
220     return super.visit(node);
221   }
222
223   public Object JavaDoc visit(SuperMethodCall node) {
224     _checkInterrupted(node);
225     return super.visit(node);
226   }
227
228   public Object JavaDoc visit(StaticMethodCall node) {
229     _checkInterrupted(node);
230     Method m = (Method) node.getProperty(NodeProperties.METHOD);
231
232     // DynamicJava doesn't check that the method is really static!
233
if (! Modifier.isStatic(m.getModifiers())) {
234       final StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
235       buf.append(m.getDeclaringClass());
236       buf.append(".");
237       buf.append(m.getName());
238       buf.append("(");
239
240       boolean first = true;
241       Class JavaDoc<?>[] 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 JavaDoc ret = super.visit(node);
255
256     // workaround to not return null for void returns
257
if (m.getReturnType().equals(Void.TYPE)) return Interpreter.NO_RESULT;
258     else return ret;
259   }
260
261   public Object JavaDoc visit(SimpleAssignExpression node) {
262     _checkInterrupted(node);
263     return super.visit(node);
264   }
265
266   public Object JavaDoc visit(QualifiedName node) {
267     _checkInterrupted(node);
268     return super.visit(node);
269   }
270
271   public Object JavaDoc visit(TypeExpression node) {
272     _checkInterrupted(node);
273     return super.visit(node);
274   }
275
276   public Object JavaDoc visit(SimpleAllocation node) {
277     _checkInterrupted(node);
278     return super.visit(node);
279   }
280
281   public Object JavaDoc visit(ArrayAllocation node) {
282     _checkInterrupted(node);
283     return super.visit(node);
284   }
285
286   public Object JavaDoc visit(ArrayInitializer node) {
287     _checkInterrupted(node);
288     return super.visit(node);
289   }
290
291   public Object JavaDoc visit(ArrayAccess node) {
292     _checkInterrupted(node);
293     return super.visit(node);
294   }
295
296   public Object JavaDoc visit(InnerAllocation node) {
297     _checkInterrupted(node);
298     return super.visit(node);
299   }
300
301   public Object JavaDoc visit(ClassAllocation node) {
302     _checkInterrupted(node);
303     return super.visit(node);
304   }
305
306   public Object JavaDoc visit(NotExpression node) {
307     _checkInterrupted(node);
308     return super.visit(node);
309   }
310
311   public Object JavaDoc visit(ComplementExpression node) {
312     _checkInterrupted(node);
313     return super.visit(node);
314   }
315
316   public Object JavaDoc visit(PlusExpression node) {
317     _checkInterrupted(node);
318     return super.visit(node);
319   }
320
321   public Object JavaDoc visit(MinusExpression node) {
322     _checkInterrupted(node);
323     return super.visit(node);
324   }
325
326   public Object JavaDoc visit(AddExpression node) {
327     _checkInterrupted(node);
328     return super.visit(node);
329   }
330
331   public Object JavaDoc visit(AddAssignExpression node) {
332     _checkInterrupted(node);
333     return super.visit(node);
334   }
335
336   public Object JavaDoc visit(SubtractExpression node) {
337     _checkInterrupted(node);
338     return super.visit(node);
339   }
340
341   public Object JavaDoc visit(SubtractAssignExpression node) {
342     _checkInterrupted(node);
343     return super.visit(node);
344   }
345
346   public Object JavaDoc visit(MultiplyExpression node) {
347     _checkInterrupted(node);
348     return super.visit(node);
349   }
350
351   public Object JavaDoc visit(MultiplyAssignExpression node) {
352     _checkInterrupted(node);
353     return super.visit(node);
354   }
355
356   public Object JavaDoc visit(DivideExpression node) {
357     _checkInterrupted(node);
358     return super.visit(node);
359   }
360
361   public Object JavaDoc visit(DivideAssignExpression node) {
362     _checkInterrupted(node);
363     return super.visit(node);
364   }
365
366   public Object JavaDoc visit(RemainderExpression node) {
367     _checkInterrupted(node);
368     return super.visit(node);
369   }
370
371   public Object JavaDoc visit(RemainderAssignExpression node) {
372     _checkInterrupted(node);
373     return super.visit(node);
374   }
375
376   public Object JavaDoc visit(EqualExpression node) {
377     _checkInterrupted(node);
378     return super.visit(node);
379   }
380
381   public Object JavaDoc visit(NotEqualExpression node) {
382     _checkInterrupted(node);
383     return super.visit(node);
384   }
385
386   public Object JavaDoc visit(LessExpression node) {
387     _checkInterrupted(node);
388     return super.visit(node);
389   }
390
391   public Object JavaDoc visit(LessOrEqualExpression node) {
392     _checkInterrupted(node);
393     return super.visit(node);
394   }
395
396   public Object JavaDoc visit(GreaterExpression node) {
397     _checkInterrupted(node);
398     return super.visit(node);
399   }
400
401   public Object JavaDoc visit(GreaterOrEqualExpression node) {
402     _checkInterrupted(node);
403     return super.visit(node);
404   }
405
406   public Object JavaDoc visit(InstanceOfExpression node) {
407     _checkInterrupted(node);
408     return super.visit(node);
409   }
410
411   public Object JavaDoc visit(ConditionalExpression node) {
412     _checkInterrupted(node);
413     return super.visit(node);
414   }
415
416   public Object JavaDoc visit(PostIncrement node) {
417     _checkInterrupted(node);
418     return super.visit(node);
419   }
420
421   public Object JavaDoc visit(PreIncrement node) {
422     _checkInterrupted(node);
423     return super.visit(node);
424   }
425
426   public Object JavaDoc visit(PostDecrement node) {
427     _checkInterrupted(node);
428     return super.visit(node);
429   }
430
431   public Object JavaDoc visit(PreDecrement node) {
432     _checkInterrupted(node);
433     return super.visit(node);
434   }
435
436   public Object JavaDoc visit(CastExpression node) {
437     _checkInterrupted(node);
438     return super.visit(node);
439   }
440
441   public Object JavaDoc visit(BitAndExpression node) {
442     _checkInterrupted(node);
443     return super.visit(node);
444   }
445
446   public Object JavaDoc visit(BitAndAssignExpression node) {
447     _checkInterrupted(node);
448     return super.visit(node);
449   }
450
451   public Object JavaDoc visit(ExclusiveOrExpression node) {
452     _checkInterrupted(node);
453     return super.visit(node);
454   }
455
456   public Object JavaDoc visit(ExclusiveOrAssignExpression node) {
457     _checkInterrupted(node);
458     return super.visit(node);
459   }
460
461   public Object JavaDoc visit(BitOrExpression node) {
462     _checkInterrupted(node);
463     return super.visit(node);
464   }
465
466   public Object JavaDoc visit(BitOrAssignExpression node) {
467     _checkInterrupted(node);
468     return super.visit(node);
469   }
470
471   public Object JavaDoc visit(ShiftLeftExpression node) {
472     _checkInterrupted(node);
473     return super.visit(node);
474   }
475
476   public Object JavaDoc visit(ShiftLeftAssignExpression node) {
477     _checkInterrupted(node);
478     return super.visit(node);
479   }
480
481   public Object JavaDoc visit(ShiftRightExpression node) {
482     _checkInterrupted(node);
483     return super.visit(node);
484   }
485
486   public Object JavaDoc visit(ShiftRightAssignExpression node) {
487     _checkInterrupted(node);
488     return super.visit(node);
489   }
490
491   public Object JavaDoc visit(UnsignedShiftRightExpression node) {
492     _checkInterrupted(node);
493     return super.visit(node);
494   }
495
496   public Object JavaDoc visit(UnsignedShiftRightAssignExpression node) {
497     _checkInterrupted(node);
498     return super.visit(node);
499   }
500
501   public Object JavaDoc visit(AndExpression node) {
502     _checkInterrupted(node);
503     return super.visit(node);
504   }
505
506   public Object JavaDoc visit(OrExpression node) {
507     _checkInterrupted(node);
508     return super.visit(node);
509   }
510
511   public Object JavaDoc visit(FunctionCall node) {
512     _checkInterrupted(node);
513 // Method m = (Method) node.getProperty(NodeProperties.METHOD);
514
Object JavaDoc ret = super.visit(node);
515
516     // workaround to not return null for void returns
517
if (Void.TYPE.equals(node.getProperty(NodeProperties.TYPE))) return Interpreter.NO_RESULT;
518     else return ret;
519   }
520
521   public Object JavaDoc visit(PackageDeclaration node) { return Interpreter.NO_RESULT; }
522
523   public Object JavaDoc visit(ImportDeclaration node) { return Interpreter.NO_RESULT; }
524
525   public Object JavaDoc visit(EmptyStatement node) { return Interpreter.NO_RESULT; }
526
527   public Object JavaDoc visit(ClassDeclaration node) { return Interpreter.NO_RESULT; }
528   
529   public Object JavaDoc visit(InterfaceDeclaration node) { return Interpreter.NO_RESULT; }
530   
531   public Object JavaDoc visit(MethodDeclaration node) { return Interpreter.NO_RESULT; }
532 }
533
Popular Tags