1 17 18 package org.apache.el; 19 20 import java.io.Externalizable ; 21 import java.io.IOException ; 22 import javax.el.ELContext; 23 import javax.el.PropertyNotWritableException; 24 25 import java.io.ObjectInput ; 26 import java.io.ObjectOutput ; 27 28 import javax.el.ValueExpression; 29 30 import org.apache.el.lang.ELSupport; 31 import org.apache.el.util.MessageFactory; 32 import org.apache.el.util.ReflectionUtil; 33 34 35 public final class ValueExpressionLiteral extends ValueExpression implements 36 Externalizable { 37 38 private static final long serialVersionUID = 1L; 39 40 private Object value; 41 42 private Class expectedType; 43 44 public ValueExpressionLiteral() { 45 super(); 46 } 47 48 public ValueExpressionLiteral(Object value, Class expectedType) { 49 this.value = value; 50 this.expectedType = expectedType; 51 } 52 53 public Object getValue(ELContext context) { 54 if (this.expectedType != null) { 55 return ELSupport.coerceToType(this.value, this.expectedType); 56 } 57 return this.value; 58 } 59 60 public void setValue(ELContext context, Object value) { 61 throw new PropertyNotWritableException(MessageFactory.get( 62 "error.value.literal.write", this.value)); 63 } 64 65 public boolean isReadOnly(ELContext context) { 66 return true; 67 } 68 69 public Class getType(ELContext context) { 70 return (this.value != null) ? this.value.getClass() : null; 71 } 72 73 public Class getExpectedType() { 74 return this.expectedType; 75 } 76 77 public String getExpressionString() { 78 return (this.value != null) ? this.value.toString() : null; 79 } 80 81 public boolean equals(Object obj) { 82 return (obj instanceof ValueExpressionLiteral && this 83 .equals((ValueExpressionLiteral) obj)); 84 } 85 86 public boolean equals(ValueExpressionLiteral ve) { 87 return (ve != null && (this.value != null && ve.value != null && (this.value == ve.value || this.value 88 .equals(ve.value)))); 89 } 90 91 public int hashCode() { 92 return (this.value != null) ? this.value.hashCode() : 0; 93 } 94 95 public boolean isLiteralText() { 96 return true; 97 } 98 99 public void writeExternal(ObjectOutput out) throws IOException { 100 out.writeObject(this.value); 101 out.writeUTF((this.expectedType != null) ? this.expectedType.getName() 102 : ""); 103 } 104 105 public void readExternal(ObjectInput in) throws IOException , 106 ClassNotFoundException { 107 this.value = in.readObject(); 108 String type = in.readUTF(); 109 if (!"".equals(type)) { 110 this.expectedType = ReflectionUtil.forName(type); 111 } 112 } 113 } 114 | Popular Tags |