1 /* gnu.classpath.tools.gjdoc.expr.Evaluator
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.classpath.tools.gjdoc.expr;
40 import java.io.StringReader;
41 import java.math.BigInteger;
42 import antlr.RecognitionException;
43 import antlr.TokenStreamException;
46 public class Evaluator
49 * Try to evaluate the given Java expression in the context of the
52 * @param expression the Java expression to evaluate. The
53 * expression string must not include a terminating semicolon.
55 * @param source the FieldDoc (part of) whose constant field value
56 * expression is being evaluated. Used to prevent circular
59 * @param environment callback hook used by the Evaluator to query
60 * the value of static fields referenced in the expression.
62 * @return a Java object representing the value of the expression,
63 * or <code>null</code> if the expression evaluates to
66 * @throws IllegalExpressionException if the expression is
67 * invalid, uses unsupported syntax constructs (e.g. method calls,
68 * array access) or references unknown static fields.
70 public static Object evaluate(String expression,
72 EvaluatorEnvironment environment)
73 throws IllegalExpressionException
76 JavaLexer lexer = new JavaLexer(new StringReader(expression));
77 JavaRecognizer recognizer = new JavaRecognizer(lexer);
78 Expression e = recognizer.expression();
79 ConstantExpression value = e.evaluate(new Context(environment, visitedFields));
80 return value.asObject();
82 catch (RecognitionException e) {
83 throw new IllegalExpressionException(e);
85 catch (TokenStreamException e) {
86 throw new IllegalExpressionException(e);
90 static int parseInt(String stringValue)
94 if (stringValue.startsWith("0x")) {
96 stringValue = stringValue.substring(2);
98 else if (stringValue.length() > 1 && stringValue.startsWith("0")) {
100 stringValue = stringValue.substring(1);
102 while (stringValue.length() > 1 && stringValue.startsWith("0")) {
103 stringValue = stringValue.substring(1);
107 return Integer.parseInt(stringValue);
110 long result = Long.parseLong(stringValue, base);
112 if (result > Integer.MAX_VALUE) {
113 result -= 0x100000000L;
116 if (result > Integer.MAX_VALUE) {
117 throw new NumberFormatException(result + " > " + Integer.MAX_VALUE);
119 else if (result < Integer.MIN_VALUE) {
120 throw new NumberFormatException(result + " < " + Integer.MIN_VALUE);
128 static long parseLong(String stringValue)
132 if (stringValue.startsWith("0x")) {
134 stringValue = stringValue.substring(2);
136 else if (stringValue.length() > 1 && stringValue.startsWith("0")) {
138 stringValue = stringValue.substring(1);
140 while (stringValue.length() > 1 && stringValue.startsWith("0")) {
141 stringValue = stringValue.substring(1);
144 BigInteger bigInt = new BigInteger(stringValue, base);
145 long result = bigInt.longValue();