Top Description Inners Fields Constructors Methods
bsh

public Class BshClassManager

extends Object
Class Inheritance
Known Direct Subclasses
bsh.classpath.ClassManagerImpl
Imports
java.net.*, java.util.*, java.io.IOException, java.io.*, java.lang.reflect.Method, .Modifier

BshClassManager manages all classloading in BeanShell. It also supports a dynamically loaded extension (bsh.classpath package) which allows classpath extension and class file reloading. Currently the extension relies on 1.2 for BshClassLoader and weak references. See http://www.beanshell.org/manual/classloading.html for details on the bsh classloader architecture.

Bsh has a multi-tiered class loading architecture. No class loader is used unless/until the classpath is modified or a class is reloaded.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
pack-priv static class
BshClassManager.SignatureKey

SignatureKey serves as a hash of a method signature on a class for fast lookup of overloaded and general resolved Java methods.

Field Summary

Modifier and TypeField and Description
protected transient Hashtable<K, V>
absoluteClassCache

Global cache for things we know are classes.

protected transient Hashtable<K, V>
absoluteNonClasses

Global cache for things we know are *not* classes.

private Interpreter
declaringInterpreter

The interpreter which created the class manager This is used to load scripted classes from source files.

protected transient Hashtable<K, V>
protected transient Hashtable<K, V>
protected ClassLoader
externalClassLoader

An external classloader supplied by the setClassLoader() command.

private static Object
NOVALUE

Identifier for no value item.

protected transient Hashtable<K, V>
resolvedObjectMethods

Caches for resolved object and static methods.

protected transient Hashtable<K, V>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
public void
public void
cacheClassInfo(String name, Class<T>
if value is non-null, cache the class if value is null, set the flag that it is *not* a class to speed later resolution
value
)

Cache info about whether name is a class or not.

public void
cacheResolvedMethod(Class<T> clas, Class<T>[] types, Method method)

Cache a resolved (possibly overloaded) method based on the argument types used to invoke it, subject to classloader change.

public boolean
public Class<T>

Returns:

the class or null
classForName
(String name)

Load the specified class by name, taking into account added classpath and reloaded classes, etc.

Note

Again, this is just a trivial implementation.

protected void
protected void
clearCaches()

Clear the caches in BshClassManager

protected static UtilEvalError
public static BshClassManager
createClassManager(Interpreter interpreter)

Create a new instance of the class manager.

public Class<T>
defineClass(String name, byte[] code)

protected void
definingClass(String className)

Flag the class name as being in the process of being defined.

protected void
doneDefiningClass(String className)

Indicate that the specified class name has been defined and may be loaded normally.

protected void
doSuperImport()

Support for "import *;" Hide details in here as opposed to NameSpace.

public void
protected String
getClassBeingDefined(String className)

This method is a temporary workaround used with definingClass.

protected String
getClassNameByUnqName(String name)

Return the name or null if none is found, Throw an ClassPathException containing detail if name is ambigous.

protected Method

Returns:

the Method or null
getResolvedMethod
(Class<T> clas, String methodName, Class<T>[] types, boolean
specifies that only a static method may be returned.
onlyStatic
)

Return a previously cached resolved method.

public URL
getResource(String
should be an absolute path
path
)

Get a resource URL using the BeanShell classpath

public InputStream
getResourceAsStream(String
should be an absolute path
path
)

Get a resource stream using the BeanShell classpath

protected boolean
hasSuperImport()

A "super import" ("import *") operation has been performed.

protected boolean
protected Class<T>
protected static Error
noClassDefFound(String className, Error e)

Annotate the NoClassDefFoundError with some info about the class we were trying to load.

public Class<T>

Returns:

the class
plainClassForName
(String name)

Perform a plain Class.forName() or call the externally provided classloader.

public void
reloadAllClasses()

Overlay the entire path with a new class loader.

public void
reloadClasses(String[] classNames)

Reloading classes means creating a new classloader and using it whenever we are asked for classes in the appropriate space.

public void
reloadPackage(String pack)

