KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > expr > Expression


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.debugger.jpda.expr;
21
22 import java.io.StringReader JavaDoc;
23 import java.util.Random JavaDoc;
24
25 /**
26  * Represents an pre-parsed Java expression to be later evaluated in a specific JVM context.
27  * The expression can have a 1.4 or 1.5 syntax.
28  *
29  * @author Maros Sandor
30  */

31 public class Expression {
32
33     public static final String JavaDoc LANGUAGE_JAVA_1_4 = JavaParser.LANGUAGE_JAVA_1_4;
34     public static final String JavaDoc LANGUAGE_JAVA_1_5 = JavaParser.LANGUAGE_JAVA_1_5;
35     
36     private static final String JavaDoc REPLACE_return = "return01234";
37     private static final String JavaDoc REPLACE_class = "class01234";
38
39     private String JavaDoc strExpression;
40     private String JavaDoc language;
41     private SimpleNode root;
42     private String JavaDoc replace_return;
43     private String JavaDoc replace_class;
44
45     /**
46      * Creates a new expression by pre-parsing the given String representation of the expression.
47      *
48      * @param expr textual representation of an expression
49      * @param language one of the LANGUAGE_XXX constants
50      * @return pre-parsed Java expression
51      * @throws ParseException if the expression has wrong syntax
52      */

53     public static Expression parse (String JavaDoc expr, String JavaDoc language)
54     throws ParseException {
55         String JavaDoc replace_return = REPLACE_return;
56         while (expr.indexOf(replace_return) >= 0) {
57             replace_return = "return" + new Random JavaDoc().nextLong(); // NOI18N
58
}
59         String JavaDoc replace_class = REPLACE_class;
60         while (expr.indexOf(replace_class) >= 0) {
61             replace_class = "class" + new Random JavaDoc().nextLong(); // NOI18N
62
}
63         String JavaDoc replacedExpr = replaceSpecialVar(expr, "return", replace_return); // NOI18N
64
replacedExpr = replaceSpecialVar(replacedExpr, "class", replace_class); // NOI18N
65
StringReader JavaDoc reader = new StringReader JavaDoc(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 JavaDoc e) {
72             throw new ParseException(e.getMessage());
73         } finally {
74             reader.close();
75         }
76     }
77     
78     private static String JavaDoc replaceSpecialVar(String JavaDoc expr, String JavaDoc var, String JavaDoc 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 JavaDoc expression, String JavaDoc language, SimpleNode root,
112                        String JavaDoc replace_return, String JavaDoc 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     /**
121      * Creates an evaluator engine that can be used to evaluate this expression in a given
122      * runtime JVM context.
123      *
124      * @param context a runtime JVM context
125      * @return the evaluator engine
126      */

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 JavaDoc getLanguage() {
136         return language;
137     }
138
139     public String JavaDoc getExpression() {
140         return strExpression;
141     }
142     
143     String JavaDoc returnReplaced() {
144         return replace_return;
145     }
146     
147     String JavaDoc classReplaced() {
148         return replace_class;
149     }
150 }
151
Popular Tags