1 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 ; 39 import java.lang.reflect.InvocationTargetException ; 40 import java.lang.reflect.Method ; 41 42 49 public class ArrayResolverExpr extends Expr { 50 private Expr _left; 51 private Expr _right; 52 53 private Class _lastClass; 55 private String _lastField; 56 private Method _lastMethod; 57 58 64 public ArrayResolverExpr(Expr left, Expr right) 65 { 66 _left = left; 67 _right = right; 68 } 69 70 73 public Expr getExpr() 74 { 75 return _left; 76 } 77 78 81 public Expr getIndex() 82 { 83 return _right; 84 } 85 86 89 @Override 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 107 @Override 108 public Object getValue(ELContext env) 109 throws ELException 110 { 111 Object aObj = _left.getValue(env); 112 113 if (aObj == null) 114 return null; 115 116 Object fieldObj = _right.getValue(env); 117 if (fieldObj == null) 118 return null; 119 120 return env.getELResolver().getValue(env, aObj, fieldObj); 121 } 122 123 130 @Override 131 public boolean isReadOnly(ELContext env) 132 throws ELException 133 { 134 Object aObj = _left.getValue(env); 135 136 if (aObj == null) 137 return true; 138 139 Object fieldObj = _right.getValue(env); 140 if (fieldObj == null) 141 return true; 142 143 return env.getELResolver().isReadOnly(env, aObj, fieldObj); 144 } 145 146 153 @Override 154 public void setValue(ELContext env, Object value) 155 throws ELException 156 { 157 Object 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 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 179 @Override 180 public MethodInfo getMethodInfo(ELContext env, 181 Class <?> returnType, 182 Class <?> []argTypes) 183 throws ELException 184 { 185 Object 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 203 @Override 204 public Object invoke(ELContext env, Class <?> []argTypes, Object []args) 205 throws ELException 206 { 207 Object 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 name = _right.evalString(env); 214 215 try { 216 Method method = base.getClass().getMethod(name, argTypes); 217 218 return method.invoke(base, args); 219 } catch (NoSuchMethodException e) { 220 throw new ELException(e); 221 } catch (IllegalAccessException e) { 222 throw new ELException(e); 223 } catch (InvocationTargetException e) { 224 throw new ELException(e.getCause()); 225 } 226 } 227 228 233 @Override 234 public void printCreate(WriteStream os) 235 throws IOException 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 247 public boolean equals(Object 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 260 public String toString() 261 { 262 return _left + "[" + _right + "]"; 263 } 264 } 265 | Popular Tags |