Assign a value to the LHS.
( Object val, boolean strictJava )
| 187 | Assign a value to the LHS. |
| 188 | */ |
| 189 | public Object assign( Object val, boolean strictJava ) |
| 190 | throws UtilEvalError |
| 191 | { |
| 192 | if ( type == VARIABLE ) |
| 193 | { |
| 194 | // Set the variable in namespace according to localVar flag |
| 195 | if ( localVar ) |
| 196 | nameSpace.setLocalVariable( varName, val, strictJava ); |
| 197 | else |
| 198 | nameSpace.setVariable( varName, val, strictJava ); |
| 199 | } else |
| 200 | if ( type == FIELD ) |
| 201 | { |
| 202 | try { |
| 203 | // This should probably be in Reflect.java |
| 204 | Reflect.setAccessible(field); |
| 205 | field.set( object, Primitive.unwrap(val)); |
| 206 | return val; |
| 207 | } |
| 208 | catch( NullPointerException e) { |
| 209 | throw new UtilEvalError( |
| 210 | "LHS ("+field.getName()+") not a static field.",e); |
| 211 | } |
| 212 | catch( IllegalAccessException e2) { |
| 213 | throw new UtilEvalError( |
| 214 | "LHS ("+field.getName()+") can't access field: "+e2,e2); |
| 215 | } |
| 216 | catch( IllegalArgumentException e3) |
| 217 | { |
| 218 | String type = val instanceof Primitive ? |
| 219 | ((Primitive)val).getType().getName() |
| 220 | : val.getClass().getName(); |
| 221 | throw new UtilEvalError( |
| 222 | "Argument type mismatch. " + (val == null ? "null" : type ) |
| 223 | + " not assignable to field "+field.getName()); |
| 224 | } |
| 225 | } |
| 226 | else |
| 227 | if ( type == PROPERTY ) |
| 228 | { |
| 229 | CollectionManager cm = CollectionManager.getCollectionManager(); |
| 230 | if ( cm.isMap( object ) ) |
| 231 | cm.putInMap( object/*map*/, propName, Primitive.unwrap(val) ); |
| 232 | else |
| 233 | try { |
| 234 | Reflect.setObjectProperty(object, propName, val); |
| 235 | } |
| 236 | catch(ReflectError e) { |
| 237 | Interpreter.debug("Assignment: " + e.getMessage()); |
| 238 | throw new UtilEvalError("No such property: " + propName); |
| 239 | } |
| 240 | } else |
| 241 | if ( type == INDEX ) |
| 242 | try { |
| 243 | Reflect.setIndex(object, index, val); |
| 244 | } catch ( UtilTargetError e1 ) { // pass along target error |
| 245 | throw e1; |
| 246 | } catch ( Exception e ) { |
no test coverage detected