1 19 20 package org.netbeans.modules.debugger.jpda.expr; 21 22 import java.io.StringReader ; 23 import java.util.Random ; 24 25 31 public class Expression { 32 33 public static final String LANGUAGE_JAVA_1_4 = JavaParser.LANGUAGE_JAVA_1_4; 34 public static final String LANGUAGE_JAVA_1_5 = JavaParser.LANGUAGE_JAVA_1_5; 35 36 private static final String REPLACE_return = "return01234"; 37 private static final String REPLACE_class = "class01234"; 38 39 private String strExpression; 40 private String language; 41 private SimpleNode root; 42 private String replace_return; 43 private String replace_class; 44 45 53 public static Expression parse (String expr, String language) 54 throws ParseException { 55 String replace_return = REPLACE_return; 56 while (expr.indexOf(replace_return) >= 0) { 57 replace_return = "return" + new Random ().nextLong(); } 59 String replace_class = REPLACE_class; 60 while (expr.indexOf(replace_class) >= 0) { 61 replace_class = "class" + new Random ().nextLong(); } 63 String replacedExpr = replaceSpecialVar(expr, "return", replace_return); replacedExpr = replaceSpecialVar(replacedExpr, "class", replace_class); StringReader reader = new StringReader (replacedExpr); 66 try { 67 JavaParser parser = new JavaParser(reader); 68 parser.setTargetJDK(language); 69 SimpleNode root = parser.Expression(); 70 return new Expression(expr, language, root, replace_return, replace_class); 71 } catch (Error e) { 72 throw new ParseException(e.getMessage()); 73 } finally { 74 reader.close(); 75 } 76 } 77 78 private static String replaceSpecialVar(String expr, String var, String replace_var) { 79 int i = expr.indexOf(var); 80 while (i >= 0) { 81 boolean replace; 82 if (i > 0) { 83 char ch = expr.charAt(i - 1); 84 if (Character.isJavaIdentifierStart(ch) || 85 Character.isJavaIdentifierPart(ch) || 86 ch == '.') { 87 replace = false; 88 } else { 89 replace = true; 90 } 91 } else { 92 replace = true; 93 } 94 if (replace && i < (expr.length() - var.length())) { 95 char ch = expr.charAt(i + var.length()); 96 if (Character.isJavaIdentifierPart(ch)) { 97 replace = false; 98 } 99 } 100 if (replace) { 101 expr = expr.substring(0, i) + replace_var + expr.substring(i + var.length()); 102 i += replace_var.length(); 103 } else { 104 i += var.length(); 105 } 106 i = expr.indexOf(var, i); 107 } 108 return expr; 109 } 110 111 private Expression(String expression, String language, SimpleNode root, 112 String replace_return, String replace_class) { 113 strExpression = expression; 114 this.language = language; 115 this.root = root; 116 this.replace_return = replace_return; 117 this.replace_class = replace_class; 118 } 119 120 127 public Evaluator evaluator(EvaluationContext context) { 128 return new Evaluator(this, context); 129 } 130 131 SimpleNode getRoot() { 132 return root; 133 } 134 135 public String getLanguage() { 136 return language; 137 } 138 139 public String getExpression() { 140 return strExpression; 141 } 142 143 String returnReplaced() { 144 return replace_return; 145 } 146 147 String classReplaced() { 148 return replace_class; 149 } 150 } 151 | Popular Tags |