KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > el > FunctionExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.el;
31
32 import com.caucho.vfs.WriteStream;
33
34 import javax.el.ELContext;
35 import javax.el.ELException;
36 import java.io.IOException JavaDoc;
37 import java.lang.reflect.Method JavaDoc;
38
39 /**
40  * Represents a method call. The expr will evaluate to a method.
41  */

42 public class FunctionExpr extends Expr {
43   private Expr _expr;
44   
45   private Expr []_args;
46
47   /**
48    * Creates a new method expression.
49    *
50    * @param expr the expression generating the method to be called
51    * @param args the arguments for the method
52    */

53   public FunctionExpr(Expr expr, Expr []args)
54   {
55     _expr = expr;
56     _args = args;
57   }
58   
59   /**
60    * Evaluate the expression as an object.
61    *
62    * @param env the variable environment
63    */

64   @Override JavaDoc
65   public Object JavaDoc getValue(ELContext env)
66     throws ELException
67   {
68     if (_expr instanceof StaticMethodExpr)
69       return ((StaticMethodExpr) _expr).evalMethod(_args, env);
70     
71     Object JavaDoc aObj = _expr.getValue(env);
72
73     Method JavaDoc method = null;
74     
75     if (aObj instanceof Method JavaDoc)
76       method = (Method JavaDoc) aObj;
77     
78     else if (aObj instanceof Method JavaDoc[]) {
79       Method JavaDoc []methods = (Method JavaDoc []) aObj;
80
81       if (methods.length < _args.length)
82         return null;
83       
84       method = methods[_args.length];
85     }
86
87     if (method == null) {
88       // jsp/18i7
89
throw new ELParseException(L.l("'{0}' is an unknown function.", _expr));
90     }
91
92     Class JavaDoc []params = method.getParameterTypes();
93     
94     if (params.length != _args.length) {
95       // jsp/18i8
96
throw new ELParseException(L.l("arguments '{0}' do not match expected length {1}.", _expr, params.length));
97     }
98
99     try {
100       Object JavaDoc []objs = new Object JavaDoc[_args.length];
101       
102       for (int i = 0; i < params.length; i++)
103         objs[i] = MethodExpr.evalArg(params[i], _args[i], env);
104
105       return method.invoke(null, objs);
106     } catch (ELException e) {
107       throw e;
108     } catch (Exception JavaDoc e) {
109       throw new ELException(e);
110     }
111   }
112
113   /**
114    * Prints the code to create an LongLiteral.
115    */

116   @Override JavaDoc
117   public void printCreate(WriteStream os)
118     throws IOException JavaDoc
119   {
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 JavaDoc o)
136   {
137     if (! (o instanceof FunctionExpr))
138       return false;
139
140     FunctionExpr expr = (FunctionExpr) o;
141
142     if (! _expr.equals(expr._expr))
143       return false;
144
145     if (_args.length != expr._args.length)
146       return false;
147
148     for (int i = 0; i < _args.length; i++) {
149       if (! _args[i].equals(expr._args[i]))
150         return false;
151     }
152
153     return true;
154   }
155
156   public String JavaDoc toString()
157   {
158     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
159
160     sb.append(_expr);
161     sb.append('(');
162     for (int i = 0; i < _args.length; i++) {
163       if (i != 0)
164     sb.append(", ");
165
166       sb.append(_args[i]);
167     }
168     sb.append(')');
169
170     return sb.toString();
171   }
172 }
173
Popular Tags