Java Reference
In-Depth Information
tection domains define the security permissions for objects in that do-
mainagain see " Security " on page 677 for further details. Both forms of
defineClass may throw SecurityException .
Before you can define a class you have to read the bytes for the class,
and that is the purpose of bytesForClass :
protected byte[] bytesForClass(String name)
throws IOException, ClassNotFoundException
{
FileInputStream in = null;
try {
in = streamFor(name + ".class");
int length = in.available(); // get byte count
if (length == 0)
throw new ClassNotFoundException(name);
byte[] buf = new byte[length];
in.read(buf); // read the bytes
return buf;
} finally {
if (in != null)
in.close();
}
}
This method uses streamFor (not shown) to get a FileInputStream to the
class's bytecodes, assuming that the bytecodes are in a file named
by the class name with a ".class" appended streamFor knows where to
search for the class files to be loaded. We then create a buffer for the
bytes, read them all, and return the buffer.
When the class has been successfully loaded, findClass returns the new
Class object returned by defineClass . There is no way to explicitly unload
a class when it is no longer needed. You simply stop using it, allowing it
to be garbage-collected when its ClassLoader is unreachable.
 
Search WWH ::




Custom Search