/** Set an untyped variable in the block namespace. The BlockNameSpace would normally delegate this set to the parent. Typed variables are naturally set locally. This is used in try/catch block argument. */ public void setBlockVariable( String name, Object value ) throws UtilEvalError { super.setVariable( name, value, false/*strict?*/, false ); }
/** Set a variable explicitly in the local scope. * @param name the name * @param value the value * @param strictJava the strict java * @return the variable * @throws UtilEvalError the util eval error */ public Variable setLocalVariable(final String name, final Object value, final boolean strictJava) throws UtilEvalError { return this.setVariable(name, value, strictJava, false/* recurse */); }
/** Set a variable explicitly in the local scope. */ void setLocalVariable( String name, Object value, boolean strictJava ) throws UtilEvalError { setVariable( name, value, strictJava, false/*recurse*/ ); }
/** Set an untyped variable in the block namespace. The BlockNameSpace would normally delegate this set to the parent. Typed variables are naturally set locally. This is used in try/catch block argument. */ public void setBlockVariable( String name, Object value ) throws UtilEvalError { super.setVariable( name, value, false/*strict?*/, false ); }
/** Set the variable through this namespace. * <p> * Note: this method is primarily intended for use internally. If you use * this method outside of the bsh package and wish to set variables with * primitive values you will have to wrap them using bsh.Primitive. * </p> * @param name the name * @param value the value * @param strictJava specifies whether strict java rules are applied. * @throws UtilEvalError the util eval error * @see bsh.Primitive * <p> * Setting a new variable (which didn't exist before) or removing a * variable causes a namespace change. </p>*/ public void setVariable(final String name, final Object value, final boolean strictJava) throws UtilEvalError { this.setVariable(name, value, strictJava, true); }
/** Set the variable through this namespace. This method obeys the LOCALSCOPING property to determine how variables are set. <p> Note: this method is primarily intended for use internally. If you use this method outside of the bsh package and wish to set variables with primitive values you will have to wrap them using bsh.Primitive. @see bsh.Primitive <p> Setting a new variable (which didn't exist before) or removing a variable causes a namespace change. @param strictJava specifies whether strict java rules are applied. */ public void setVariable( String name, Object value, boolean strictJava ) throws UtilEvalError { // if localscoping switch follow strictJava, else recurse boolean recurse = Interpreter.LOCALSCOPING ? strictJava : true; setVariable( name, value, strictJava, recurse ); }
/** Shared system object visible under bsh.system */ public static void setShutdownOnExit(final boolean value) { try { SYSTEM_OBJECT.getNameSpace().setVariable("shutdownOnExit", Boolean.valueOf(value), false); } catch (final UtilEvalError utilEvalError) { throw new IllegalStateException(utilEvalError); } }
/** */ void setVariable( String name, Object value, boolean strictJava, boolean recurse ) throws UtilEvalError { super.setVariable( name, value, strictJava, recurse ); putExternalMap( name, value ); }
@Override public Object put(String key, Object value) { final Object result = get(key); try { nameSpace.setVariable(key, value, false); } catch (UtilEvalError e) { // ignore } return result; }
public static void setShutdownOnExit(final boolean value) { try { SYSTEM_OBJECT.getNameSpace().setVariable("shutdownOnExit", Boolean.valueOf(value), false); } catch (final UtilEvalError utilEvalError) { throw new IllegalStateException(utilEvalError); } }
/** Override the standard namespace behavior to make assignments happen in our parent (enclosing) namespace, unless the variable has already been assigned here via a typed declaration or through the special setBlockVariable() (used for untyped args in try/catch). <p> i.e. only allow typed var declaration to happen in this namespace. Typed vars are handled in the ordinary way local scope. All untyped assignments are delegated to the enclosing context. */ /* Note: it may see like with the new 1.3 scoping this test could be removed, but it cannot. When recurse is false we still need to set the variable in our parent, not here. */ public void setVariable( String name, Object value, boolean strictJava, boolean recurse ) throws UtilEvalError { if ( weHaveVar( name ) ) // set the var here in the block namespace super.setVariable( name, value, strictJava, false ); else // set the var in the enclosing (parent) namespace getParent().setVariable( name, value, strictJava, recurse ); }
/** Override the standard namespace behavior to make assignments happen in our parent (enclosing) namespace, unless the variable has already been assigned here via a typed declaration or through the special setBlockVariable() (used for untyped args in try/catch). <p> i.e. only allow typed var declaration to happen in this namespace. Typed vars are handled in the ordinary way local scope. All untyped assignments are delegated to the enclosing context. */ /* Note: it may see like with the new 1.3 scoping this test could be removed, but it cannot. When recurse is false we still need to set the variable in our parent, not here. */ public Variable setVariable( String name, Object value, boolean strictJava, boolean recurse ) throws UtilEvalError { if ( weHaveVar( name ) ) // set the var here in the block namespace return super.setVariable( name, value, strictJava, false ); else // set the var in the enclosing (parent) namespace return getParent().setVariable( name, value, strictJava, recurse ); }
Interpreter i = new Interpreter(); NameSpace ns = i.getNameSpace(); ns.setVariable("args", new String[]{"param1", "param2"}, false); i.source("Target.bsh");
protected void set(Scope scope, String name, Object val) { if (scope != null) { final NameSpace bshns = prepareNS(scope); //note: we have to create NameSpace (with prepareNS) //to have the correct chain if (bshns != _bshns) { try { bshns.setVariable(name, val != null ? val : Primitive.NULL, false); return; } catch (UtilEvalError ex) { throw UiException.Aide.wrap(ex); } } } set(name, val); }
for (Map.Entry<String, Object> me : _vars.entrySet()) { try { _bshns.setVariable(me.getKey(), me.getValue(), false); } catch (Throwable ex) { log.warn("Ignored failure of set " + me.getKey(), ex);
break; //no more ns.setVariable(nm, s.readObject(), false); } catch (IOException ex) { throw ex;
public Object invoke(final Map<String, ?> context) throws EvalError { final NameSpace nameSpace = new NameSpace("BeanshellExecutable", _interpreter.getClassManager()); nameSpace.setParent(_interpreter.getNameSpace()); final BshMethod method = new BshMethod(_method.getName(), _method.getReturnType(), _method.getParameterNames(), _method.getParameterTypes(), _method.getParameterModifiers(), _method.methodBody, nameSpace, _method.getModifiers()); for (final Map.Entry<String, ?> entry : context.entrySet()) { try { final Object value = entry.getValue(); nameSpace.setVariable(entry.getKey(), value != null ? value : Primitive.NULL, false); } catch (final UtilEvalError e) { throw new EvalError("cannot set variable '" + entry.getKey() + '\'', null, null, e); } } final Object result = method.invoke(Reflect.ZERO_ARGS, _interpreter); if ( Types.getType(result) == Void.TYPE ) return null; return Primitive.unwrap(result); }
public Object invoke(final Map<String, ?> context) throws EvalError { final NameSpace nameSpace = new NameSpace(_interpreter.getClassManager(), "BeanshellExecutable"); nameSpace.setParent(_interpreter.getNameSpace()); final BshMethod method = new BshMethod(_method.getName(), _method.getReturnType(), _method.getParameterNames(), _method.getParameterTypes(), _method.methodBody, nameSpace, _method.getModifiers()); for (final Map.Entry<String, ?> entry : context.entrySet()) { try { nameSpace.setVariable(entry.getKey(), entry.getValue(), false); } catch (final UtilEvalError e) { throw new EvalError("cannot set variable '" + entry.getKey() + '\'', null, null, e); } } final Object result = method.invoke(new Object[0], new BshEvaluatingVisitor(null, _interpreter)); if (result instanceof Primitive) { if (( (Primitive) result).getType() == Void.TYPE) { return null; } return ( (Primitive) result).getValue(); } return result; }
/** 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 ); } }
/** 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( new BshEvaluatingVisitor(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 ); } }