Page 3 of 5
/* Define it (parse the class file) */ result = defineClass(classData, 0, classData.length);
If the class implementation was loaded, the penultimate step is to call the defineClass() method from java.lang.ClassLoader, which can be considered the first step of class verification. This method is implemented in the Java virtual machine and is responsible for verifying that the class bytes are a legal Java class file. Internally, the defineClass method fills out a data structure that the JVM uses to hold classes. If the class data is malformed, this call will cause a ClassFormatError to be thrown.
if (resolveIt) {
resolveClass(result);
}
The last class loader-specific requirement is to call resolveClass() if the boolean parameter resolveIt was true. This method does two things: First, it causes any classes that are referenced by this class explicitly to be loaded and a prototype object for this class to be created; then, it invokes the verifier to do dynamic verification of the legitimacy of the bytecodes in this class. If verification fails, this method call will throw a LinkageError, the most common of which is a VerifyError.
Note that for any class you will load, the resolveIt variable will always be true. It is only when the system is recursively calling loadClass() that it may set this variable false because it knows the class it is asking for is already resolved.
classes.put(className, result);
System.out.println(" >>>>>> Returning newly loaded class.");
return result;
}
The final step in the process is to store the class we've loaded and resolved into our hash table so that we can return it again if need be, and then to return the Class reference to the caller.
Of course if it were this simple there wouldn't be much more to talk about. In fact, there are two issues that class loader builders will have to deal with, security and talking to classes loaded by the custom class loader.
In our simple class loader, if the primordial class loader couldn't find the class, we loaded it from our private repository. What happens when that repository contains the class java.lang.FooBar ? There is no class named java.lang.FooBar, but we could install one by loading it from the class repository. This class, by virtue of the fact that it would have access to any package-protected variable in the java.lang package, can manipulate some sensitive variables so that later classes could subvert security measures. Therefore, one of the jobs of any class loader is to protect the system name space.
In our simple class loader we can add the code:
if (className.startsWith("java."))
throw newClassNotFoundException();
just after the call to findSystemClass above. This technique can be used to protect any package where you are sure that the loaded code will never have a reason to load a new class into some package.
Great!By Anonymous on September 21, 2009, 4:34 amI'd say this is a great tutorial, it helps me easily understand the mechanism of ClassLoader, thanks!
Reply | Read entire comment
Great Article , But i am really curious to know if there is any By Anonymous on September 17, 2009, 4:55 amGreat Article , But i am really curious to know if there is any way to create the objects related to the praticuler class loader say classloader.classname obj
Reply | Read entire comment
Great ArticleBy Anonymous on September 9, 2009, 7:16 amExcellent
Reply | Read entire comment
its goodBy Anonymous on September 5, 2009, 8:01 amits good
Reply | Read entire comment
no subBy Anonymous on August 18, 2009, 1:04 amwhat is the function of class loader?
Reply | Read entire comment
View all comments