KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > base > Evaluator


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  */

22
23 package org.aspectj.debugger.base;
24
25 import com.sun.jdi.*;
26 import java.math.*;
27
28 // For parsing expressions
29
import org.aspectj.compiler.base.*;
30 import org.aspectj.compiler.base.parser.*;
31
32 /**
33  * Evaluator.java
34  *
35  *
36  * Created: Fri Sep 08 17:07:25 2000
37  *
38  * @author <a HREF="mailto:palm@parc.xerox.com"Jeffrey Palm</a>
39  */

40
41 public class Evaluator {
42
43     private Debugger debugger;
44
45     public final static String JavaDoc[] keywords = {
46         "abstract",
47         "boolean",
48         "break",
49         "byte",
50         "catch",
51         "char",
52         "class",
53         "continue",
54         "do",
55         "double",
56         "else",
57         "extends",
58         "false",
59         "final",
60         "finally",
61         "float",
62         "for",
63         "if",
64         "implements",
65         "import",
66         "instanceof",
67         "int",
68         "long",
69         "new",
70         "null",
71         "package",
72         "private",
73         "protected",
74         "public",
75         "return",
76         "short",
77         "static",
78         "super",
79         "switch",
80         "case",
81         "default",
82         "synchronized",
83         "this",
84         "throw",
85         "throws",
86         "try",
87         "true",
88         "void",
89         "while",
90     };
91
92     public Evaluator(Debugger debugger) {
93         this.debugger = debugger;
94     }
95
96     private AJDebugger dbg() {
97         return (AJDebugger) debugger;
98     }
99
100     private VirtualMachine vm() throws NoVMException {
101         return dbg().getVM();
102     }
103
104     public Value getValue(Object JavaDoc o, StackFrame frame)
105         throws NoVMException, DebuggerException {
106         String JavaDoc valueString = (o + "").trim();
107         if (isIdent(valueString)) {
108             LocalVariable local = getLocalVariable(o, frame);
109             if (local != null) {
110                 return frame.getValue(local);
111             }
112         }
113         return getPrimitiveValue(valueString);
114     }
115
116     public Value getPrimitiveValue(String JavaDoc valueString)
117         throws NoVMException, DebuggerException {
118         if (valueString.startsWith("\"")) {
119             if (valueString.length() > 1) {
120                 String JavaDoc s;
121                 if (valueString.endsWith("\"")) {
122                     s = valueString.substring(1, valueString.length() - 1);
123                 } else {
124                     s = valueString.substring(1);
125                 }
126                 return vm().mirrorOf(s);
127             }
128         } else if (valueString.startsWith("'")) {
129             if (valueString.length() > 1) {
130                 char c = valueString.charAt(1);
131                 return vm().mirrorOf(c);
132             }
133         } else if (valueString.equals("true")) {
134             return vm().mirrorOf(true);
135         } else if (valueString.equals("false")) {
136             return vm().mirrorOf(false);
137         } else {
138             try {
139                 return vm().mirrorOf(Integer.parseInt(valueString));
140             } catch (NumberFormatException JavaDoc e) {
141             }
142             try {
143                 return vm().mirrorOf(Long.parseLong(valueString));
144             } catch (NumberFormatException JavaDoc e) {
145             }
146             try {
147                 return vm().mirrorOf(Float.parseFloat(valueString));
148             } catch (NumberFormatException JavaDoc e) {
149             }
150             try {
151                 return vm().mirrorOf(Double.parseDouble(valueString));
152             } catch (NumberFormatException JavaDoc e) {
153             }
154         }
155         throw new UnableToParseException(valueString);
156     }
157
158     public LocalVariable getLocalVariable(Object JavaDoc o, StackFrame frame)
159         throws NoVMException, DebuggerException {
160         String JavaDoc valueString = o + "";
161         if (isIdent(valueString)) {
162             try {
163                 LocalVariable local = frame.visibleVariableByName(valueString);
164                 return local;
165             } catch (AbsentInformationException aie) {
166             } catch (InvalidStackFrameException isfe) {
167             }
168         }
169         throw new UnableToParseException(valueString);
170     }
171
172     private boolean isNumber(String JavaDoc s) {
173         try {
174             new BigDecimal(s);
175             return true;
176         } catch (NumberFormatException JavaDoc e) {
177         }
178         return false;
179     }
180
181     private boolean isIdent(String JavaDoc s) {
182         if (s == null || s.length() == 0 || s.equals("")) {
183             return true;
184         }
185         if (isJavaKeyword(s)) {
186             return false;
187         }
188         if (!(Character.isJavaIdentifierStart(s.charAt(0)))) {
189             return false;
190         }
191         for (int i = 1; i < s.length(); i++) {
192             if (!Character.isJavaIdentifierPart(s.charAt(i))) {
193                 return false;
194             }
195         }
196         return true;
197     }
198
199     private boolean isJavaKeyword(String JavaDoc s) {
200         for (int i = 0; i < keywords.length; i++) {
201             if (s.equals(keywords[i])) {
202                 return true;
203             }
204         }
205         return false;
206     }
207
208     class UnableToParseException extends DebuggerException {
209         public UnableToParseException(String JavaDoc expr) {
210             super("The following expression cannot be currently parsed: " + expr);
211         }
212     }
213 }
214
Popular Tags