Package org.gjt.sp.jedit.bsh
Class BshClassManager
- java.lang.Object
-
- org.gjt.sp.jedit.bsh.BshClassManager
-
- Direct Known Subclasses:
ClassManagerImpl
public class BshClassManager extends java.lang.ObjectBshClassManager 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 Class Summary
Nested Classes Modifier and Type Class Description static interfaceBshClassManager.Listener
-
Field Summary
Fields Modifier and Type Field Description protected java.util.HashtableabsoluteClassCacheGlobal cache for things we know are classes.protected java.util.HashtableabsoluteNonClassesGlobal cache for things we know are *not* classes.protected java.util.HashtabledefiningClassesprotected java.util.HashtabledefiningClassesBaseNamesprotected java.lang.ClassLoaderexternalClassLoaderAn external classloader supplied by the setClassLoader() command.protected java.util.HashtableresolvedObjectMethodsCaches for resolved object and static methods.protected java.util.HashtableresolvedStaticMethods
-
Constructor Summary
Constructors Constructor Description BshClassManager()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddClassPath(java.net.URL path)voidaddListener(BshClassManager.Listener l)voidcacheClassInfo(java.lang.String name, java.lang.Class value)Cache info about whether name is a class or not.voidcacheResolvedMethod(java.lang.Class clas, java.lang.Class[] types, java.lang.reflect.Method method)Cache a resolved (possibly overloaded) method based on the argument types used to invoke it, subject to classloader change.booleanclassExists(java.lang.String name)java.lang.ClassclassForName(java.lang.String name)Load the specified class by name, taking into account added classpath and reloaded classes, etc.protected voidclassLoaderChanged()protected voidclearCaches()Clear the caches in BshClassManagerprotected static UtilEvalErrorcmUnavailable()static BshClassManagercreateClassManager(Interpreter interpreter)Create a new instance of the class manager.java.lang.ClassdefineClass(java.lang.String name, byte[] code)protected voiddefiningClass(java.lang.String className)Flag the class name as being in the process of being defined.protected voiddoneDefiningClass(java.lang.String className)Indicate that the specified class name has been defined and may be loaded normally.protected voiddoSuperImport()Support for "import *;" Hide details in here as opposed to NameSpace.voiddump(java.io.PrintWriter pw)protected java.lang.StringgetClassBeingDefined(java.lang.String className)This method is a temporary workaround used with definingClass.protected java.lang.StringgetClassNameByUnqName(java.lang.String name)Return the name or null if none is found, Throw an ClassPathException containing detail if name is ambigous.protected java.lang.reflect.MethodgetResolvedMethod(java.lang.Class clas, java.lang.String methodName, java.lang.Class[] types, boolean onlyStatic)Return a previously cached resolved method.java.net.URLgetResource(java.lang.String path)Get a resource URL using the BeanShell classpathjava.io.InputStreamgetResourceAsStream(java.lang.String path)Get a resource stream using the BeanShell classpathprotected booleanhasSuperImport()A "super import" ("import *") operation has been performed.protected booleanisClassBeingDefined(java.lang.String className)protected java.lang.ClassloadSourceClass(java.lang.String name)protected static java.lang.ErrornoClassDefFound(java.lang.String className, java.lang.Error e)Annotate the NoClassDefFoundError with some info about the class we were trying to load.java.lang.ClassplainClassForName(java.lang.String name)Perform a plain Class.forName() or call the externally provided classloader.voidreloadAllClasses()Overlay the entire path with a new class loader.voidreloadClasses(java.lang.String[] classNames)Reloading classes means creating a new classloader and using it whenever we are asked for classes in the appropriate space.voidreloadPackage(java.lang.String pack)Reload all classes in the specified package: e.g.voidremoveListener(BshClassManager.Listener l)voidreset()Clear all loaders and start over.voidsetClassLoader(java.lang.ClassLoader externalCL)Set an external class loader.voidsetClassPath(java.net.URL[] cp)Set a new base classpath and create a new base classloader.
-
-
-
Field Detail
-
externalClassLoader
protected java.lang.ClassLoader externalClassLoader
An external classloader supplied by the setClassLoader() command.
-
absoluteClassCache
protected transient java.util.Hashtable 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)
-
absoluteNonClasses
protected transient java.util.Hashtable 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)
-
resolvedObjectMethods
protected transient java.util.Hashtable 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.
-
resolvedStaticMethods
protected transient java.util.Hashtable resolvedStaticMethods
-
definingClasses
protected transient java.util.Hashtable definingClasses
-
definingClassesBaseNames
protected transient java.util.Hashtable definingClassesBaseNames
-
-
Method Detail
-
createClassManager
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:
getClassManager,setClassLoader
-
classExists
public boolean classExists(java.lang.String name)
-
classForName
public java.lang.Class classForName(java.lang.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:
- the class or null
-
loadSourceClass
protected java.lang.Class loadSourceClass(java.lang.String name)
-
plainClassForName
public java.lang.Class plainClassForName(java.lang.String name) throws java.lang.ClassNotFoundExceptionPerform 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:
- the class
- Throws:
java.lang.ClassNotFoundException- See Also:
classForName( String )
-
getResource
public java.net.URL getResource(java.lang.String path)
Get a resource URL using the BeanShell classpath- Parameters:
path- should be an absolute path
-
getResourceAsStream
public java.io.InputStream getResourceAsStream(java.lang.String path)
Get a resource stream using the BeanShell classpath- Parameters:
path- should be an absolute path
-
cacheClassInfo
public void cacheClassInfo(java.lang.String name, java.lang.Class value)Cache info about whether name is a class or not.- Parameters:
value- 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
-
cacheResolvedMethod
public void cacheResolvedMethod(java.lang.Class clas, java.lang.Class[] types, java.lang.reflect.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.
-
getResolvedMethod
protected java.lang.reflect.Method getResolvedMethod(java.lang.Class clas, java.lang.String methodName, java.lang.Class[] types, boolean onlyStatic)Return a previously cached resolved method.- Parameters:
onlyStatic- specifies that only a static method may be returned.- Returns:
- the Method or null
-
clearCaches
protected void clearCaches()
Clear the caches in BshClassManager
-
setClassLoader
public void setClassLoader(java.lang.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.
-
addClassPath
public void addClassPath(java.net.URL path) throws java.io.IOException- Throws:
java.io.IOException
-
reset
public void reset()
Clear all loaders and start over. No class loading.
-
setClassPath
public void setClassPath(java.net.URL[] cp) throws UtilEvalErrorSet a new base classpath and create a new base classloader. This means all types change.- Throws:
UtilEvalError
-
reloadAllClasses
public void reloadAllClasses() throws UtilEvalErrorOverlay 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).- Throws:
UtilEvalError
-
reloadClasses
public void reloadClasses(java.lang.String[] classNames) throws UtilEvalErrorReloading 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- Throws:
UtilEvalError
-
reloadPackage
public void reloadPackage(java.lang.String pack) throws UtilEvalErrorReload all classes in the specified package: e.g. "com.sun.tools" The special package name "<unpackaged>" can be used to refer to unpackaged classes.- Throws:
UtilEvalError
-
doSuperImport
protected void doSuperImport() throws UtilEvalErrorSupport for "import *;" Hide details in here as opposed to NameSpace.- Throws:
UtilEvalError
-
hasSuperImport
protected boolean hasSuperImport()
A "super import" ("import *") operation has been performed.
-
getClassNameByUnqName
protected java.lang.String getClassNameByUnqName(java.lang.String name) throws UtilEvalErrorReturn the name or null if none is found, Throw an ClassPathException containing detail if name is ambigous.- Throws:
UtilEvalError
-
addListener
public void addListener(BshClassManager.Listener l)
-
removeListener
public void removeListener(BshClassManager.Listener l)
-
dump
public void dump(java.io.PrintWriter pw)
-
definingClass
protected void definingClass(java.lang.String className)
Flag the class name as being in the process of being defined. The class manager will not attempt to load it.
-
isClassBeingDefined
protected boolean isClassBeingDefined(java.lang.String className)
-
getClassBeingDefined
protected java.lang.String getClassBeingDefined(java.lang.String className)
This method is a temporary workaround used with definingClass. It is to be removed at some point.
-
doneDefiningClass
protected void doneDefiningClass(java.lang.String className)
Indicate that the specified class name has been defined and may be loaded normally.
-
defineClass
public java.lang.Class defineClass(java.lang.String name, byte[] code)
-
classLoaderChanged
protected void classLoaderChanged()
-
noClassDefFound
protected static java.lang.Error noClassDefFound(java.lang.String className, java.lang.Error e)Annotate the NoClassDefFoundError with some info about the class we were trying to load.
-
cmUnavailable
protected static UtilEvalError cmUnavailable()
-
-