/** 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. @see Primitive#unwrap( Object ) @return The variable value or Primitive.VOID if it is not defined. */ public Object getVariable( String name ) throws UtilEvalError { return getVariable( name, true ); }
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))) {
return nameSpace.getVariable( varName );
/** 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); }
/** This is the invocation handler for the dynamic proxy. <p> Notes: Inner class for the invocation handler seems to shield this unavailable interface from JDK1.2 VM... I don't understand this. JThis works just fine even if those classes aren't there (doesn't it?) This class shouldn't be loaded if an XThis isn't instantiated in NameSpace.java, should it? */ class Handler implements InvocationHandler, java.io.Serializable { public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable { try { return invokeImpl( proxy, method, args ); } catch ( TargetError te ) { // Unwrap target exception. If the interface declares that // it throws the ex it will be delivered. If not it will be // wrapped in an UndeclaredThrowable // This isn't simple because unwrapping this loses all context info. // So rewrap is better than unwrap. - fschmidt Throwable t = te.getTarget(); Class<? extends Throwable> c = t.getClass(); String msg = t.getMessage(); try {
/** * <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; }
protected boolean contains(String name) { try { return _ip.getNameSpace().getVariable(name) != Primitive.VOID; //Primitive.VOID means not defined } catch (UtilEvalError ex) { throw UiException.Aide.wrap(ex); } }
/** Set the global namespace for this interpreter. <p> Note: This is here for completeness. If you're using this a lot it may be an indication that you are doing more work than you have to. For example, caching the interpreter instance rather than the namespace should not add a significant overhead. No state other than the debug status is stored in the interpreter. <p> All features of the namespace can also be accessed using the interpreter via eval() and the script variable 'this.namespace' (or global.namespace as necessary). */ public void setNameSpace( NameSpace globalNameSpace ) { this.globalNameSpace = globalNameSpace; if ( null != globalNameSpace ) try { if ( ! (globalNameSpace.getVariable("bsh") instanceof This) ) { initRootSystemObject(); if ( interactive ) loadRCFiles(); } } catch (final UtilEvalError e) { throw new IllegalStateException(e); } }
/** */ public static Object getObjectFieldValue(Object object, String fieldName) throws UtilEvalError, ReflectError { if (object instanceof This) { return ((This) object).namespace.getVariable(fieldName); } else if (object == Primitive.NULL) { //noinspection ThrowableInstanceNeverThrown throw new UtilTargetError(new NullPointerException("Attempt to access field '" + fieldName + "' on null value")); } else { try { return getFieldValue(object.getClass(), object, fieldName, false/*onlystatic*/); } catch (ReflectError e) { // no field, try property acces if (hasObjectPropertyGetter(object.getClass(), fieldName)) { return getObjectProperty(object, fieldName); } else { throw e; } } } }
/** * Check for a field with the given name in a java object or scripted object * if the field exists fetch the value, if not check for a property value. * If neither is found return Primitive.VOID. */ public static Object getObjectFieldValue( Object object, String fieldName ) throws UtilEvalError, ReflectError { if ( object instanceof This ) { return ((This) object).namespace.getVariable( fieldName ); } else if( object == Primitive.NULL ) { throw new UtilTargetError( new NullPointerException( "Attempt to access field '" +fieldName+"' on null value" ) ); } else { try { return getFieldValue( object.getClass(), object, fieldName, false/*onlystatic*/); } catch ( ReflectError e ) { // no field, try property access if ( hasObjectPropertyGetter( object.getClass(), fieldName ) ) return getObjectProperty( object, fieldName ); else throw e; } } }
protected boolean contains(Scope scope, String name) { 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 { return bshns.getVariable(name) != Primitive.VOID; } catch (UtilEvalError ex) { throw UiException.Aide.wrap(ex); } } } return contains(name); }
protected Object get(Scope scope, String name) { 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 { return unwrap(bshns.getVariable(name)); } catch (UtilEvalError ex) { throw UiException.Aide.wrap(ex); } } } return get(name); }
/** Lazy initialize static context implementation. * Called from <clinit> after static This was populated we will now * proceed to evaluate the static block node. * @param genClass the generated class. * @param className name of the class. * @throws UtilEvalError combined exceptions. */ public static void initStatic(Class<?> genClass) throws UtilEvalError { String className = genClass.getSimpleName(); try { This staticThis = Reflect.getClassStaticThis(genClass, className); NameSpace classStaticNameSpace = staticThis.getNameSpace(); Interpreter interpreter = staticThis.declaringInterpreter; if (null == interpreter) throw new UtilEvalError("No namespace or interpreter for statitc This." +" Start interpreter for class not implemented yet."); //startInterpreterForClass(genClass); ??? BSHBlock block = (BSHBlock) classStaticNameSpace.getVariable(BSHINIT.toString()); CallStack callstack = new CallStack(classStaticNameSpace); // evaluate the static portion of the block in the static space block.evalBlock(callstack, interpreter, true/*override*/, CLASSSTATIC); // Validate that static final variables were set for (Variable var : Reflect.getVariables(classStaticNameSpace)) var.validateFinalIsSet(true); } catch (Exception e) { throw new UtilEvalError("Exception in static init block <clinit> for class " + className + ". With message: " + e.getMessage(), e); } }
public Object getValue() throws UtilEvalError { if ( type == VARIABLE ) return nameSpace.getVariable( varName ); if (type == FIELD) try { Object o = field.get( object ); return Primitive.wrap( o, field.getType() ); } catch(IllegalAccessException e2) { throw new UtilEvalError("Can't read field: " + field); } if ( type == PROPERTY ) try { return Reflect.getObjectProperty(object, propName); } catch(ReflectError e) { Interpreter.debug(e.getMessage()); throw new UtilEvalError("No such property: " + propName); } if ( type == INDEX ) try { return Reflect.getIndex(object, index); } catch(Exception e) { throw new UtilEvalError("Array access: " + e); } throw new InterpreterError("LHS type"); }
globalNameSpace = namespace; try { if ( ! (globalNameSpace.getVariable("bsh") instanceof This)) { initRootSystemObject();
instanceInitBlock = (BSHBlock) classStaticThis.getNameSpace().getVariable(BSHINIT.toString()); } catch (Exception e) { throw new InterpreterError("unable to get instance initializer: " + e, e);
Object cons = classStaticThis.getNameSpace().getVariable(BSHCONSTRUCTORS.toString()); if (cons == Primitive.VOID) throw new InterpreterError("Unable to find constructors array in class");
instanceInitBlock = (BSHBlock) classStaticThis.getNameSpace().getVariable(BSHINIT); } catch (Exception e) { throw new InterpreterError("unable to get instance initializer: " + e);
DelayedEvalBshMethod[] constructors; try { constructors = (DelayedEvalBshMethod[]) classStaticThis.getNameSpace().getVariable(BSHCONSTRUCTORS); } catch (Exception e) { throw new InterpreterError("unable to get instance initializer: " + e);
dec.name, type, value, modifiers ); value = namespace.getVariable(dec.name); } catch ( UtilEvalError e ) { throw e.toEvalError( this, callstack );