Friday Jun 22, 2007

Some information on the Class Loaders

It's just a recap of the class loaders from my experience.


Class Loaders use a delegation model to delegate the loading of a class to its parent class loaders recursively before handling it.

Arrays don't have any class loaders. The class loader of an array is same as of the element type. Therefore, an array of primitive values doesn't have any class loader.


a. Bootstrap class loader is a native implementation in the JVM and it doesn't have any parent. If you do a getClassLoader() on the class loaded by this  loader, it would return NULL.

 ex. String.class.getClassLoader() = null.< /FONT >

b. ClassLoader for extension (JRE/lib/ext) - sun.misc.Launcher$ExtClassLoader is used for loading the above classes. In some implementations, it is
                combined with bootstrap class loader.
 
c. Delegation model
 bootstrap <- ExtClassLoader <- AppClassLoader

d. BootstrapClass loader loads the classes from the local file system in a platform-dependent manner. It loads  classes of java.\* ,javax.\* and org.omg.\*, and if the search is made on those loaded classes to find out the class loaders, it would return NULL.

How does a ClassLoader provide security ?

Class loaders play a critical role in providing security because the class loader is responsible for locating and fetching the class file, consulting the security policy and defining the class object with the appropriate permissions.

What is the algorithm for loading a class ?

   a. Check if the class is loaded in the cache.
   b. If the current class loader has a delegation parent , redirect the loading request to that. Otherwise, redirect it to the bootstrap class loader.
   c.  If not found, call a customized method to find the class from the current classloader.

   In pseudo code
   findLoadedClass()
   parent.loadClass()
   findClass()

What are the rules for loading classes in terms of individual class loaders?

    a. First class of an application: AppClassLoader
    b. First class of an applet: AppletClassLoader
    c. class.forName("someclass") : bootstrap classLoader
   d. A reference inside a loaded class: current class loader

 a delegation model

   URLClassLoader::findClass()
   AppClassLoader::loadClass()
   classLoader::loadClass()
   ClassLoader::loadClassInternal() -> Called by JVM

What all does a classloader do?

     a.  produces a stream of binary data.
     b. parse the binary data into internal structures in the method area of JVM.
     c. creates an instance of Class to represent the type.

 

About

This is the blog of a software engineer, specialized in identity management. Kunal Sinha works in Directory Services Engineering (OpenDS) team from Austin,Texas.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks