public class NameSpace extends java.lang.Object implements java.io.Serializable, BshClassManager.Listener, NameSource
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: I'd really like to use collections here, but we have to keep this compatible with JDK1.1
NameSource.Listener
Modifier and Type | Field and Description |
---|---|
protected java.util.Hashtable |
importedClasses |
static NameSpace |
JAVACODE |
Constructor and Description |
---|
NameSpace(BshClassManager classManager,
java.lang.String name) |
NameSpace(NameSpace parent,
BshClassManager classManager,
java.lang.String name) |
NameSpace(NameSpace parent,
java.lang.String name) |
Modifier and Type | Method and Description |
---|---|
void |
addCommandPath(java.lang.String path,
java.lang.Class clas)
Adds a URL to the command path.
|
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.
|
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.Vector 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() |
java.io.InputStream |
getCommand(java.lang.String name)
Looks up a command.
|
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) |
protected Variable |
getImportedVar(java.lang.String name) |
int |
getInvocationLine() |
java.lang.String |
getInvocationText() |
BshMethod |
getMethod(java.lang.String name,
java.lang.Class[] sig) |
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.
|
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.
|
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) |
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,
org.gjt.sp.jedit.bsh.SimpleNode callerInfo)
This method simply delegates to This.invokeMethod();
|
void |
loadDefaultImports()
Import standard packages.
|
void |
nameSpaceChanged()
Clear all cached classes and names
|
void |
prune()
Used for serialization
|
void |
removeCommandPath(java.lang.String path,
java.lang.Class clas)
Remove a URLfrom the command path.
|
void |
setMethod(java.lang.String name,
BshMethod method)
Note: this is primarily for internal use.
|
void |
setName(java.lang.String name) |
void |
setParent(NameSpace 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) |
void |
setVariable(java.lang.String name,
java.lang.Object value,
boolean strictJava)
Set the variable through this namespace.
|
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.Hashtable importedClasses
public NameSpace(NameSpace parent, java.lang.String name)
parent
- the parent namespace of this namespace. Child namespaces
inherit all variables and methods of their parent and can (of course)
override / shadow them.name
- a namepublic NameSpace(BshClassManager classManager, java.lang.String name)
public NameSpace(NameSpace parent, BshClassManager classManager, java.lang.String name)
public void setName(java.lang.String name)
public java.lang.String getName()
public java.lang.Object get(java.lang.String name, Interpreter interpreter) throws UtilEvalError
UtilEvalError
public 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.
strictJava
- specifies whether strict java rules are applied.UtilEvalError
Setting a new variable (which didn't exist before) or removing
a variable causes a namespace change.
public void unsetVariable(java.lang.String name)
public java.lang.String[] getVariableNames()
public java.lang.String[] getMethodNames()
public BshMethod[] getMethods()
public NameSpace getParent()
public This getSuper(Interpreter declaringInterpreter)
public This getGlobal(Interpreter declaringInterpreter)
public BshClassManager getClassManager()
public void prune()
public void setParent(NameSpace parent)
public 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.
UtilEvalError
Primitive.unwrap( Object )
public java.lang.Object getVariable(java.lang.String name, boolean recurse) throws UtilEvalError
recurse
- 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
Primitive.unwrap( Object )
protected Variable getVariableImpl(java.lang.String name, boolean recurse) throws UtilEvalError
If this namespace is static, return only static variables.
UtilEvalError
public Variable[] getDeclaredVariables()
protected java.lang.Object unwrapVariable(Variable var) throws UtilEvalError
UtilEvalError
public void setTypedVariable(java.lang.String name, java.lang.Class type, java.lang.Object value, boolean isFinal) throws UtilEvalError
UtilEvalError
public 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.
value
- If value is null, you'll get the default value for the typemodifiers
- may be nullUtilEvalError
Primitive
public void setMethod(java.lang.String name, BshMethod method) throws UtilEvalError
UtilEvalError
Interpreter.source( String )
,
Interpreter.eval( String )
public BshMethod getMethod(java.lang.String name, java.lang.Class[] sig) throws UtilEvalError
UtilEvalError
getMethod( 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.
declaredOnly
- if true then only methods declared directly in this
namespace will be found and no inherited or imported methods will
be visible.UtilEvalError
Primitive
public void importClass(java.lang.String name)
public void importPackage(java.lang.String name)
public void importCommands(java.lang.String name)
public java.lang.Object getCommand(java.lang.String name, java.lang.Class[] argTypes, Interpreter interpreter) throws UtilEvalError
This method searches the imported commands packages for a script or command object corresponding to the name of the method. If it is a script the script is sourced into this namespace and the BshMethod for the requested signature is returned. If it is a compiled class the class is returned. (Compiled command classes implement static invoke() methods).
The imported packages are searched in reverse order, so that later imports take priority. Currently only the first object (script or class) with the appropriate name is checked. If another, overloaded form, is located in another package it will not currently be found. This could be fixed.
name
- is the name of the desired command methodargTypes
- is the signature of the desired command method.UtilEvalError
- 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
UtilEvalError
protected Variable getImportedVar(java.lang.String name) throws UtilEvalError
UtilEvalError
public java.lang.Class getClass(java.lang.String name) throws UtilEvalError
UtilEvalError
public java.lang.String[] getAllNames()
getAllNames
in interface NameSource
protected void getAllNamesAux(java.util.Vector vec)
public void addNameSourceListener(NameSource.Listener listener)
addNameSourceListener
in interface NameSource
public void doSuperImport() throws UtilEvalError
UtilEvalError
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.Object invokeMethod(java.lang.String methodName, java.lang.Object[] args, Interpreter interpreter) throws EvalError
EvalError
invokeMethod
public java.lang.Object invokeMethod(java.lang.String methodName, java.lang.Object[] args, Interpreter interpreter, CallStack callstack, org.gjt.sp.jedit.bsh.SimpleNode callerInfo) throws EvalError
EvalError
invokeMethod
public void classLoaderChanged()
classLoaderChanged
in interface BshClassManager.Listener
public void nameSpaceChanged()
public void loadDefaultImports()
importClass("org.gjt.sp.jedit.bsh.EvalError"); importClass("org.gjt.sp.jedit.bsh.Interpreter"); importPackage("javax.swing.event"); importPackage("javax.swing"); importPackage("java.awt.event"); importPackage("java.awt"); importPackage("java.net"); importPackage("java.util"); importPackage("java.io"); importPackage("java.lang"); addCommandPath("/org/gjt/sp/jedit/bsh/commands",getClass());
public int getInvocationLine()
public java.lang.String getInvocationText()
public static java.lang.Class identifierToClass(ClassIdentifier ci)
public void clear()
loadDefaultImports()
public void importObject(java.lang.Object obj)
public void importStatic(java.lang.Class clas)
public void setVariable(java.lang.String name, java.lang.Object value) throws UtilEvalError
UtilEvalError
public void addCommandPath(java.lang.String path, java.lang.Class clas)
public void removeCommandPath(java.lang.String path, java.lang.Class clas)
public java.io.InputStream getCommand(java.lang.String name)