Esempio n. 1
0
  /**
   * Set the value of a the variable 'name' through this namespace. The variable may be an existing
   * or non-existing variable. It may live in this namespace or in a parent namespace if recurse is
   * true.
   *
   * <p>Note: This method is not public and does *not* know about LOCALSCOPING. Its caller methods
   * must set recurse intelligently in all situations (perhaps based on LOCALSCOPING).
   *
   * <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.
   * @param recurse determines whether we will search for the variable in our parent's scope before
   *     assigning locally.
   */
  void setVariable(String name, Object value, boolean strictJava, boolean recurse)
      throws UtilEvalError {
    ensureVariables();

    // primitives should have been wrapped
    if (value == null) throw new InterpreterError("null variable value");

    // Locate the variable definition if it exists.
    Variable existing = getVariableImpl(name, recurse);

    // Found an existing variable here (or above if recurse allowed)
    if (existing != null) {
      try {
        existing.setValue(value, Variable.ASSIGNMENT);
      } catch (UtilEvalError e) {
        throw new UtilEvalError("Variable assignment: " + name + ": " + e.getMessage());
      }
    } else
    // No previous variable definition found here (or above if recurse)
    {
      if (strictJava)
        throw new UtilEvalError("(Strict Java mode) Assignment to undeclared variable: " + name);

      // If recurse, set global untyped var, else set it here.
      // NameSpace varScope = recurse ? getGlobal() : this;
      // This modification makes default allocation local
      NameSpace varScope = this;

      varScope.variables.put(name, new Variable(name, value, null /*modifiers*/));

      // nameSpaceChanged() on new variable addition
      nameSpaceChanged();
    }
  }
Esempio n. 2
0
  /** subsequent imports override earlier ones */
  public void importPackage(String name) {
    if (importedPackages == null) importedPackages = new ArrayList<String>();

    // If it exists, remove it and add it at the end (avoid memory leak)
    importedPackages.remove(name);

    importedPackages.add(name);
    nameSpaceChanged();
  }
Esempio n. 3
0
  public void importStatic(Class clas) {
    if (importedStatic == null) importedStatic = new ArrayList<Class>();

    // If it exists, remove it and add it at the end (avoid memory leak)
    importedStatic.remove(clas);

    importedStatic.add(clas);
    nameSpaceChanged();
  }
Esempio n. 4
0
  /*
  	Note: this impor pattern is becoming common... could factor it out into
  	an importedObject Vector class.
  */
  public void importObject(Object obj) {
    if (importedObjects == null) importedObjects = new ArrayList<Object>();

    // If it exists, remove it and add it at the end (avoid memory leak)
    importedObjects.remove(obj);

    importedObjects.add(obj);
    nameSpaceChanged();
  }
Esempio n. 5
0
  /**
   * Import scripted or compiled BeanShell commands in the following package in the classpath. You
   * may use either "/" path or "." package notation. e.g. importCommands("/bsh/commands") or
   * importCommands("bsh.commands") are equivalent. If a relative path style specifier is used then
   * it is made into an absolute path by prepending "/".
   */
  public void importCommands(String name) {
    if (importedCommands == null) importedCommands = new ArrayList<String>();

    // dots to slashes
    name = name.replace('.', '/');
    // absolute
    if (!name.startsWith("/")) name = "/" + name;
    // remove trailing (but preserve case of simple "/")
    if (name.length() > 1 && name.endsWith("/")) name = name.substring(0, name.length() - 1);

    // If it exists, remove it and add it at the end (avoid memory leak)
    importedCommands.remove(name);

    importedCommands.add(name);
    nameSpaceChanged();
  }
Esempio n. 6
0
  /** Import a class name. Subsequent imports override earlier ones */
  public void importClass(String name) {
    if (importedClasses == null) importedClasses = new HashMap<String, String>();

    importedClasses.put(Name.suffix(name, 1), name);
    nameSpaceChanged();
  }
Esempio n. 7
0
 /** Remove the variable from the namespace. */
 public void unsetVariable(String name) {
   if (variables != null) {
     variables.remove(name);
     nameSpaceChanged();
   }
 }
Esempio n. 8
0
 /** Clear all cached classes and names */
 public void classLoaderChanged() {
   nameSpaceChanged();
 }