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.

Slides:



Advertisements
Similar presentations
1 Dynamic Proxies Explained Simply. 2 Dynamic Proxies License Copyright © 2008 Ciaran McHale. Permission is hereby granted, free of charge, to any person.
Advertisements

Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Java Card Technology Ch07: Applet Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Computer Science & Engineering.
Dynamic Code Generation in Java. Class Loading Class loading is the process of transforming a byte code (e.g., a.class file) into a Java class A Java.
10/18/08 Matt Swatzell. What is Reflection?  Some Definitions….  Reflection is the process by which a program can modify its own behavior.  A program.
Copyright © 2001 Qusay H. Mahmoud Case Study A Global Compute Engine Here we will discuss some implementation details: – Class Loaders – Compute Engine.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.
Dynamic Code Generation in Java. Class Loading Class loading is the process of transforming a byte code (e.g., a.class file) into a Java class A Java.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Programming Languages and Paradigms Object-Oriented Programming.
Object Oriented Programming
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
1 Java Programming II Java Network II (Distributed Objects in Java)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
The Terminator to Android Hardening Services
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Java 2 security model Valentina Casola. Components of Java the development environment –development lifecycle –Java language features –class files and.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance in the Java programming language J. W. Rider.
IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
RMI remote method invocation. Traditional network programming The client program sends data to the server in some intermediary format and the server has.
RMI Remote Method Invocation Distributed Object-based System and RPC Together 2-Jun-16.
 Remote Method Invocation  A true distributed computing application interface for Java, written to provide easy access to objects existing on remote.
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Garbage Collection and Classloading Java Garbage Collectors  Eden Space  Surviver Space  Tenured Gen  Perm Gen  Garbage Collection Notes Classloading.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
IBM TSpaces Lab 2 Customizing tuples and fields. Summary Blocking commands Tuple Expiration Extending Tuples (The SubclassableTuple) Reading/writing user.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Programming Languages and Paradigms Activation Records in Java.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
RMI1 Remote Method Invocation Adapted from “Core Java 2” by Cay Horstmann.
RealTimeSystems Lab Jong-Koo, Lim
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Modern Programming Tools And Techniques-I
Topic: Java Class Loader
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Methods Attributes Method Modifiers ‘static’
Topic: Java Security Models
Running An Encrypted Java Program
Presentation on Object Oriented programming Topic
EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun
null, true, and false are also reserved.
Java Programming Language
Inheritance 2nd Lecture
Chapter 9 Carrano Chapter 10 Small Java
Presentation transcript:

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