public class NameSpace extends java.lang.Object implements java.io.Serializable, BshClassManager.Listener, NameSource, java.lang.Cloneable
A bsh.This object is a thin layer over a NameSpace that associates it with an Interpreter instance. Together they comprise a Bsh scripted object context.
Note: Dropping support for JDK 1.1. in favour of Collections Note: This class has gotten too big. It should be broken down a bit.
NameSource.Listener| Modifier and Type | Field and Description |
|---|---|
protected java.util.Map<java.lang.String,java.lang.String> |
importedClasses
The imported classes.
|
static NameSpace |
JAVACODE
The Constant JAVACODE.
|
| Constructor and Description |
|---|
NameSpace(NameSpace parent,
BshClassManager classManager,
java.lang.String name)
Instantiates a new name space.
|
NameSpace(NameSpace parent,
java.lang.String name)
Instantiates a new name space.
|
NameSpace(java.lang.String name)
Instantiates a new name space for name.
|
NameSpace(java.lang.String name,
BshClassManager classManager)
Instantiates a new name space for name with class manager.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addNameSourceListener(NameSource.Listener listener)
Implements NameSource Add a listener who is notified upon changes to
names in this space.
|
void |
classLoaderChanged()
Clear all cached classes and names.
|
void |
clear()
Clear all variables, methods, and imports from this namespace.
|
protected Variable |
createVariable(java.lang.String name,
java.lang.Class<?> type,
bsh.LHS lhs)
Creates the variable.
|
protected Variable |
createVariable(java.lang.String name,
java.lang.Class<?> type,
java.lang.Object value,
Modifiers mods)
Creates the variable.
|
protected Variable |
createVariable(java.lang.String name,
java.lang.Object value,
Modifiers mods)
Creates the variable.
|
void |
doSuperImport()
Perform "import *;" causing the entire classpath to be mapped.
|
java.lang.Object |
get(java.lang.String name,
Interpreter interpreter)
Resolve name to an object through this namespace.
|
java.lang.String[] |
getAllNames()
Implements NameSource.
|
protected void |
getAllNamesAux(java.util.List<java.lang.String> vec)
Helper for implementing NameSource.
|
java.lang.Class<?> |
getClass(java.lang.String name)
Load a class through this namespace taking into account imports.
|
BshClassManager |
getClassManager()
Gets the class manager.
|
java.lang.Object |
getCommand(java.lang.String name,
java.lang.Class<?>[] argTypes,
Interpreter interpreter)
A command is a scripted method or compiled command class implementing a
specified method signature.
|
Variable[] |
getDeclaredVariables() |
This |
getGlobal(Interpreter declaringInterpreter)
Get the top level namespace or this namespace if we are the top.
|
protected BshMethod |
getImportedMethod(java.lang.String name,
java.lang.Class<?>[] sig)
Gets the imported method.
|
protected Variable |
getImportedVar(java.lang.String name)
Gets the imported var.
|
int |
getInvocationLine()
Gets the invocation line.
|
java.lang.String |
getInvocationText()
Gets the invocation text.
|
BshMethod |
getMethod(java.lang.String name,
java.lang.Class<?>[] sig)
Gets the method.
|
BshMethod |
getMethod(java.lang.String name,
java.lang.Class<?>[] sig,
boolean declaredOnly)
Get the bsh method matching the specified signature declared in this
name space or a parent.
|
java.lang.String[] |
getMethodNames()
Get the names of methods declared in this namespace.
|
BshMethod[] |
getMethods()
Get the methods defined in this namespace.
|
java.lang.String |
getName()
The name of this namespace.
|
NameSpace |
getParent()
Get the parent namespace.
|
This |
getSuper(Interpreter declaringInterpreter)
Get the parent namespace' This reference or this namespace' This
reference if we are the top.
|
This |
getThis(Interpreter declaringInterpreter)
A This object is a thin layer over a namespace, comprising a bsh object
context.
|
java.lang.Object |
getVariable(java.lang.String name)
Get the specified variable in this namespace or a parent namespace.
|
java.lang.Object |
getVariable(java.lang.String name,
boolean recurse)
Get the specified variable in this namespace.
|
protected Variable |
getVariableImpl(java.lang.String name,
boolean recurse)
Locate a variable and return the Variable object with optional recursion
through parent name spaces.
|
java.lang.String[] |
getVariableNames()
Get the names of variables defined in this namespace.
|
java.lang.Object |
getVariableOrProperty(java.lang.String name,
Interpreter interp)
Get the specified variable or property in this namespace or a parent
namespace.
|
Variable[] |
getVariables()
Get the variables defined in this namespace.
|
static java.lang.Class<?> |
identifierToClass(ClassIdentifier ci)
This is a helper method for working inside of bsh scripts and commands.
|
void |
importClass(java.lang.String name)
Import a class name.
|
void |
importCommands(java.lang.String name)
Import scripted or compiled BeanShell commands in the following package
in the classpath.
|
void |
importObject(java.lang.Object obj)
Import a compiled Java object's methods and variables into this
namespace.
|
void |
importPackage(java.lang.String name)
subsequent imports override earlier ones.
|
void |
importStatic(java.lang.Class<?> clas)
Import static.
|
protected java.lang.Object |
invokeCommand(java.lang.String commandName,
java.lang.Object[] args,
Interpreter interpreter,
CallStack callstack,
Node callerInfo)
Invoke an imported command scoped by namespace hierarchy.
|
protected java.lang.Object |
invokeCommand(java.lang.String commandName,
java.lang.Object[] args,
Interpreter interpreter,
CallStack callstack,
Node callerInfo,
boolean ignoreInvoke)
Invoke an imported command scoped by namespace hierarchy.
|
protected java.lang.Object |
invokeDefaultInvokeMethod(java.lang.String commandName,
java.lang.Object[] args,
Interpreter interpreter,
CallStack callstack,
Node callerInfo,
boolean[] outHasMethod)
Invoke the default invoke method if found in the namespace,
Updates the outHasMethod array index 0 by reference, if false then method was
not found and the return result is meaningless.
|
java.lang.Object |
invokeMethod(java.lang.String methodName,
java.lang.Object[] args,
Interpreter interpreter)
Invoke a method in this namespace with the specified args and
interpreter reference.
|
java.lang.Object |
invokeMethod(java.lang.String methodName,
java.lang.Object[] args,
Interpreter interpreter,
CallStack callstack,
Node callerInfo)
This method simply delegates to This.invokeMethod();.
|
boolean |
isChildOf(NameSpace parent)
Check if this namespace is a child of the given namespace.
|
void |
loadDefaultImports()
Import standard packages.
|
void |
nameSpaceChanged()
Clear all cached classes and names.
|
void |
prune()
Used for serialization.
|
Variable |
setLocalVariable(java.lang.String name,
java.lang.Object value,
boolean strictJava)
Set a variable explicitly in the local scope.
|
void |
setMethod(BshMethod method)
Dissallow static vars outside of a class.
|
void |
setName(java.lang.String name)
Sets the name.
|
void |
setParent(NameSpace parent)
Sets the parent.
|
void |
setTypedVariable(java.lang.String name,
java.lang.Class<?> type,
java.lang.Object value,
boolean isFinal)
Deprecated.
See #setTypedVariable(String, Class, Object, Modifiers)
|
void |
setTypedVariable(java.lang.String name,
java.lang.Class<?> type,
java.lang.Object value,
Modifiers modifiers)
Declare a variable in the local scope and set its initial value.
|
void |
setVariable(java.lang.String name,
java.lang.Object value,
boolean strictJava)
Set the variable through this namespace.
|
protected void |
setVariableImpl(Variable var) |
void |
setVariableOrProperty(java.lang.String name,
java.lang.Object value,
boolean strictJava)
Sets a variable or property.
|
java.lang.String |
toString() |
void |
unsetVariable(java.lang.String name)
Remove the variable from the namespace.
|
protected java.lang.Object |
unwrapVariable(Variable var)
Unwrap a variable to its value.
|
public static final NameSpace JAVACODE
protected java.util.Map<java.lang.String,java.lang.String> importedClasses
public NameSpace(NameSpace parent, java.lang.String name)
parent - the parentname - the namepublic NameSpace(java.lang.String name)
name - the namepublic NameSpace(java.lang.String name,
BshClassManager classManager)
name - the nameclassManager - the class managerpublic NameSpace(NameSpace parent, BshClassManager classManager, java.lang.String name)
parent - the parentclassManager - the class managername - the namepublic void setName(java.lang.String name)
name - the new namepublic java.lang.String getName()
public java.lang.Object get(java.lang.String name,
Interpreter interpreter)
throws UtilEvalError
name - the nameinterpreter - the interpreterUtilEvalError - the util eval errorpublic void setVariable(java.lang.String name,
java.lang.Object value,
boolean strictJava)
throws UtilEvalError
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.
name - the namevalue - the valuestrictJava - specifies whether strict java rules are applied.UtilEvalError - the util eval error
Setting a new variable (which didn't exist before) or removing a
variable causes a namespace change.
public Variable setLocalVariable(java.lang.String name, java.lang.Object value, boolean strictJava) throws UtilEvalError
name - the namevalue - the valuestrictJava - the strict javaUtilEvalError - the util eval errorpublic void setVariableOrProperty(java.lang.String name,
java.lang.Object value,
boolean strictJava)
throws UtilEvalError
Sets a variable or property. See "setVariable" for rules regarding scoping.
We first check for the existence of the variable. If it exists, we set it. If the variable does not exist we look for a property. If the property exists and is writable we set it. Finally, if neither the variable or the property exist, we create a new variable.
name - the namevalue - the valuestrictJava - specifies whether strict java rules are applied.UtilEvalError - the util eval errorprotected Variable createVariable(java.lang.String name, java.lang.Object value, Modifiers mods) throws UtilEvalError
name - the namevalue - the valuemods - the modsUtilEvalError - the util eval errorprotected Variable createVariable(java.lang.String name, java.lang.Class<?> type, java.lang.Object value, Modifiers mods) throws UtilEvalError
name - the nametype - the typevalue - the valuemods - the modsUtilEvalError - the util eval errorprotected Variable createVariable(java.lang.String name, java.lang.Class<?> type, bsh.LHS lhs) throws UtilEvalError
name - the nametype - the typelhs - the lhsUtilEvalError - the util eval errorpublic void unsetVariable(java.lang.String name)
name - the namepublic java.lang.String[] getVariableNames()
public Variable[] getVariables()
public java.lang.String[] getMethodNames()
public BshMethod[] getMethods()
public NameSpace getParent()
public boolean isChildOf(NameSpace parent)
parent - the possible parentpublic This getSuper(Interpreter declaringInterpreter)
declaringInterpreter - the declaring interpreterpublic This getGlobal(Interpreter declaringInterpreter)
declaringInterpreter - the declaring interpreterpublic This getThis(Interpreter declaringInterpreter)
The declaringInterpreter is here to support callbacks from Java through generated proxies. The scripted object "remembers" who created it for things like printing messages and other per-interpreter phenomenon when called externally from Java.
declaringInterpreter - the declaring interpreterpublic BshClassManager getClassManager()
public void prune()
public void setParent(NameSpace parent)
parent - the new parentpublic java.lang.Object getVariableOrProperty(java.lang.String name,
Interpreter interp)
throws UtilEvalError
Get the specified variable or property in this namespace or a parent namespace.
We first search for a variable name, and then a property.
name - the nameinterp - the interpUtilEvalError - the util eval errorpublic java.lang.Object getVariable(java.lang.String name)
throws UtilEvalError
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.
name - the nameUtilEvalError - the util eval errorPrimitive.unwrap(Object)public java.lang.Object getVariable(java.lang.String name,
boolean recurse)
throws UtilEvalError
name - the namerecurse - If recurse is true then we recursively search through
parent namespaces for the variable.
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.
UtilEvalError - the util eval errorPrimitive.unwrap(Object)protected Variable getVariableImpl(java.lang.String name, boolean recurse) throws UtilEvalError
name - the namerecurse - the recurseUtilEvalError - the util eval errorprotected void setVariableImpl(Variable var)
public Variable[] getDeclaredVariables()
protected java.lang.Object unwrapVariable(Variable var) throws UtilEvalError
var - the varUtilEvalError - the util eval error@Deprecated
public void setTypedVariable(java.lang.String name,
java.lang.Class<?> type,
java.lang.Object value,
boolean isFinal)
throws UtilEvalError
name - the nametype - the typevalue - the valueisFinal - the is finalUtilEvalError - the util eval errorpublic void setTypedVariable(java.lang.String name,
java.lang.Class<?> type,
java.lang.Object value,
Modifiers modifiers)
throws UtilEvalError
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.
name - the nametype - the typevalue - If value is null, you'll get the default value for the typemodifiers - may be nullUtilEvalError - the util eval errorPrimitivepublic void setMethod(BshMethod method)
name - is here just to allow the error message to use it protected
void checkVariableModifiers(String name, Modifiers modifiers)
throws UtilEvalError { if (modifiers!=null &&
modifiers.hasModifier("static")) throw new UtilEvalError("Can't
declare static variable outside of class: "+name); }method - the methodUtilEvalError - the util eval error Note: this is primarily for
internal use.Interpreter.source(String),
Interpreter.eval(String)public BshMethod getMethod(java.lang.String name, java.lang.Class<?>[] sig) throws UtilEvalError
name - the namesig - the sigUtilEvalError - the util eval errorgetMethod(String, Class [], boolean),
getMethod(String, Class [])public BshMethod getMethod(java.lang.String name, java.lang.Class<?>[] sig, boolean declaredOnly) throws UtilEvalError
Note: this method is primarily intended for use internally. If you use this method outside of the bsh package you will have to be familiar with BeanShell's use of the Primitive wrapper class.
name - the namesig - the sigdeclaredOnly - if true then only methods declared directly in this
namespace will be found and no inherited or imported methods will
be visible.UtilEvalError - the util eval errorPrimitivepublic void importClass(java.lang.String name)
name - the namepublic void importPackage(java.lang.String name)
name - the namepublic void importCommands(java.lang.String name)
name - the namepublic java.lang.Object getCommand(java.lang.String name,
java.lang.Class<?>[] argTypes,
Interpreter interpreter)
throws UtilEvalError
name - is the name of the desired command methodargTypes - is the signature of the desired command method.interpreter - the interpreterUtilEvalError - if loadScriptedCommand throws UtilEvalError i.e. on
errors loading a script that was foundprotected BshMethod getImportedMethod(java.lang.String name, java.lang.Class<?>[] sig) throws UtilEvalError
name - the namesig - the sigUtilEvalError - the util eval errorprotected Variable getImportedVar(java.lang.String name) throws UtilEvalError
name - the nameUtilEvalError - the util eval errorpublic java.lang.Class<?> getClass(java.lang.String name)
throws UtilEvalError
name - the nameUtilEvalError - the util eval errorpublic java.lang.String[] getAllNames()
getAllNames in interface NameSourceprotected void getAllNamesAux(java.util.List<java.lang.String> vec)
vec - the vecpublic void addNameSourceListener(NameSource.Listener listener)
addNameSourceListener in interface NameSourcelistener - the listenerpublic void doSuperImport()
throws UtilEvalError
Super imports are different than regular imports. They are done in the context of the ClassManager rather than the NameSpace and thus their effects may persist after the NameSpace is released. It seems irregular that named imports are 'local' to the namespace but super imports are 'global' to the class manager but I suppose for performance reasons it is a good idea to scan the super class path only once and store the results.
UtilEvalError - the util eval errorpublic java.lang.String toString()
toString in class java.lang.Objectpublic java.lang.Object invokeMethod(java.lang.String methodName,
java.lang.Object[] args,
Interpreter interpreter)
throws EvalError
methodName - the method nameargs - the argsinterpreter - the interpreterEvalError - the eval errorbsh.This.invokeMethod(String methodName, Object [] args,
Interpreter interpreter, CallStack callstack, Node callerInfo,
boolean)public java.lang.Object invokeMethod(java.lang.String methodName,
java.lang.Object[] args,
Interpreter interpreter,
CallStack callstack,
Node callerInfo)
throws EvalError
methodName - the method nameargs - the argsinterpreter - the interpretercallstack - the callstackcallerInfo - the caller infoEvalError - the eval errorbsh.This.invokeMethod(String methodName, Object [] args,
Interpreter interpreter, CallStack callstack, Node
callerInfo)protected java.lang.Object invokeCommand(java.lang.String commandName,
java.lang.Object[] args,
Interpreter interpreter,
CallStack callstack,
Node callerInfo)
throws EvalError
commandName - the command nameargs - the argsinterpreter - the interpretercallstack - the callstackcallerInfo - the caller infoEvalError - the eval errorprotected java.lang.Object invokeCommand(java.lang.String commandName,
java.lang.Object[] args,
Interpreter interpreter,
CallStack callstack,
Node callerInfo,
boolean ignoreInvoke)
throws EvalError
commandName - the command nameargs - the argsinterpreter - the interpretercallstack - the callstackcallerInfo - the caller infoignoreInvoke - do not call default invoke if no commands foundEvalError - the eval errorprotected java.lang.Object invokeDefaultInvokeMethod(java.lang.String commandName,
java.lang.Object[] args,
Interpreter interpreter,
CallStack callstack,
Node callerInfo,
boolean[] outHasMethod)
throws EvalError
commandName - the command nameargs - the argsinterpreter - the interpretercallstack - the callstackcallerInfo - the caller infooutHasMethod - update array by reference if method foundEvalError - the eval errorpublic void classLoaderChanged()
classLoaderChanged in interface BshClassManager.Listenerpublic void nameSpaceChanged()
public void loadDefaultImports()
importClass("bsh.EvalError");
importClass("bsh.Interpreter");
importClass("bsh.Capabilities");
importPackage("java.net");
importClass("java.util.Map.Entry");
importPackage("java.util.function");
importPackage("java.util.stream");
importPackage("java.util.regex");
importPackage("java.util");
importPackage("java.io");
importPackage("java.lang");
importClass("bsh.FileReader");
importPackage("java.math");
importCommands("/bsh/commands");
public int getInvocationLine()
public java.lang.String getInvocationText()
public static java.lang.Class<?> identifierToClass(ClassIdentifier ci)
ci - the cipublic void clear()
loadDefaultImports()public void importObject(java.lang.Object obj)
obj - the obj Note: this impor pattern is becoming common... could
factor it out into an importedObject Listpublic void importStatic(java.lang.Class<?> clas)
clas - the clas