@Override public int size() { return nameSpace.getVariableNames().length; }
/** 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 */); }
/** */ public void unsetVariable( String name ) { super.unsetVariable( name ); externalMap.remove( name ); }
/** Get the specified variable in this namespace. * @param name the name * @param recurse If recurse is true then we recursively search through * parent namespaces for the variable. * <p> * Note: this method is primarily intended for use internally. If you * use this method outside of the bsh package you will have to use * Primitive.unwrap() to get primitive values. * @return The variable value or Primitive.VOID if it is not defined. * @throws UtilEvalError the util eval error * @see Primitive#unwrap(Object) */ public Object getVariable(final String name, final boolean recurse) throws UtilEvalError { final Variable var = this.getVariableImpl(name, recurse); return this.unwrapVariable(var); }
/** * <p> * Get the specified variable or property in this namespace or a parent * namespace. * </p> * <p> * We first search for a variable name, and then a property. * </p> * @param name the name * @param interp the interp * @return The variable or property value or Primitive.VOID if neither is * defined. * @throws UtilEvalError the util eval error */ public Object getVariableOrProperty(final String name, final Interpreter interp) throws UtilEvalError { final Object val = this.getVariable(name, true); return val == Primitive.VOID ? this.getPropertyValue(name, interp) : val; }
@Override public Collection<Object> values() { final List<Object> result = new ArrayList<Object>(); for (final String name : nameSpace.getVariableNames()) try { result.add(nameSpace.get(name, interpreter)); } catch (UtilEvalError exc) { // ignore for now } return result; }
BshMethod bshMethod = null; try { bshMethod = namespace.getMethod( methodName, types, declaredOnly ); } catch ( UtilEvalError e ) { NameSpace ns = new NameSpace(namespace,namespace.getName()+" clone"); try { for( String varName : namespace.getVariableNames() ) { ns.setLocalVariable(varName,namespace.getVariable(varName,false),false); for( BshMethod method : namespace.getMethods() ) { ns.setMethod(method); return ns.getThis(declaringInterpreter); bshMethod = namespace.getMethod( "invoke", new Class [] { null, null } ); } catch ( UtilEvalError e ) { /*leave null*/ } " not found in bsh scripted object: "+ namespace.getName(), callerInfo, callstack );
This ths = thisNameSpace.getThis( interpreter ); thisNameSpace= ths.getNameSpace(); Object result = ths; result = classNameSpace.getThis( interpreter ); else result = classNameSpace.getClassInstance(); This ths = thisNameSpace.getSuper( interpreter ); thisNameSpace = ths.getNameSpace(); thisNameSpace.getParent() != null && thisNameSpace.getParent().isClass ths = thisNameSpace.getParent().getThis( interpreter ); obj = thisNameSpace.getGlobal( interpreter ); obj = thisNameSpace; else if (varName.equals("variables")) obj = thisNameSpace.getVariableNames(); else if (varName.equals("methods")) obj = thisNameSpace.getMethodNames(); else if ( varName.equals("interpreter") ) if ( lastEvalName.equals("this") ) obj = callstack.get( ++callstackDepth ).getThis( interpreter );
String packageName = enclosingNameSpace.getPackage(); String className = enclosingNameSpace.isClass ? (enclosingNameSpace.getName() + "$" + name) : name; String fqClassName = packageName == null ? className : packageName + "." + className; NameSpace classStaticNameSpace = new NameSpace(enclosingNameSpace, className); classStaticNameSpace.isClass = true; enclosingNameSpace.importClass(fqClassName.replace('$', '.')); classStaticNameSpace.setLocalVariable(ClassGeneratorUtil.BSHINIT, block, false/*strictJava*/); } catch (UtilEvalError e) { throw new InterpreterError("unable to init static: " + e); classStaticNameSpace.setClassStatic(genClass); try { LHS lhs = Reflect.getLHSStaticField(genClass, bshStaticFieldName); lhs.assign(classStaticNameSpace.getThis(visitor.getInterpreter()), false/*strict*/); } catch (Exception e) { throw new InterpreterError("Error in class gen setup: " + e);
instanceInitBlock = (BSHBlock) classStaticThis.getNameSpace().getVariable(BSHINIT); } catch (Exception e) { throw new InterpreterError("unable to get instance initializer: " + e); instanceNameSpace = classStaticThis.getNameSpace().copy(); instanceNameSpace.setParent(CONTEXT_NAMESPACE.get()); } else { instanceNameSpace = new NameSpace(classStaticThis.getNameSpace(), className); // todo: old code instanceThis = instanceNameSpace.getThis(interpreter); try { LHS lhs = Reflect.getLHSObjectField(instance, BSHTHIS + className); instanceNameSpace.setClassInstance(instance); try { BshMethod constructor = instanceNameSpace.getMethod(constructorName, sig, true/*declaredOnly*/);
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); }
NameSpace instanceNameSpace = classStaticThis.getNameSpace().copy(); if (CONTEXT_NAMESPACE.get() != null) instanceNameSpace.setParent(CONTEXT_NAMESPACE.get()); instanceThis = instanceNameSpace.getThis(CONTEXT_INTERPRETER.get()); else instanceThis = instanceNameSpace.getThis(classStaticThis.declaringInterpreter); try { LHS lhs = Reflect.getLHSObjectField(instance, BSHTHIS + className); instanceNameSpace.setClassInstance(instance); instanceInitBlock = (BSHBlock) classStaticThis.getNameSpace().getVariable(BSHINIT.toString()); } catch (Exception e) { throw new InterpreterError("unable to get instance initializer: " + e, e);
/*package*/ static void write(NameSpace ns, ObjectOutputStream s, Filter filter) throws IOException { final String[] vars = ns.getVariableNames(); for (int j = vars != null ? vars.length : 0; --j >= 0;) { final String nm = vars[j]; if (nm != null && !"bsh".equals(nm) && isVariableSerializable(nm)) { try { final Object val = ns.getVariable(nm, false); if ((val == null || (val instanceof Serializable) || (val instanceof Externalizable)) && !(val instanceof Component) && (filter == null || filter.accept(nm, val))) { final BshMethod[] mtds = ns.getMethods(); for (int j = mtds != null ? mtds.length : 0; --j >= 0;) { final String nm = mtds[j].getName();
/** Gets the method. * @param name the name * @param sig the sig * @return the method * @throws UtilEvalError the util eval error * @see #getMethod(String, Class [], boolean) * @see #getMethod(String, Class []) */ public BshMethod getMethod(final String name, final Class<?>[] sig) throws UtilEvalError { return this.getMethod(name, sig, false/* declaredOnly */); }
public Object constructWithInterfaceBody( BSHAllocationExpression node, Class type, Object[] args, BSHBlock body ) throws EvalError { NameSpace namespace = callstack.top(); NameSpace local = new NameSpace(namespace, "AnonymousBlock"); callstack.push(local); evalBlock(body, true); callstack.pop(); // statical import fields from the interface so that code inside // can refer to the fields directly (e.g. HEIGHT) local.importStatic( type ); return local.getThis(interpreter).getInterface( type ); }
/** 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 ); } }
super.unsetVariable( name ); var = super.getVariableImpl( name, recurse ); } else Variable localVar = super.getVariableImpl( name, false );
/** Get the specified variable in this namespace or a parent namespace. * <p> * Note: this method is primarily intended for use internally. If you use * this method outside of the bsh package you will have to use * Primitive.unwrap() to get primitive values. * @param name the name * @return The variable value or Primitive.VOID if it is not defined. * @throws UtilEvalError the util eval error * @see Primitive#unwrap(Object) */ public Object getVariable(final String name) throws UtilEvalError { return this.getVariable(name, true); }
public PreparsedScript(final String source, final ClassLoader classLoader) throws EvalError { final ClassManagerImpl classManager = new ClassManagerImpl(); classManager.setClassLoader(classLoader); final NameSpace nameSpace = new NameSpace("global", classManager); _interpreter = new Interpreter(new StringReader(""), System.out, System.err, false, nameSpace, null, null); try { final This callable = (This) _interpreter.eval("__execute() { " + source + "\n" + "}\n" + "return this;"); _method = callable.getNameSpace().getMethod("__execute", Reflect.ZERO_TYPES, false); } catch (final UtilEvalError e) { throw new IllegalStateException(e); } }
/** We have the variable: either it was declared here with a type, giving it block local scope or an untyped var was explicitly set here via setBlockVariable(). */ private boolean weHaveVar( String name ) { // super.variables.containsKey( name ) not any faster, I checked try { return super.getVariableImpl( name, false ) != null; } catch ( UtilEvalError e ) { return false; } }