KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.lang.reflect.Modifier JavaDoc;
39
40 /**
41  * Represents a named method call on an object.
42  */

43 public class MethodExpr extends Expr {
44   private Expr _expr;
45   private String JavaDoc _methodName;
46   
47   private Expr []_args;
48
49   /**
50    * Creates a new method expression.
51    *
52    * @param expr the expression generating the object on which the method
53    * is to be called
54    * @param methodName the name of the method to call
55    * @param args the arguments for the method
56    */

57   public MethodExpr(Expr expr, String JavaDoc methodName, Expr []args)
58   {
59     _expr = expr;
60     _methodName = methodName;
61     _args = args;
62   }
63   
64   /**
65    * Evaluate the expression as an object.
66    *
67    * @param env the variable environment
68    */

69   @Override JavaDoc
70   public Object JavaDoc getValue(ELContext env)
71     throws ELException
72   {
73     Object JavaDoc aObj = _expr.getValue(env);
74
75     if (aObj == null)
76       return null;
77
78     Object JavaDoc []objs = new Object JavaDoc[_args.length];
79
80     try {
81       Method JavaDoc method = findMethod(aObj.getClass());
82
83       if (method != null) {
84         Class JavaDoc []params = method.getParameterTypes();
85     
86         for (int j = 0; j < params.length; j++) {
87           objs[j] = evalArg(params[j], _args[j], env);
88         }
89
90         // XXX: probably should look for the interface instead.
91
try {
92           method.setAccessible(true);
93         } catch (Throwable JavaDoc e) {
94         }
95         
96         return method.invoke(aObj, objs);
97       }
98       
99       return null;
100     } catch (Exception JavaDoc e) {
101       return invocationError(e);
102     }
103   }
104   
105   private Method JavaDoc findMethod(Class JavaDoc type)
106   {
107     if (type == null)
108       return null;
109     
110     if (Modifier.isPublic(type.getModifiers())) {
111       Method JavaDoc []methods = type.getDeclaredMethods();
112       
113       for (int i = 0; i < methods.length; i++) {
114         Method JavaDoc method = methods[i];
115         
116         if (! Modifier.isPublic(method.getModifiers()))
117           continue;
118         
119         Class JavaDoc []params = method.getParameterTypes();
120         
121         if (method.getName().equals(_methodName) &&
122             params.length == _args.length)
123           return method;
124       }
125     }
126     
127     Class JavaDoc []interfaces = type.getInterfaces();
128     for (int i = 0; i < interfaces.length; i++) {
129       Method JavaDoc method = findMethod(interfaces[i]);
130       if (method != null)
131         return method;
132     }
133     
134     return findMethod(type.getSuperclass());
135   }
136   
137   static Object JavaDoc evalArg(Class JavaDoc cl, Expr expr, ELContext env)
138     throws ELException
139   {
140     Marshall marshall = Marshall.create(cl);
141
142     return marshall.marshall(expr, env);
143   }
144   
145   /**
146    * Prints the code to create an LongLiteral.
147    */

148   @Override JavaDoc
149   public void printCreate(WriteStream os)
150     throws IOException JavaDoc
151   {
152     os.print("new com.caucho.el.MethodExpr(");
153     _expr.printCreate(os);
154     os.print(", \"");
155     os.print(_methodName);
156     os.print("\", new com.caucho.el.Expr[] {");
157     
158     for (int i = 0; i < _args.length; i++) {
159       if (i != 0)
160         os.print(", ");
161       _args[i].printCreate(os);
162     }
163     os.println("})");
164   }
165   
166   /**
167    * Returns true for equal strings.
168    */

169   public boolean equals(Object JavaDoc o)
170   {
171     if (! (o instanceof MethodExpr))
172       return false;
173     
174     MethodExpr expr = (MethodExpr) o;
175     
176     if (! _expr.equals(expr._expr))
177       return false;
178     
179     if (! _methodName.equals(expr._methodName))
180       return false;
181     
182     if (_args.length != expr._args.length)
183       return false;
184     
185     for (int i = 0; i < _args.length; i++) {
186       if (! _args[i].equals(expr._args[i]))
187         return false;
188     }
189     
190     return true;
191   }
192   
193   /**
194    * Returns the printed version.
195    */

196   public String JavaDoc toString()
197   {
198     return "MethodExpr[" + _expr + "," + _methodName + "]";
199   }
200 }
201
Popular Tags