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