public static LHS getLHSStaticField(Class clas, String fieldName) throws UtilEvalError, ReflectError { Field f = resolveExpectedJavaField(clas, fieldName, true/*onlystatic*/); return new LHS(f); }
private Object lhsUnaryOperation( LHS lhs, boolean strictJava ) throws UtilEvalError { Interpreter.debug("lhsUnaryOperation"); Object prevalue, postvalue; prevalue = lhs.getValue(); postvalue = unaryOperation(prevalue, kind); Object retVal; if ( postfix ) retVal = prevalue; else retVal = postvalue; lhs.assign( postvalue, strictJava ); return retVal; }
public Variable getVariable() { if ( null != var ) return this.var; if ( null != nameSpace ) this.var = Reflect.getVariable(nameSpace, getName()); else if ( isStatic() ) this.var = Reflect.getVariable(field.getDeclaringClass(), getName()); else this.var = Reflect.getVariable(object, getName()); return this.var; }
public Variable createVariable( String name, Class type, Object value, Modifiers mods ) { LHS lhs = new LHS( externalMap, name ); // Is this race condition worth worrying about? // value will appear in map before it's really in the interpreter try { lhs.assign( value, false/*strict*/ ); } catch ( UtilEvalError e) { throw new InterpreterError( e.toString() ); } return new Variable( name, type, lhs ); }
if ( operator != ASSIGN ) // assign doesn't need the pre-value try { lhsValue = lhs.getValue(); } catch ( UtilEvalError e ) { throw e.toEvalError( this, callstack ); if (lhs.isFinal()) { lhs.getVariable().setValue( rhs, Variable.ASSIGNMENT ); return rhs; return lhs.assign( rhs, strictJava ); return lhs.assign( operation(lhsValue, rhs, PLUS), strictJava ); return lhs.assign( operation(lhsValue, rhs, MINUS), strictJava ); return lhs.assign( operation(lhsValue, rhs, STAR), strictJava ); return lhs.assign( operation(lhsValue, rhs, SLASH), strictJava ); return lhs.assign( operation(lhsValue, rhs, BIT_AND), strictJava ); return lhs.assign( operation(lhsValue, rhs, BIT_OR), strictJava );
/** Overloaded assign with false as strict java. * @param val value to assign * @return result based on type * @throws UtilEvalError on exception */ public Object assign( Object val ) throws UtilEvalError { return this.assign(val, false); }
Object getValue() throws UtilEvalError { if ( lhs != null ) return lhs.getValue(); return value; }
if ( obj instanceof LHS && ((LHS) obj).isStatic() ) { namespace.setVariableImpl( ((LHS) obj).getVariable() ); return Primitive.VOID;
public boolean isFinal() { if ( getVariable() == null ) return false; return var.hasModifier("final"); }
/** Set the value of the typed variable. @param value should be an object or wrapped bsh Primitive type. if value is null the appropriate default value will be set for the type: e.g. false for boolean, zero for integer types. */ public void setValue( Object value, int context ) throws UtilEvalError { // check this.value if ( hasModifier("final") && this.value != null ) throw new UtilEvalError ("Final variable, can't re-assign."); if ( value == null ) value = Primitive.getDefaultValue( type ); if ( lhs != null ) { lhs.assign( value, false/*strictjava*/ ); return; } // TODO: should add isJavaCastable() test for strictJava // (as opposed to isJavaAssignable()) if ( type != null ) value = Types.castObject( value, type, context == DECLARATION ? Types.CAST : Types.ASSIGNMENT ); this.value= value; }
/** * Get an LHS reference to an object field. * <p/> * This method also deals with the field style property access. * In the field does not exist we check for a property setter. */ public static LHS getLHSObjectField(Object object, String fieldName) throws UtilEvalError, ReflectError { if (object instanceof This) { // I guess this is when we pass it as an argument? // Setting locally boolean recurse = false; return new LHS(((This) object).namespace, fieldName, recurse); } try { Field f = resolveExpectedJavaField(object.getClass(), fieldName, false/*staticOnly*/); return new LHS(object, f); } catch (ReflectError e) { // not a field, try property access if (hasObjectPropertySetter(object.getClass(), fieldName)) { return new LHS(object, fieldName); } else { throw e; } } }
public Object lhsUnaryOperation( LHS lhs, boolean strictJava ) throws UtilEvalError { if ( Interpreter.DEBUG ) Interpreter.debug("lhsUnaryOperation"); Object prevalue, postvalue; prevalue = lhs.getValue(); postvalue = unaryOperation(prevalue, kind); Object retVal; if ( postfix ) retVal = prevalue; else retVal = postvalue; lhs.assign( postvalue, strictJava ); return retVal; }
/** Set the value of the typed variable. @param value should be an object or wrapped bsh Primitive type. if value is null the appropriate default value will be set for the type: e.g. false for boolean, zero for integer types. */ public void setValue( Object value, int context ) throws UtilEvalError { // prevent final variable re-assign if (hasModifier("final")) { if (this.value != null) throw new UtilEvalError("Cannot re-assign final variable "+name+"."); if (value == null) return; } // TODO: should add isJavaCastable() test for strictJava // (as opposed to isJavaAssignable()) if ( type != null && type != Object.class && value != null ) { this.value = Types.castObject( value, type, context == DECLARATION ? Types.CAST : Types.ASSIGNMENT ); value = this.value; } this.value = value; if ( this.value == null && context != DECLARATION ) this.value = Primitive.getDefaultValue( type ); if ( lhs != null ) this.value = lhs.assign( this.value, false/*strictjava*/ ); }
public Class<?> getType() { if ( null != field ) return field.getReturnType(); if ( null != var ) return var.getType(); try { return Types.getType(getValue()); } catch ( UtilEvalError e ) { return null; } }
protected Variable getImportedVar( String name ) throws UtilEvalError { // Try object imports if ( importedObjects != null ) for(int i=0; i<importedObjects.size(); i++) { Object object = importedObjects.get(i); Class clas = object.getClass(); Field field = Reflect.resolveJavaField( clas, name, false/*onlyStatic*/ ); if ( field != null ) return new Variable( name, field.getType(), new LHS( object, field ) ); } // Try static imports if ( importedStatic!= null ) for(int i=0; i<importedStatic.size(); i++) { Class clas = importedStatic.get(i); Field field = Reflect.resolveJavaField( clas, name, true/*onlyStatic*/ ); if ( field != null ) return new Variable( name, field.getType(), new LHS( field ) ); } return null; }
if ( node.operator != ParserConstants.ASSIGN ) // assign doesn't need the pre-value try { lhsValue = lhs.getValue(); } catch ( UtilEvalError e ) { throw e.toEvalError( node, callstack ); return lhs.assign( rhs, strictJava ); return lhs.assign( BshInterpreterUtil.operation(node, lhsValue, rhs, ParserConstants.PLUS), strictJava ); return lhs.assign( BshInterpreterUtil.operation(node, lhsValue, rhs, ParserConstants.MINUS), strictJava ); return lhs.assign( BshInterpreterUtil.operation(node, lhsValue, rhs, ParserConstants.STAR), strictJava ); return lhs.assign( BshInterpreterUtil.operation(node, lhsValue, rhs, ParserConstants.SLASH), strictJava ); return lhs.assign( BshInterpreterUtil.operation(node, lhsValue, rhs, ParserConstants.BIT_AND), strictJava ); return lhs.assign( BshInterpreterUtil.operation(node, lhsValue, rhs, ParserConstants.BIT_OR), strictJava ); return lhs.assign( BshInterpreterUtil.operation(node, lhsValue, rhs, ParserConstants.XOR), strictJava );
/** Assign the value to the name. name may evaluate to anything assignable. e.g. a variable or field. */ public void set( String name, Object value ) throws EvalError { // map null to Primtive.NULL coming in... if ( value == null ) value = Primitive.NULL; CallStack callstack = new CallStack(); try { if ( Name.isCompound( name ) ) { LHS lhs = globalNameSpace.getNameResolver( name ).toLHS( callstack, this ); lhs.assign( value, false ); } else // optimization for common case globalNameSpace.setVariable( name, value, false ); } catch ( UtilEvalError e ) { throw e.toEvalError( SimpleNode.JAVACODE, callstack ); } }
else try { return ((LHS)obj).getValue(); } catch ( UtilEvalError e ) { throw e.toEvalError( node, callstack );