KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.el.MethodInfo;
37 import javax.el.PropertyNotFoundException;
38 import java.io.IOException JavaDoc;
39 import java.lang.reflect.InvocationTargetException JavaDoc;
40 import java.lang.reflect.Method JavaDoc;
41
42 /**
43  * Represents an array reference:
44  *
45  * <pre>
46  * a[b]
47  * </pre>
48  */

49 public class ArrayResolverExpr extends Expr {
50   private Expr _left;
51   private Expr _right;
52   
53   // cached getter method
54
private Class JavaDoc _lastClass;
55   private String JavaDoc _lastField;
56   private Method JavaDoc _lastMethod;
57
58   /**
59    * Creates a new array expression.
60    *
61    * @param left the object expression
62    * @param right the index expression.
63    */

64   public ArrayResolverExpr(Expr left, Expr right)
65   {
66     _left = left;
67     _right = right;
68   }
69
70   /**
71    * Returns the base expression.
72    */

73   public Expr getExpr()
74   {
75     return _left;
76   }
77
78   /**
79    * Returns the index expression.
80    */

81   public Expr getIndex()
82   {
83     return _right;
84   }
85
86   /**
87    * Creates a method for constant arrays.
88    */

89   @Override JavaDoc
90   public Expr createMethod(Expr []args)
91   {
92     if (! (_right instanceof StringLiteral))
93       return null;
94
95     StringLiteral literal = (StringLiteral) _right;
96
97     return new MethodExpr(_left, literal.getValue(), args);
98   }
99   
100   /**
101    * Evaluate the expression as an object.
102    *
103    * @param env the variable environment
104    *
105    * @return the evaluated object
106    */

107   @Override JavaDoc
108   public Object JavaDoc getValue(ELContext env)
109     throws ELException
110   {
111     Object JavaDoc aObj = _left.getValue(env);
112
113     if (aObj == null)
114       return null;
115
116     Object JavaDoc fieldObj = _right.getValue(env);
117     if (fieldObj == null)
118       return null;
119
120     return env.getELResolver().getValue(env, aObj, fieldObj);
121   }
122   
123   /**
124    * Returns the read-only value of the expression.
125    *
126    * @param env the variable environment
127    *
128    * @return true if read-only
129    */

130   @Override JavaDoc
131   public boolean isReadOnly(ELContext env)
132     throws ELException
133   {
134     Object JavaDoc aObj = _left.getValue(env);
135
136     if (aObj == null)
137       return true;
138
139     Object JavaDoc fieldObj = _right.getValue(env);
140     if (fieldObj == null)
141       return true;
142
143     return env.getELResolver().isReadOnly(env, aObj, fieldObj);
144   }
145   
146   /**
147    * Evaluate the expression as an object.
148    *
149    * @param env the variable environment
150    *
151    * @return the evaluated object
152    */

153   @Override JavaDoc
154   public void setValue(ELContext env, Object JavaDoc value)
155     throws ELException
156   {
157     Object JavaDoc aObj = _left.getValue(env);
158
159     if (aObj == null)
160       throw new PropertyNotFoundException(L.l("'{0}' is null in '{1}'",
161                           _left.toString(), toString()));
162                           
163
164     Object JavaDoc fieldObj = _right.getValue(env);
165     if (fieldObj == null)
166       throw new PropertyNotFoundException(L.l("'{0}' is null in '{1}'",
167                           _right.toString(), toString()));
168
169     env.getELResolver().setValue(env, aObj, fieldObj, value);
170   }
171
172   /**
173    * Evaluates the expression, returning an object.
174    *
175    * @param env the variable environment
176    *
177    * @return the value of the expression as an object
178    */

179   @Override JavaDoc
180   public MethodInfo getMethodInfo(ELContext env,
181                   Class JavaDoc<?> returnType,
182                   Class JavaDoc<?> []argTypes)
183     throws ELException
184   {
185     Object JavaDoc base = _left.getValue(env);
186
187     if (base == null)
188       throw new ELException(L.l("'{0}' is an illegal method expression.",
189                 toString()));
190
191     return new MethodInfo(_right.evalString(env),
192               returnType,
193               argTypes);
194   }
195
196   /**
197    * Evaluates the expression, returning an object.
198    *
199    * @param env the variable environment
200    *
201    * @return the value of the expression as an object
202    */

203   @Override JavaDoc
204   public Object JavaDoc invoke(ELContext env, Class JavaDoc<?> []argTypes, Object JavaDoc []args)
205     throws ELException
206   {
207     Object JavaDoc base = _left.getValue(env);
208
209     if (base == null)
210       throw new ELException(L.l("'{0}' is an illegal method expression.",
211                 toString()));
212
213     String JavaDoc name = _right.evalString(env);
214
215     try {
216       Method JavaDoc method = base.getClass().getMethod(name, argTypes);
217
218       return method.invoke(base, args);
219     } catch (NoSuchMethodException JavaDoc e) {
220       throw new ELException(e);
221     } catch (IllegalAccessException JavaDoc e) {
222       throw new ELException(e);
223     } catch (InvocationTargetException JavaDoc e) {
224       throw new ELException(e.getCause());
225     }
226   }
227
228   /**
229    * Prints the code to create an LongLiteral.
230    *
231    * @param os stream to the generated *.java code
232    */

233   @Override JavaDoc
234   public void printCreate(WriteStream os)
235     throws IOException JavaDoc
236   {
237     os.print("new com.caucho.el.ArrayResolverExpr(");
238     _left.printCreate(os);
239     os.print(", ");
240     _right.printCreate(os);
241     os.print(")");
242   }
243
244   /**
245    * Returns true for equal strings.
246    */

247   public boolean equals(Object JavaDoc o)
248   {
249     if (! (o instanceof ArrayResolverExpr))
250       return false;
251
252     ArrayResolverExpr expr = (ArrayResolverExpr) o;
253
254     return (_left.equals(expr._left) && _right.equals(expr._right));
255   }
256
257   /**
258    * Returns a readable representation of the expr.
259    */

260   public String JavaDoc toString()
261   {
262     return _left + "[" + _right + "]";
263   }
264 }
265
Popular Tags