24 |
import java.math.BigInteger; |
import java.math.BigInteger; |
25 |
import antlr.RecognitionException; |
import antlr.RecognitionException; |
26 |
import antlr.TokenStreamException; |
import antlr.TokenStreamException; |
27 |
|
import java.util.Set; |
28 |
|
|
29 |
public class Evaluator |
public class Evaluator |
30 |
{ |
{ |
35 |
* @param expression the Java expression to evaluate. The |
* @param expression the Java expression to evaluate. The |
36 |
* expression string must not include a terminating semicolon. |
* expression string must not include a terminating semicolon. |
37 |
* |
* |
38 |
|
* @param source the FieldDoc (part of) whose constant field value |
39 |
|
* expression is being evaluated. Used to prevent circular |
40 |
|
* references. |
41 |
|
* |
42 |
* @param environment callback hook used by the Evaluator to query |
* @param environment callback hook used by the Evaluator to query |
43 |
* the value of static fields referenced in the expression. |
* the value of static fields referenced in the expression. |
44 |
* |
* |
51 |
* array access) or references unknown static fields. |
* array access) or references unknown static fields. |
52 |
*/ |
*/ |
53 |
public static Object evaluate(String expression, |
public static Object evaluate(String expression, |
54 |
|
Set visitedFields, |
55 |
EvaluatorEnvironment environment) |
EvaluatorEnvironment environment) |
56 |
throws IllegalExpressionException |
throws IllegalExpressionException |
57 |
{ |
{ |
59 |
JavaLexer lexer = new JavaLexer(new StringReader(expression)); |
JavaLexer lexer = new JavaLexer(new StringReader(expression)); |
60 |
JavaRecognizer recognizer = new JavaRecognizer(lexer); |
JavaRecognizer recognizer = new JavaRecognizer(lexer); |
61 |
Expression e = recognizer.expression(); |
Expression e = recognizer.expression(); |
62 |
ConstantExpression value = e.evaluate(new Context(environment)); |
ConstantExpression value = e.evaluate(new Context(environment, visitedFields)); |
63 |
return value.asObject(); |
return value.asObject(); |
64 |
} |
} |
65 |
catch (RecognitionException e) { |
catch (RecognitionException e) { |