KickJava   Java API By Example, From Geeks To Geeks.

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


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.config.types.Signature;
33 import com.caucho.vfs.WriteStream;
34
35 import javax.el.ELContext;
36 import javax.el.ELException;
37 import java.io.IOException JavaDoc;
38 import java.lang.reflect.Method JavaDoc;
39 import java.util.logging.Level JavaDoc;
40
41 /**
42  * Represents a method call. The expr will evaluate to a method.
43  */

44 public class StaticMethodExpr extends Expr {
45   private Method JavaDoc _method;
46   private Marshall []_marshall;
47   private boolean _isVoid;
48
49   /**
50    * Creates a new method expression.
51    *
52    * @param expr the expression generating the method to be called
53    * @param args the arguments for the method
54    */

55   public StaticMethodExpr(Method JavaDoc method)
56   {
57     _method = method;
58
59     initMethod();
60   }
61
62   /**
63    * Creates a new static method.
64    *
65    * @param signature signature
66    */

67   public StaticMethodExpr(String JavaDoc signature)
68   {
69     try {
70       Signature sig = new Signature();
71       sig.addText(signature);
72       sig.init();
73
74       _method = sig.getMethod();
75     } catch (Exception JavaDoc e) {
76       log.log(Level.FINE, e.toString(), e);
77     }
78     
79     initMethod();
80   }
81
82   /**
83    * Initialize the marshall arguments.
84    */

85   private void initMethod()
86   {
87     Class JavaDoc []param = _method.getParameterTypes();
88
89     _marshall = new Marshall[param.length];
90
91     for (int i = 0; i < _marshall.length; i++) {
92       _marshall[i] = Marshall.create(param[i]);
93     }
94
95     _isVoid = void.class.equals(_method.getReturnType());
96   }
97   
98   /**
99    * Evaluate the expression as an object.
100    *
101    * @param env the variable environment
102    */

103   @Override JavaDoc
104   public Object JavaDoc getValue(ELContext env)
105     throws ELException
106   {
107     return _method;
108   }
109   
110   /**
111    * Evaluate the expression as an object.
112    *
113    * @param env the variable environment
114    */

115   public Object JavaDoc evalMethod(Expr []args,
116                ELContext env)
117     throws ELException
118   {
119     if (_marshall.length != args.length) {
120       // jsp/18i8
121
throw new ELParseException(L.l("Arguments to '{0}' do not match expected length {1}.", _method.getName(), _marshall.length));
122     }
123
124     try {
125       Object JavaDoc []objs = new Object JavaDoc[args.length];
126       
127       for (int i = 0; i < _marshall.length; i++)
128         objs[i] = _marshall[i].marshall(args[i], env);
129
130       if (! _isVoid)
131     return _method.invoke(null, objs);
132       else {
133     _method.invoke(null, objs);
134
135     return null;
136       }
137     } catch (ELException e) {
138       throw e;
139     } catch (Exception JavaDoc e) {
140       throw new ELException(e);
141     }
142   }
143
144   /**
145    * Prints the code to create an LongLiteral.
146    */

147   public void printCreate(WriteStream os)
148     throws IOException JavaDoc
149   {
150     os.print("new com.caucho.el.StaticMethodExpr(\"");
151     printType(os, _method.getReturnType());
152     os.print(" ");
153     os.print(_method.getDeclaringClass().getName());
154     os.print(".");
155     os.print(_method.getName());
156     os.print("(");
157     Class JavaDoc []parameterTypes = _method.getParameterTypes();
158     
159     for (int i = 0; i < parameterTypes.length; i++) {
160       if (i != 0)
161         os.print(", ");
162       printType(os, parameterTypes[i]);
163     }
164     os.print(")");
165     os.print("\")");
166   }
167
168   private void printType(WriteStream os, Class JavaDoc cl)
169     throws IOException JavaDoc
170   {
171     if (cl.isArray()) {
172       printType(os, cl.getComponentType());
173       os.print("[]");
174     }
175     else
176       os.print(cl.getName());
177   }
178
179   /**
180    * Returns true for equal strings.
181    */

182   public boolean equals(Object JavaDoc o)
183   {
184     if (! (o instanceof StaticMethodExpr))
185       return false;
186
187     StaticMethodExpr expr = (StaticMethodExpr) o;
188
189     return _method.equals(expr._method);
190   }
191
192   public String JavaDoc toString()
193   {
194     return _method.getName();
195   }
196 }
197
Popular Tags