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.vfs.WriteStream;
033:
034: import javax.el.ELContext;
035: import javax.el.ELException;
036: import java.io.IOException;
037: import java.lang.reflect.Method;
038:
039: /**
040: * Represents a method call. The expr will evaluate to a method.
041: */
042: public class FunctionExpr extends Expr {
043: private Expr _expr;
044:
045: private Expr[] _args;
046:
047: /**
048: * Creates a new method expression.
049: *
050: * @param expr the expression generating the method to be called
051: * @param args the arguments for the method
052: */
053: public FunctionExpr(Expr expr, Expr[] args) {
054: _expr = expr;
055: _args = args;
056: }
057:
058: /**
059: * Evaluate the expression as an object.
060: *
061: * @param env the variable environment
062: */
063: @Override
064: public Object getValue(ELContext env) throws ELException {
065: if (_expr instanceof StaticMethodExpr)
066: return ((StaticMethodExpr) _expr).evalMethod(_args, env);
067:
068: Object aObj = _expr.getValue(env);
069:
070: Method method = null;
071:
072: if (aObj instanceof Method)
073: method = (Method) aObj;
074:
075: else if (aObj instanceof Method[]) {
076: Method[] methods = (Method[]) aObj;
077:
078: if (methods.length < _args.length)
079: return null;
080:
081: method = methods[_args.length];
082: }
083:
084: if (method == null) {
085: // jsp/18i7
086: throw new ELParseException(L.l(
087: "'{0}' is an unknown function.", _expr));
088: }
089:
090: Class[] params = method.getParameterTypes();
091:
092: if (params.length != _args.length) {
093: // jsp/18i8
094: throw new ELParseException(
095: L
096: .l(
097: "arguments '{0}' do not match expected length {1}.",
098: _expr, params.length));
099: }
100:
101: try {
102: Object[] objs = new Object[_args.length];
103:
104: for (int i = 0; i < params.length; i++)
105: objs[i] = MethodExpr.evalArg(params[i], _args[i], env);
106:
107: return method.invoke(null, objs);
108: } catch (ELException e) {
109: throw e;
110: } catch (Exception e) {
111: throw new ELException(e);
112: }
113: }
114:
115: /**
116: * Prints the code to create an LongLiteral.
117: */
118: @Override
119: public void printCreate(WriteStream os) throws IOException {
120: os.print("new com.caucho.el.FunctionExpr(");
121: _expr.printCreate(os);
122: os.print(", new com.caucho.el.Expr[] {");
123:
124: for (int i = 0; i < _args.length; i++) {
125: if (i != 0)
126: os.print(", ");
127: _args[i].printCreate(os);
128: }
129: os.println("})");
130: }
131:
132: /**
133: * Returns true for equal strings.
134: */
135: public boolean equals(Object o) {
136: if (!(o instanceof FunctionExpr))
137: return false;
138:
139: FunctionExpr expr = (FunctionExpr) o;
140:
141: if (!_expr.equals(expr._expr))
142: return false;
143:
144: if (_args.length != expr._args.length)
145: return false;
146:
147: for (int i = 0; i < _args.length; i++) {
148: if (!_args[i].equals(expr._args[i]))
149: return false;
150: }
151:
152: return true;
153: }
154:
155: public String toString() {
156: StringBuilder sb = new StringBuilder();
157:
158: sb.append(_expr);
159: sb.append('(');
160: for (int i = 0; i < _args.length; i++) {
161: if (i != 0)
162: sb.append(", ");
163:
164: sb.append(_args[i]);
165: }
166: sb.append(')');
167:
168: return sb.toString();
169: }
170: }
|