Reload all classes in the specified package: e.g. "com.sun.tools" The special package name "<unpackaged>" can be used to refer to unpackaged classes.

public void
public void
reset()

Clear all loaders and start over.

public void
setClassLoader(ClassLoader externalCL)

Set an external class loader.

public void
setClassPath(URL[] cp)

Set a new base classpath and create a new base classloader.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

absoluteClassCacheback to summary
protected transient Hashtable<K, V> absoluteClassCache

Global cache for things we know are classes.

Note

these should probably be re-implemented with Soft references. (as opposed to strong or Weak)

absoluteNonClassesback to summary
protected transient Hashtable<K, V> absoluteNonClasses

Global cache for things we know are *not* classes.

Note

these should probably be re-implemented with Soft references. (as opposed to strong or Weak)

declaringInterpreterback to summary
private Interpreter declaringInterpreter

The interpreter which created the class manager This is used to load scripted classes from source files.

definingClassesback to summary
protected transient Hashtable<K, V> definingClasses
definingClassesBaseNamesback to summary
protected transient Hashtable<K, V> definingClassesBaseNames
externalClassLoaderback to summary
protected ClassLoader externalClassLoader

An external classloader supplied by the setClassLoader() command.

NOVALUEback to summary
private static Object NOVALUE

Identifier for no value item. Use a hashtable as a Set.

resolvedObjectMethodsback to summary
protected transient Hashtable<K, V> resolvedObjectMethods

Caches for resolved object and static methods. We keep these maps separate to support fast lookup in the general case where the method may be either.

resolvedStaticMethodsback to summary
protected transient Hashtable<K, V> resolvedStaticMethods

Constructor Detail

BshClassManagerback to summary
public BshClassManager()

Method Detail

addClassPathback to summary
public void addClassPath(URL path) throws IOException
addListenerback to summary
public void addListener(BshClassManager.Listener l)
cacheClassInfoback to summary
public void cacheClassInfo(String name, Class<T> value)

Cache info about whether name is a class or not.

Parameters
value:Class<T>

if value is non-null, cache the class if value is null, set the flag that it is *not* a class to speed later resolution

cacheResolvedMethodback to summary
public void cacheResolvedMethod(Class<T> clas, Class<T>[] types, Method method)

Cache a resolved (possibly overloaded) method based on the argument types used to invoke it, subject to classloader change. Static and Object methods are cached separately to support fast lookup in the general case where either will do.

classExistsback to summary
public boolean classExists(String name)
classForNameback to summary
public Class<T> classForName(String name)

Load the specified class by name, taking into account added classpath and reloaded classes, etc.

Note

Again, this is just a trivial implementation. See bsh.classpath.ClassManagerImpl for the fully functional class management package.

Returns:Class<T>

the class or null

classLoaderChangedback to summary
protected void classLoaderChanged()
clearCachesback to summary
protected void clearCaches()

Clear the caches in BshClassManager

See Also
void #reset() for external usage
cmUnavailableback to summary
protected static UtilEvalError cmUnavailable()
createClassManagerback to summary
public static BshClassManager createClassManager(Interpreter interpreter)

Create a new instance of the class manager. Class manager instnaces are now associated with the interpreter.

See Also
bsh.Interpreter.getClassManager(), bsh.Interpreter.setClassLoader(ClassLoader)
defineClassback to summary
public Class<T> defineClass(String name, byte[] code)
definingClassback to summary
protected void definingClass(String className)

Flag the class name as being in the process of being defined. The class manager will not attempt to load it.

doneDefiningClassback to summary
protected void doneDefiningClass(String className)

Indicate that the specified class name has been defined and may be loaded normally.

doSuperImportback to summary
protected void doSuperImport() throws UtilEvalError

Support for "import *;" Hide details in here as opposed to NameSpace.

dumpback to summary
public void dump(PrintWriter pw)
getClassBeingDefinedback to summary
protected String getClassBeingDefined(String className)

This method is a temporary workaround used with definingClass. It is to be removed at some point.

getClassNameByUnqNameback to summary
protected String getClassNameByUnqName(String name) throws UtilEvalError

