expr.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,371 行 · 第 1/3 页

JAVA
1,371
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.el;import com.caucho.config.types.Period;import com.caucho.util.BeanUtil;import com.caucho.util.IntMap;import com.caucho.util.L10N;import com.caucho.vfs.ReadStream;import com.caucho.vfs.WriteStream;import javax.el.ELContext;import javax.el.ELException;import javax.el.MethodInfo;import javax.el.PropertyNotFoundException;import javax.el.PropertyNotWritableException;import javax.el.ValueExpression;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.JspException;import java.io.IOException;import java.io.Reader;import java.io.Writer;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.math.BigDecimal;import java.math.BigInteger;import java.util.HashMap;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;/** * Abstract implementation class for an expression. */public abstract class Expr extends ValueExpression {  protected static final Logger log    = Logger.getLogger(Expr.class.getName());  protected static final L10N L = new L10N(Expr.class);  private static final Character NULL_CHAR    = new Character((char) 0);    private static final long DAY = 24L * 3600L * 1000L;  private static final BigDecimal BIG_DECIMAL_ZERO = new BigDecimal("0");  private static final BigInteger BIG_INTEGER_ZERO = new BigInteger("0");    private static final HashMap<Class,CoerceType> _coerceMap    = new HashMap<Class,CoerceType>();  // lexeme codes  final static int ADD = 1;  final static int SUB = ADD + 1;  final static int MUL = SUB + 1;  final static int DIV = MUL + 1;  final static int MOD = DIV + 1;  final static int EQ = MOD + 1;  final static int NE = EQ + 1;  final static int LT = NE + 1;  final static int LE = LT + 1;  final static int GT = LE + 1;  final static int GE = GT + 1;  final static int AND = GE + 1;  final static int OR = AND + 1;    final static int NOT = OR + 1;  final static int MINUS = NOT + 1;  final static int EMPTY = MINUS + 1;  final static int OBJECT = 0;  final static int BOOLEAN = OBJECT + 1;  final static int BYTE = BOOLEAN + 1;  final static int SHORT = BYTE + 1;  final static int INT = SHORT + 1;  final static int LONG = INT + 1;  final static int FLOAT = LONG + 1;  final static int DOUBLE = FLOAT + 1;    final static int BOOLEAN_OBJ = DOUBLE + 1;  final static int BYTE_OBJ = BOOLEAN_OBJ + 1;  final static int SHORT_OBJ = BYTE_OBJ + 1;  final static int INT_OBJ = SHORT_OBJ + 1;  final static int LONG_OBJ = INT_OBJ + 1;  final static int FLOAT_OBJ = LONG_OBJ + 1;  final static int DOUBLE_OBJ = FLOAT_OBJ + 1;    final static int STRING = DOUBLE_OBJ + 1;  final static IntMap _typeMap = new IntMap();  /**   * Returns true if the expression is constant.   */  public boolean isConstant()  {    return false;  }  /**   * Returns true if the expression is read-only.   */  public boolean isReadOnly(ELContext env)  {    return true;  }  /**   * Returns true if the expression is literal text   */  public boolean isLiteralText()  {    return false;  }  /**   * Creates a field reference using this expression as the base object.   *   * @param field the expression for the field.   */  public Expr createField(Expr field)  {    return new ArrayResolverExpr(this, field);  }  /**   * Creates a field reference using this expression as the base object.   *   * @param field the string reference for the field.   */  public Expr createField(String field)  {    return createField(new StringLiteral(field));  }  /**   * Creates a method call using this as the <code>obj.method</code>   * expression   *   * @param args the arguments for the method   */  public Expr createMethod(Expr []args)  {    return new FunctionExpr(this, args);  }  /**   * Evaluates the expression, returning an object.   *   * @param env the variable environment   *   * @return the value of the expression as an object   */  abstract public Object getValue(ELContext env)    throws ELException;  /**   * Evaluates the expression, returning an object.   *   * @param env the variable environment   *   * @return the value of the expression as an object   */  public MethodInfo getMethodInfo(ELContext env,				  Class<?> returnType,				  Class<?> []argTypes)    throws ELException  {    throw new ELException(L.l("'{0}' is an illegal method expression." + getClass(),			      toString()));  }  /**   * Evaluates the expression, returning an object.   *   * @param env the variable environment   *   * @return the value of the expression as an object   */  public Object invoke(ELContext env, Class<?> []argTypes, Object []args)    throws ELException  {    throw new ELException(L.l("'{0}' is an illegal method expression." + getClass(),			      toString()));  }  /**   * Evaluates the expression, returning an object.   *   * @param env the variable environment   *   * @return the value of the expression as an object   */  public final Object evalObject(ELContext env)    throws ELException  {    return getValue(env);  }  /**   * Evaluate the expression, knowing the value should be a boolean.   *   * @param env the variable environment   *   * @return the value of the expression as a boolean   */  public boolean evalBoolean(ELContext env)    throws ELException  {    return toBoolean(getValue(env), env);  }  /**   * Evaluate the expression, knowing the value should be a double.   *   * @param env the variable environment   *   * @return the value of the expression as a double   */  public double evalDouble(ELContext env)    throws ELException  {    return toDouble(getValue(env), env);  }  /**   * Evaluate the expression, knowing the value should be a long   *   * @param env the variable environment   *   * @return the value of the expression as a double   */  public long evalLong(ELContext env)    throws ELException  {    return toLong(getValue(env), env);  }  /**   * Evaluate the expression, knowing the value should be a string   *   * @param env the variable environment   *   * @return the value of the expression as a string   */  public String evalString(ELContext env)    throws ELException  {    return toString(getValue(env), env);  }  /**   * Evaluate the expression, knowing the value should be a string   *   * @param env the variable environment   *   * @return the value of the expression as a string   */  public String evalStringWithNull(ELContext env)    throws ELException  {    return toStringWithNull(getValue(env), env);  }  /**   * Evaluate the expression, knowing the value should be a string   *   * @param env the variable environment   *   * @return the value of the expression as a string   */  public char evalCharacter(ELContext env)    throws ELException  {    return toCharacter(getValue(env), env);  }  /**   * Evaluate the expression, knowing the value should be a period   *   * @param env the variable environment   *   * @return the value of the expression as a period   */  public long evalPeriod(ELContext env)    throws ELException  {    try {      Object obj = getValue(env);      if (obj instanceof Number)        return 1000L * ((Number) obj).longValue();      else        return Period.toPeriod(toString(obj, env));    } catch (Exception e) {      throw new ELException(e.getMessage());    }                          }  /**   * Evaluate the expression, knowing the value should be a BigInteger.   *   * @param env the variable environment   *   * @return the value of the expression as a BigInteger   */  public BigInteger evalBigInteger(ELContext env)    throws ELException  {    return toBigInteger(getValue(env), env);  }  /**   * Evaluate the expression, knowing the value should be a BigDecimal.   *   * @param env the variable environment   *   * @return the value of the expression as a BigDecimal   */  public BigDecimal evalBigDecimal(ELContext env)    throws ELException  {    return toBigDecimal(getValue(env), env);  }  /**   * Evaluates the expression, setting an object.   *   * @param env the variable environment   *   * @return the value of the expression as an object   */  @Override  public void setValue(ELContext env, Object value)    throws PropertyNotFoundException,	   PropertyNotWritableException,	   ELException  {    throw new PropertyNotWritableException(getClass().getName() + ": " + toString());  }  /**   * Evaluates directly to the output.  The method returns true   * if the default value should be printed instead.   *   * @param out the output writer   * @param env the variable environment   * @param escapeXml if true, escape reserved XML   *   * @return true if the object is null, otherwise false   */  public boolean print(WriteStream out,                       ELContext env,                       boolean escapeXml)    throws IOException, ELException  {    Object obj = getValue(env);    if (obj == null)      return true;    else if (escapeXml) {      toStreamEscaped(out, obj);      return false;    }    else {      toStream(out, obj);      return false;    }  }  /**   * Evaluates directly to the output.  The method returns true   * if the default value should be printed instead.   *   * @param out the output writer   * @param env the variable environment   * @param escapeXml if true, escape reserved XML   *   * @return true if the object is null, otherwise false   */  public boolean print(JspWriter out,                       ELContext env,                       boolean escapeXml)    throws IOException, ELException  {    //try {        Object obj = getValue(env);    if (obj == null)      return true;    else if (escapeXml) {      toStreamEscaped(out, obj);      return false;    }    else {      toStream(out, obj);      return false;    }          /*    } catch (ELException e) {      // jsp/3253            log.log(Level.WARNING, e.toString(), e);      return false;    }      */  }  /**   * Generates the code to regenerate the expression.   *   * @param os the stream to the *.java page   */  public void printCreate(WriteStream os)    throws IOException  {    throw new UnsupportedOperationException(getClass().getName());  }  //  // EL methods  //  @Override  public String getExpressionString()  {    return toString();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?