public class LocalClassLoader
extends java.net.URLClassLoader
package pkg; class A { class B { class C { } } B.C field; }The loading of class B.C requires some name resolving, which is done in the javac compiler, so that the type of field is always known as pkg.A$B$C at run-time.
Class name resolution implemented in this class also takes into account importations. In the following example, Hashtable is resolved as java.util.Hashtable:
import java.util.*; class A { Hashtable table; }
This class loader only loads classes from the specified class paths and Java built-in classes. It does not use the CLASSPATH environment variable.
All the loaded classes are cached in a hash table so that they can be
loaded efficiently the second time. This also means whenever two Class
objects with the same name are returned, they also exist in the
same memory location.
Red (tfeng) |
Red (tfeng) |
Modifier and Type | Class and Description |
---|---|
static class |
LocalClassLoader.ClassImport
The data structure that represent class importation.
|
Constructor and Description |
---|
LocalClassLoader()
Construct a class loader with no special class path.
|
LocalClassLoader(java.lang.String[] classPaths)
Construct a class loader with a set of class paths specified as
a string array.
|
Modifier and Type | Method and Description |
---|---|
void |
addClassPath(java.lang.String path)
Add a class path to the list of class paths to be searched
from.
|
java.util.List<LocalClassLoader.ClassImport> |
getImportedClasses()
Get the list of names of imported classes.
|
java.util.List<java.lang.String> |
getImportedPackages()
Get the list of names of imported packages.
|
void |
importClass(java.lang.String classFullName)
Import a class.
|
void |
importPackage(java.lang.String packageName)
Import a package.
|
protected java.lang.Class<?> |
loadClass(java.lang.String name,
boolean resolve)
Load a class with a given name.
|
java.lang.Class |
searchForClass(java.lang.String name)
Search for a class with its partial name in the current class
or current package.
|
java.lang.Class |
searchForClass(java.lang.StringBuffer name,
java.lang.Class currentClass)
Search for a class with a partial name in the given scope.
|
void |
setCurrentClass(java.lang.Class c)
Set the current class within which class names are to be
solved with
ClassLoader.loadClass(String) . |
void |
setCurrentClass(java.lang.Class c,
boolean resetPackage)
Set the current class within which class names are to be
solved with
ClassLoader.loadClass(String) . |
void |
setCurrentPackage(java.lang.String packageName)
Set the current package.
|
void |
setEnclosingClass(java.lang.String anonymousClass,
java.lang.Class enclosingClass)
Deprecated.
|
addURL, close, definePackage, findClass, findResource, findResources, getPermissions, getResourceAsStream, getURLs, newInstance, newInstance
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getClassLoadingLock, getPackage, getPackages, getParent, getResource, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
public LocalClassLoader() throws java.net.MalformedURLException
java.net.MalformedURLException
- If a string is not a proper URL.public LocalClassLoader(java.lang.String[] classPaths) throws java.net.MalformedURLException
classPaths
- The array of class paths to be searched in
order.java.net.MalformedURLException
- If a string is not a proper URL.public void addClassPath(java.lang.String path)
path
- The path to be added.public java.util.List<LocalClassLoader.ClassImport> getImportedClasses()
public java.util.List<java.lang.String> getImportedPackages()
public void importClass(java.lang.String classFullName)
ClassLoader.loadClass(String)
.
Class importations "import packageName.className" takes precedence over package importations "import packageName.*".
classFullName
- The full name of the class to be
imported. It must contain the package name as its prefix.
Nested classes are separated with ".", not "$", as used as
name separator at run-time.public void importPackage(java.lang.String packageName)
searchForClass(String)
.
Class importations in the form of "import packageName.className" takes precedence over package importations in the form of "import packageName.*".
packageName
- The full name of the package to be
imported.public java.lang.Class searchForClass(java.lang.String name) throws java.lang.ClassNotFoundException
name
- The partial name of the class to be loaded.java.lang.ClassNotFoundException
- If the class cannot be found.searchForClass(StringBuffer, Class)
public java.lang.Class searchForClass(java.lang.StringBuffer name, java.lang.Class currentClass) throws java.lang.ClassNotFoundException
It takes the following steps in this name resolving:
ClassLoader.loadClass(String, boolean)
and returned. Nested classes (case 2) take precedence
over this full name resolution.
importClass(String)
, check if its simple class name
(the last part) is the same as the first part of the
class name to be searched for. If so, the imported
class is loaded, and if necessary, its nested classes
are searched.
importPackage(String)
.
The implicitly imported package "java.lang" is
also searched.
This function is considerably slower than ClassLoader.loadClass(String)
,
which does not do a search but takes the given name for the
full class name.
name
- The partial name of the class to be loaded.currentClass
- The current class to be used as the scope.
If it is null, current class is not considered.java.lang.ClassNotFoundException
- If the class cannot be found.importClass(String)
,
importPackage(String)
public void setCurrentClass(java.lang.Class c)
ClassLoader.loadClass(String)
. This class can be
an interface, a nested class, and an anonymous class
(created at instantiation), as well as any normal class.
This function is the same as call setCurrentClass(c, true).
c
- The class to be set as the current class. When it
is null, both the current class and the current
package are set undefined.setCurrentClass(Class, boolean)
public void setCurrentClass(java.lang.Class c, boolean resetPackage)
ClassLoader.loadClass(String)
. This class can be
an interface, a nested class, and an anonymous class
(created at instantiation), as well as any normal class.
If resetPackage is true, this function
also sets the current package (see setCurrentPackage(String)
) to the package which the
class belongs to.
c
- The class to be set as the current class. When it
is null, both the current class and the current
package are set undefined.resetPackage
- If true, the current package
is set to be the package that contains class c;
when c is null, the current package is
also set to null. If false, do not
modify the current package.public void setCurrentPackage(java.lang.String packageName)
ClassLoader.loadClass(String)
searches for classes in the current package.packageName
- The package name to be set as the current
package. When it is null, the current package is
set undefined.public void setEnclosingClass(java.lang.String anonymousClass, java.lang.Class enclosingClass)
Class
class in Java 1.5.
While TypeAnalyzer
analyzes an AST, it registers the
enclosing class of each anonymous class that it sees. This
helps the class loader to resolve enclosing classes.
The same problem is solved in TypeAnalyzer
by using
a stack to record all the classes entered so far.
anonymousClass
- The internal name of an anonymous class
(using a number to identify it), which is used at run-time by
the Java virtual machine.enclosingClass
- The Class
object representing the
enclosing class.protected java.lang.Class<?> loadClass(java.lang.String name, boolean resolve) throws java.lang.ClassNotFoundException
ClassLoader.loadClass(String name)
, the function that users use to
dynamically resolve classes.
For efficiency, the class name must be complete.
If resolve is true, it uses ClassLoader.resolveClass(Class)
to further resolve the class. Resolving a class is a step in
preparing the class for use. It is not always required. When
Java virtual machine merely uses the signature of the loaded
class to perform dynamic verification, ClassLoader.loadClass(String)
calls this function with resolve equal to
false when this is the case.
loadClass
in class java.lang.ClassLoader
name
- The partial name of the class to be loaded.resolve
- Whether ClassLoader.resolveClass(Class)
should be
called.java.lang.ClassNotFoundException
- If the class cannot be found.