Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 InTroToJCL Introduction to Java Class Loaders. 2 class loader l ia an object responsible for loading classes. The class ClassLoader is an abstract class.

Similar presentations


Presentation on theme: "1 InTroToJCL Introduction to Java Class Loaders. 2 class loader l ia an object responsible for loading classes. The class ClassLoader is an abstract class."— Presentation transcript:

1 1 InTroToJCL Introduction to Java Class Loaders

2 2 class loader l ia an object responsible for loading classes. The class ClassLoader is an abstract class. »Given the name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. »A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system.

3 3 How ClassLoader find classes/resources l Uses a delegation model to search for classes and resources. When called upon 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. The virtual machine's built-in class loader, called the bootstrap class loader, does not itself have a parent but may serve as the parent of a ClassLoader instance.

4 4 Where classes are loaded l Normally, from local file system, However, l Occasionally from other sources, such as the network, or constructed by an application. The method defineClass converts an array of bytes into an instance of class Class. Instances of this newly defined class can be created using the newInstance method in class Class. Class clazz = defineClass(“com.Myclass”, bytes, 0, bytes.length); MyClass o = (MyClass) clazz.newInstance();

5 5 l if class A refers to class B, then JVM use the loadClass() method of the class loader of A to load class B via. »A.getClassLoader().loadClass(“B”); l Ex: ClassLoader loader = new NetworkClassLoader(host, port); Object main = loader.loadClass("Main", true).newInstance();...

6 6 NetworkClassLoader must : »define the methods 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. l sample implementation:

7 7 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... } }

8 8 The delegation model for loading classes l client (JVM) invoke loadClass(…) to load class. l loadClass(className) { »// delegate to parent class »if( parent != null ){ » clazz = parent.loadClass(className) ; »} »if

9 9 Constructor Summary l protected ClassLoader() »Creates a new class loader using the ClassLoader returned by the method getSystemClassLoader() as the parent class loader. l protected ClassLoader(ClassLoader parent) »Creates a new class loader using the specified parent class loader for delegation.

10 10 Method Summary l protected Class defineClass(String name, byte[] b, int off, int len [, ProtectionDomain ]) »Converts an array of bytes into an instance of class Class, with an optional ProtectionDomain. protected Class findClass(String name) throws ClassNotFoundException »Finds the specified class. »should be overridden by subclass following the new delegation model for loading classes, and »will be called by the loadClass method after checking the parent class loader for the requested class. »The default implementation throws ClassNotFoundException.

11 11 loadClass(…) protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException l Loads the class with the specified name. l The default implementation : »Call findLoadedClass(String) to check if the class has already been loaded. »Call the loadClass() method on the parent class loader. If the parent is null the class loader built-in to the virtual machine is used, instead. »Call the findClass(String) method to find the class. If the class was found using the above steps, and the resolve flag is true, this method will then call the resolveClass(Class) method on the resulting class object. Subclasses of ClassLoader are encouraged to override findClass(String), rather than this method.

12 12 The source // First, check if the class has already been loaded Class c = findLoadedClass(name); if (c == null) { try { if (parent != null) { c = parent.loadClass(name, false);} else { c = findBootstrapClass0(name); } } catch (ClassNotFoundException e) { // If still not found, then call findClass in order // to find the class. c = findClass(name); } } if (resolve) { resolveClass(c); } return c;

13 13 findLoadedClass(name) protected final Class findLoadedClass(String name) »Finds the class with the given name if it had been previously loaded through this class loader. »return null if the class has not been previously loaded.

14 14 findClass(…) protected Class findClass(String name) throws ClassNotFoundException »Finds the specified class. »Should be overridden by class loader implementations that follow the new delegation model for loading classes, and »will be called by the loadClass method after checking the parent class loader for the requested class. »Default implementation throws ClassNotFoundException.

15 15 findSystemClass(…) protected final Class findSystemClass(String name) throws ClassNotFoundException »Finds a class with the specified name, loading it if necessary. »Since the Java 2 SDK v1.2, this method loads the class through the system class loader(see getSystemClassLoader() ). »Class objects returned might have ClassLoader s associated with them. Subclasses of ClassLoader need not usually call this method, because most class loaders need to override just findClass(String).

16 16 getSystemClassLoader() l public static ClassLoader getSystemClassLoader() »Returns the system class loader for delegation. »This is the default delegation parent for new ClassLoader instances, and is typically the class loader used to start the application. »This method is first invoked early in the runtime's startup sequence, at which point it creates the system class loader and sets it as the context class loader of the invoking Thread. l The default system class loader is implementation dependent.

17 17 l System class loader class name given at system property java.system.class.loader. »loaded using the default system class loader and must define a public constructor that takes a single parameter of type ClassLoader which is used as the delegation parent. »An instance is then created using this constructor with the default system class loader as the parameter. »The resulting class loader is defined to be the system class loader. If a security manager is present, and the caller's class loader is not null and the caller's class loader is not the same as or an ancestor of the system class loader, then this method calls the security manager's checkPermission method with a RuntimePermission("getClassLoader") permission to ensure it's ok to access the system class loader. If not, a SecurityException will be thrown.


Download ppt "1 InTroToJCL Introduction to Java Class Loaders. 2 class loader l ia an object responsible for loading classes. The class ClassLoader is an abstract class."

Similar presentations


Ads by Google