1 /* gnu.classpath.tools.gjdoc.expr.Evaluator
2 Copyright (C) 2004, 2012 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 com.sun.javadoc.FieldDoc;
42 import java.io.StringReader;
43 import java.math.BigInteger;
44 import antlr.RecognitionException;
45 import antlr.TokenStreamException;
48 public class Evaluator
51 * Try to evaluate the given Java expression in the context of the
54 * @param expression the Java expression to evaluate. The
55 * expression string must not include a terminating semicolon.
57 * @param source the FieldDoc (part of) whose constant field value
58 * expression is being evaluated. Used to prevent circular
61 * @param environment callback hook used by the Evaluator to query
62 * the value of static fields referenced in the expression.
64 * @return a Java object representing the value of the expression,
65 * or <code>null</code> if the expression evaluates to
68 * @throws IllegalExpressionException if the expression is
69 * invalid, uses unsupported syntax constructs (e.g. method calls,
70 * array access) or references unknown static fields.
72 public static Object evaluate(String expression,
73 Set<FieldDoc> visitedFields,
74 EvaluatorEnvironment environment)
75 throws IllegalExpressionException
78 JavaLexer lexer = new JavaLexer(new StringReader(expression));
79 JavaRecognizer recognizer = new JavaRecognizer(lexer);
80 Expression e = recognizer.expression();
81 ConstantExpression value = e.evaluate(new Context(environment, visitedFields));
82 return value.asObject();
84 catch (RecognitionException e) {
85 throw new IllegalExpressionException(e);
87 catch (TokenStreamException e) {
88 throw new IllegalExpressionException(e);
92 static int parseInt(String stringValue)
96 if (stringValue.startsWith("0x")) {
98 stringValue = stringValue.substring(2);
100 else if (stringValue.length() > 1 && stringValue.startsWith("0")) {
102 stringValue = stringValue.substring(1);
104 while (stringValue.length() > 1 && stringValue.startsWith("0")) {
105 stringValue = stringValue.substring(1);
109 return Integer.parseInt(stringValue);
112 long result = Long.parseLong(stringValue, base);
114 if (result > Integer.MAX_VALUE) {
115 result -= 0x100000000L;
118 if (result > Integer.MAX_VALUE) {
119 throw new NumberFormatException(result + " > " + Integer.MAX_VALUE);
121 else if (result < Integer.MIN_VALUE) {
122 throw new NumberFormatException(result + " < " + Integer.MIN_VALUE);
130 static long parseLong(String stringValue)
134 if (stringValue.startsWith("0x")) {
136 stringValue = stringValue.substring(2);
138 else if (stringValue.length() > 1 && stringValue.startsWith("0")) {
140 stringValue = stringValue.substring(1);
142 while (stringValue.length() > 1 && stringValue.startsWith("0")) {
143 stringValue = stringValue.substring(1);
146 BigInteger bigInt = new BigInteger(stringValue, base);
147 long result = bigInt.longValue();