Return the name or null if none is found, Throw an ClassPathException containing detail if name is ambigous.

getResolvedMethodback to summary
protected Method getResolvedMethod(Class<T> clas, String methodName, Class<T>[] types, boolean onlyStatic)

Return a previously cached resolved method.

Parameters
onlyStatic:boolean

specifies that only a static method may be returned.

Returns:Method

the Method or null

getResourceback to summary
public URL getResource(String path)

Get a resource URL using the BeanShell classpath

Parameters
path:String

should be an absolute path

getResourceAsStreamback to summary
public InputStream getResourceAsStream(String path)

Get a resource stream using the BeanShell classpath

Parameters
path:String

should be an absolute path

hasSuperImportback to summary
protected boolean hasSuperImport()

A "super import" ("import *") operation has been performed.

isClassBeingDefinedback to summary
protected boolean isClassBeingDefined(String className)
loadSourceClassback to summary
protected Class<T> loadSourceClass(String name)
noClassDefFoundback to summary
protected static Error noClassDefFound(String className, Error e)

Annotate the NoClassDefFoundError with some info about the class we were trying to load.

plainClassForNameback to summary
public Class<T> plainClassForName(String name) throws ClassNotFoundException

Perform a plain Class.forName() or call the externally provided classloader. If a BshClassManager implementation is loaded the call will be delegated to it, to allow for additional hooks.

This simply wraps that bottom level class lookup call and provides a central point for monitoring and handling certain Java version dependent bugs, etc.

Returns:Class<T>

the class

See Also
classForName(String)
reloadAllClassesback to summary
public void reloadAllClasses() throws UtilEvalError

Overlay the entire path with a new class loader. Set the base path to the user path + base path. No point in including the boot class path (can't reload thos).

reloadClassesback to summary
public void reloadClasses(String[] classNames) throws UtilEvalError

Reloading classes means creating a new classloader and using it whenever we are asked for classes in the appropriate space. For this we use a DiscreteFilesClassLoader

reloadPackageback to summary
public void reloadPackage(String pack) throws UtilEvalError

Reload all classes in the specified package: e.g. "com.sun.tools" The special package name "" can be used to refer to unpackaged classes.

removeListenerback to summary
public void removeListener(BshClassManager.Listener l)
resetback to summary
public void reset()

Clear all loaders and start over. No class loading.

setClassLoaderback to summary
public void setClassLoader(ClassLoader externalCL)

Set an external class loader. BeanShell will use this at the same point it would otherwise use the plain Class.forName(). i.e. if no explicit classpath management is done from the script (addClassPath(), setClassPath(), reloadClasses()) then BeanShell will only use the supplied classloader. If additional classpath management is done then BeanShell will perform that in addition to the supplied external classloader. However BeanShell is not currently able to reload classes supplied through the external classloader.

setClassPathback to summary
public void setClassPath(URL[] cp) throws UtilEvalError

Set a new base classpath and create a new base classloader. This means all types change.

bsh back to summary

public Interface BshClassManager.Listener

Known Direct Implementers
bsh.BSHType, bsh.NameSpace

Method Summary

Modifier and TypeMethod and Description
public void

Method Detail

classLoaderChangedback to summary
public void classLoaderChanged()
bsh back to summary

pack-priv Class BshClassManager.SignatureKey

extends Object
Class Inheritance

SignatureKey serves as a hash of a method signature on a class for fast lookup of overloaded and general resolved Java methods.

Field Summary

Modifier and TypeField and Description
pack-priv Class<T>
pack-priv int
pack-priv String
pack-priv Class<T>[]

Constructor Summary

AccessConstructor and Description
pack-priv
SignatureKey(Class<T> clas, String methodName, Class<T>[] types)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

clasback to summary
pack-priv Class<T> clas
hashCodeback to summary
pack-priv int hashCode
methodNameback to summary
pack-priv String methodName
typesback to summary
pack-priv Class<T>[] types

Constructor Detail

SignatureKeyback to summary
pack-priv SignatureKey(Class<T> clas, String methodName, Class<T>[] types)

Method Detail

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.