Thursday, May 1, 2014

JVM,Part3

Something important about java.lang.ClassLoader

  1. The class ClassLoader is an abstract class.  Given the  binary name of a class, a class loader should attempt to  locate or generate data that constitutes a definition for the class.
  2. Applications implement subclasses of ClassLoader in order to extend the manner in which the Java virtual machine dynamically loads classes.
  3. The ClassLoader class uses a delegation model to search for classes and resources.  Each instance of ClassLoader  has an associated parent class loader.  When requested to find a class or resource, a ClassLoader  instance will delegate the search for the  class or resource to its parent class loader before attempting to find the  class or resource itself.
  4. Normally, the Java virtual machine loads classes from the local file system in a platform-dependent manner.  For example, on UNIX systems, the virtual machine loads classes from the directory defined by the CLASSPATH environment variable.
  5. However, some classes may not originate from a file; they may originate from other sources, such as the network, or they could be constructed by an application.  The method defineClass(String, byte[], int, int) converts an array of bytes into an instance of class.Instances of this newly defined class can be created using  Class.newInstance.
  6. The methods and constructors of objects created by a classloader may reference other classes.  To determine the class(es) referred to, the Java virtual machine invokes the loadClass  method of the class loader that originally created the class.
  7. For example, an application could create a network class loader to download class files from a server.  Sample code might look like:
                     ClassLoader loader=new NetworkClassLoader(host,port); 
                      Object main= loader.loadClass("Main", true).newInstance();



The network class loader subclass must define the method findClass() and loadClassData to load a class  from the network.  Once it has downloaded the bytes that make up the class, it should use the method  defineClass to create a class instance.  A sample implementation is:



   class NetworkClassLoader extends ClassLoader {

    String host;

    int port;



  public Class findClass(String name) {

              byte[] b = loadClassData(name);

              return defineClass(name, b, 0, b.length);

          }



          private byte[] loadClassData(String name) {

              // load the class data from the connection

              

       }

     }


How class loader behaves in case of array?


Class objects for array classes are not created by class loaders, but are created automatically as required by the Java runtime. The class loader for an array class, as returned by getClassLoader() is the same as the class loader for its element  type; if the element type is a primitive type, then the array class has no  class loader.



Is there any other related class loaders available in JDK?

1) public class SecureClassLoader extends ClassLoader = This class extends ClassLoader with additional support for defining classes with an associated code source and permissions which are retrieved by the system policy by default.

2) public class URLClassLoader extends SecureClassLoader = This class loader is used to load classes and resources from a search path of URLs referring to both JAR files and directories. Any URL that  ends with a '/' is assumed to refer to a directory. Otherwise, the URL is assumed to refer to a JAR file which will be opened as needed. The AccessControlContext of the thread that created the instance of  URLClassLoader will be used when subsequently loading classes and  resources.

3) There is also class loader related with RMI called as RMIClassLoader



Some of the methods of ClassLoader that you need to care and it is useful in case  of user-defined class loader:

1) protected final Class<?> defineClass(String name, byte[] b, int off, int len) throws ClassFormatError: Converts an array of bytes into an instance of class Class Before the class can  be used it must be resolved. Every Java virtual machine implementation must make sure the defineClass() method of class ClassLoader can cause a new type to be imported into the method area.
2)protected final Class findSystemClass(String name):The findSystemClass() method accepts a String representing a fully qualified name of a type. If the bootstrap class loader has already loaded or successfully loads the type, it returns a reference to the Class object representing the type. If it can't locate the binary data for the type, it throws ClassNotFoundException. In JAVA2 the findSystemClass() method attempts to load the requested type from the system class loader.

3)  protected final void resolveClass(Class c):The resolveClass() method accepts a reference to a Class instance. This method causes the type represented by the Class instance to be linked (if it hasn't already been linked). The defineClass() method,  only takes care of loading. When defineClass() returns a Class instance, the binary file for the type has definitely been located and imported into the method area, but not necessarily linked and initialized. Java virtual machine implementations make sure the resolveClass() method of class ClassLoader can cause the class loader subsystem to perform linking.

No comments:

Post a Comment