001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.el;
031:
032: import com.caucho.util.L10N;
033:
034: import javax.el.ELContext;
035: import javax.el.ELException;
036: import javax.el.MethodExpression;
037: import javax.el.MethodInfo;
038: import javax.el.MethodNotFoundException;
039: import javax.el.PropertyNotFoundException;
040: import java.util.logging.Logger;
041:
042: /**
043: * Implementation of the method expression.
044: */
045: public class MethodExpressionImpl extends MethodExpression implements
046: java.io.Serializable {
047: protected static final Logger log = Logger
048: .getLogger(MethodExpressionImpl.class.getName());
049: protected static final L10N L = new L10N(MethodExpressionImpl.class);
050:
051: private final String _expressionString;
052: private final Expr _expr;
053: private final Class _expectedType;
054: private final Class[] _expectedArgs;
055:
056: // XXX: for serialization
057: public MethodExpressionImpl() {
058: _expressionString = "";
059: _expr = null;
060: _expectedType = null;
061: _expectedArgs = null;
062: }
063:
064: public MethodExpressionImpl(Expr expr, String expressionString,
065: Class<?> expectedType, Class<?>[] expectedArgs) {
066: if (expectedArgs == null)
067: throw new NullPointerException();
068:
069: _expr = expr;
070: _expressionString = expressionString;
071: _expectedType = expectedType;
072: _expectedArgs = expectedArgs;
073: }
074:
075: public boolean isLiteralText() {
076: return _expr.isLiteralText();
077: }
078:
079: public String getExpressionString() {
080: return _expressionString;
081: }
082:
083: public MethodInfo getMethodInfo(ELContext context)
084: throws PropertyNotFoundException, MethodNotFoundException,
085: ELException {
086: return _expr.getMethodInfo(context, _expectedType,
087: _expectedArgs);
088: }
089:
090: public Object invoke(ELContext context, Object[] params)
091: throws PropertyNotFoundException, MethodNotFoundException,
092: ELException {
093: if (params == null && _expectedArgs.length != 0
094: || params != null
095: && params.length != _expectedArgs.length) {
096: throw new IllegalArgumentException(
097: L
098: .l(
099: "'{0}' expected arguments ({1}) do not match actual arguments ({2})",
100: _expr.toString(),
101: _expectedArgs.length,
102: (params != null ? params.length : 0)));
103: }
104:
105: Object value = _expr.invoke(context, _expectedArgs, params);
106:
107: return Expr.coerceToType(value, _expectedType);
108: }
109:
110: public int hashCode() {
111: return _expr.hashCode();
112: }
113:
114: public boolean equals(Object o) {
115: if (this == o)
116: return true;
117: else if (!(o instanceof MethodExpressionImpl))
118: return false;
119:
120: MethodExpressionImpl expr = (MethodExpressionImpl) o;
121:
122: return _expr.equals(expr._expr);
123: }
124:
125: public String toString() {
126: return "MethodExpressionImpl[" + getExpressionString() + "]";
127: }
128: }
|