1 33 34 35 package bsh; 36 37 import java.lang.reflect.Field ; 38 import java.util.Hashtable ; 39 40 53 class LHS implements ParserConstants, java.io.Serializable 54 { 55 NameSpace nameSpace; 56 57 boolean localVar; 58 59 62 static final int 63 VARIABLE = 0, 64 FIELD = 1, 65 PROPERTY = 2, 66 INDEX = 3, 67 METHOD_EVAL = 4; 68 69 int type; 70 71 String varName; 72 String propName; 73 Field field; 74 Object object; 75 int index; 76 77 80 LHS( NameSpace nameSpace, String varName ) 81 { 82 throw new Error ("namespace lhs"); 83 88 } 89 90 96 LHS( NameSpace nameSpace, String varName, boolean localVar ) 97 { 98 type = VARIABLE; 99 this.localVar = localVar; 100 this.varName = varName; 101 this.nameSpace = nameSpace; 102 } 103 104 108 LHS( Field field ) 109 { 110 type = FIELD; 111 this.object = null; 112 this.field = field; 113 } 114 115 118 LHS( Object object, Field field ) 119 { 120 if ( object == null) 121 throw new NullPointerException ("constructed empty LHS"); 122 123 type = FIELD; 124 this.object = object; 125 this.field = field; 126 } 127 128 131 LHS( Object object, String propName ) 132 { 133 if(object == null) 134 throw new NullPointerException ("constructed empty LHS"); 135 136 type = PROPERTY; 137 this.object = object; 138 this.propName = propName; 139 } 140 141 144 LHS( Object array, int index ) 145 { 146 if(array == null) 147 throw new NullPointerException ("constructed empty LHS"); 148 149 type = INDEX; 150 this.object = array; 151 this.index = index; 152 } 153 154 public Object getValue() throws UtilEvalError 155 { 156 if ( type == VARIABLE ) 157 return nameSpace.getVariable( varName ); 158 159 if (type == FIELD) 160 try { 161 Object o = field.get( object ); 162 return Primitive.wrap( o, field.getType() ); 163 } catch(IllegalAccessException e2) { 164 throw new UtilEvalError("Can't read field: " + field); 165 } 166 167 if ( type == PROPERTY ) 168 try { 169 return Reflect.getObjectProperty(object, propName); 170 } 171 catch(ReflectError e) { 172 Interpreter.debug(e.getMessage()); 173 throw new UtilEvalError("No such property: " + propName); 174 } 175 176 if ( type == INDEX ) 177 try { 178 return Reflect.getIndex(object, index); 179 } 180 catch(Exception e) { 181 throw new UtilEvalError("Array access: " + e); 182 } 183 184 throw new InterpreterError("LHS type"); 185 } 186 187 190 public Object assign( Object val, boolean strictJava ) 191 throws UtilEvalError 192 { 193 if ( type == VARIABLE ) 194 { 195 if ( localVar ) 197 nameSpace.setLocalVariable( varName, val, strictJava ); 198 else 199 nameSpace.setVariable( varName, val, strictJava ); 200 } else 201 if ( type == FIELD ) 202 { 203 try { 204 Object fieldVal = val instanceof Primitive ? 205 ((Primitive)val).getValue() : val; 206 207 ReflectManager.RMSetAccessible( field ); 209 field.set( object, fieldVal ); 210 return val; 211 } 212 catch( NullPointerException e) { 213 throw new UtilEvalError( 214 "LHS ("+field.getName()+") not a static field."); 215 } 216 catch( IllegalAccessException e2) { 217 throw new UtilEvalError( 218 "LHS ("+field.getName()+") can't access field: "+e2); 219 } 220 catch( IllegalArgumentException e3) 221 { 222 String type = val instanceof Primitive ? 223 ((Primitive)val).getType().getName() 224 : val.getClass().getName(); 225 throw new UtilEvalError( 226 "Argument type mismatch. " + (val == null ? "null" : type ) 227 + " not assignable to field "+field.getName()); 228 } 229 } 230 else 231 if ( type == PROPERTY ) 232 { 233 237 CollectionManager cm = CollectionManager.getCollectionManager(); 238 if ( cm.isMap( object ) ) 239 cm.putInMap( object, propName, val ); 240 else 241 try { 242 Reflect.setObjectProperty(object, propName, val); 243 } 244 catch(ReflectError e) { 245 Interpreter.debug("Assignment: " + e.getMessage()); 246 throw new UtilEvalError("No such property: " + propName); 247 } 248 } else 249 if ( type == INDEX ) 250 try { 251 Reflect.setIndex(object, index, val); 252 } catch ( UtilTargetError e1 ) { throw e1; 254 } catch ( Exception e ) { 255 throw new UtilEvalError("Assignment: " + e.getMessage()); 256 } 257 else 258 throw new InterpreterError("unknown lhs"); 259 260 return val; 261 } 262 263 public String toString() { 264 return "LHS: " 265 +((field!=null)? "field = "+field.toString():"") 266 +(varName!=null ? " varName = "+varName: "") 267 +(nameSpace!=null ? " nameSpace = "+nameSpace.toString(): ""); 268 } 269 } 270 271 | Popular Tags |