1 25 package com.sun.el; 26 27 import java.io.Externalizable ; 28 import java.io.IOException ; 29 import java.io.ObjectInput ; 30 import java.io.ObjectOutput ; 31 32 import javax.el.ELContext; 33 import javax.el.ELException; 34 import javax.el.ELResolver; 35 import javax.el.Expression; 36 import javax.el.ExpressionFactory; 37 import javax.el.FunctionMapper; 38 import javax.el.PropertyNotFoundException; 39 import javax.el.PropertyNotWritableException; 40 import javax.el.ValueExpression; 41 import javax.el.VariableMapper; 42 43 import com.sun.el.lang.ELSupport; 44 import com.sun.el.lang.EvaluationContext; 45 import com.sun.el.lang.ExpressionBuilder; 46 import com.sun.el.parser.AstLiteralExpression; 47 import com.sun.el.parser.Node; 48 import com.sun.el.util.ReflectionUtil; 49 50 98 public final class ValueExpressionImpl extends ValueExpression implements 99 Externalizable { 100 101 private Class expectedType; 102 103 private String expr; 104 105 private FunctionMapper fnMapper; 106 107 private VariableMapper varMapper; 108 109 private transient Node node; 110 111 public ValueExpressionImpl() { 112 113 } 114 115 118 public ValueExpressionImpl(String expr, Node node, FunctionMapper fnMapper, 119 VariableMapper varMapper, Class expectedType) { 120 this.expr = expr; 121 this.node = node; 122 this.fnMapper = fnMapper; 123 this.varMapper = varMapper; 124 this.expectedType = expectedType; 125 } 126 127 132 public boolean equals(Object obj) { 133 return (obj instanceof ValueExpressionImpl && obj.hashCode() == this 134 .hashCode()); 135 } 136 137 142 public Class getExpectedType() { 143 return this.expectedType; 144 } 145 146 156 public String getExpressionString() { 157 return this.expr; 158 } 159 160 164 private Node getNode() throws ELException { 165 if (this.node == null) { 166 this.node = ExpressionBuilder.createNode(this.expr); 167 } 168 return this.node; 169 } 170 171 176 public Class getType(ELContext context) throws PropertyNotFoundException, 177 ELException { 178 EvaluationContext ctx = new EvaluationContext(context, this.fnMapper, 179 this.varMapper); 180 return this.getNode().getType(ctx); 181 } 182 183 188 public Object getValue(ELContext context) throws PropertyNotFoundException, 189 ELException { 190 EvaluationContext ctx = new EvaluationContext(context, this.fnMapper, 191 this.varMapper); 192 Object value = this.getNode().getValue(ctx); 193 if (this.expectedType != null) { 194 return ELSupport.coerceToType(value, this.expectedType); 195 } 196 return value; 197 } 198 199 204 public int hashCode() { 205 return this.expr.hashCode(); 206 } 207 208 213 public boolean isLiteralText() { 214 try { 215 return this.getNode() instanceof AstLiteralExpression; 216 } catch (ELException ele) { 217 return false; 218 } 219 } 220 221 226 public boolean isReadOnly(ELContext context) 227 throws PropertyNotFoundException, ELException { 228 EvaluationContext ctx = new EvaluationContext(context, this.fnMapper, 229 this.varMapper); 230 return this.getNode().isReadOnly(ctx); 231 } 232 233 public void readExternal(ObjectInput in) throws IOException , 234 ClassNotFoundException { 235 this.expr = in.readUTF(); 236 String type = in.readUTF(); 237 if (!"".equals(type)) { 238 this.expectedType = ReflectionUtil.forName(type); 239 } 240 this.fnMapper = (FunctionMapper) in.readObject(); 241 this.varMapper = (VariableMapper) in.readObject(); 242 } 243 244 250 public void setValue(ELContext context, Object value) 251 throws PropertyNotFoundException, PropertyNotWritableException, 252 ELException { 253 EvaluationContext ctx = new EvaluationContext(context, this.fnMapper, 254 this.varMapper); 255 this.getNode().setValue(ctx, value); 256 } 257 258 public void writeExternal(ObjectOutput out) throws IOException { 259 out.writeUTF(this.expr); 260 out.writeUTF((this.expectedType != null) ? this.expectedType.getName() 261 : ""); 262 out.writeObject(this.fnMapper); 263 out.writeObject(this.varMapper); 264 } 265 266 public String toString() { 267 return "ValueExpression["+this.expr+"]"; 268 } 269 } 270 | Popular Tags |