1 22 23 package org.aspectj.debugger.base; 24 25 import com.sun.jdi.*; 26 import java.math.*; 27 28 import org.aspectj.compiler.base.*; 30 import org.aspectj.compiler.base.parser.*; 31 32 40 41 public class Evaluator { 42 43 private Debugger debugger; 44 45 public final static String [] 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 o, StackFrame frame) 105 throws NoVMException, DebuggerException { 106 String 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 valueString) 117 throws NoVMException, DebuggerException { 118 if (valueString.startsWith("\"")) { 119 if (valueString.length() > 1) { 120 String 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 e) { 141 } 142 try { 143 return vm().mirrorOf(Long.parseLong(valueString)); 144 } catch (NumberFormatException e) { 145 } 146 try { 147 return vm().mirrorOf(Float.parseFloat(valueString)); 148 } catch (NumberFormatException e) { 149 } 150 try { 151 return vm().mirrorOf(Double.parseDouble(valueString)); 152 } catch (NumberFormatException e) { 153 } 154 } 155 throw new UnableToParseException(valueString); 156 } 157 158 public LocalVariable getLocalVariable(Object o, StackFrame frame) 159 throws NoVMException, DebuggerException { 160 String 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 s) { 173 try { 174 new BigDecimal(s); 175 return true; 176 } catch (NumberFormatException e) { 177 } 178 return false; 179 } 180 181 private boolean isIdent(String 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 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 expr) { 210 super("The following expression cannot be currently parsed: " + expr); 211 } 212 } 213 } 214 | Popular Tags |