circle.getClass()

Download Report

Transcript circle.getClass()

Java Virtual Machine
• Reading Assignment:
http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTOC.doc.html
Chapter 1: All
Chapter 3: Sections 5, 6
Chapter 5: Sections 1 - 5
JVM-1
Java Virtual Machine (JVM)
• What is Java Virtual Machine?
• The Class Loader Subsystem
• Linking
o Verification
o Preparation
o Resolution
• Class Initialization
• Class Instantiation
JVM-2
What is JVM
• JVM is a component of the Java system that interprets and
executes the instructions in our class files.
• The following figure shows a block diagram of the JVM that
includes its major subsystems and memory areas.
JVM-3
What is JVM Cont’d
•
Each instance of the JVM has one method area, one heap, and one or
more stacks - one for each thread.
–
•
•
•
Each JVM Thread has its own program counter (pc)
When JVM loads a class file, it puts its information in the method area.
As the program runs, all objects instantiated are stored in the heap.
The stack area is used to store activation records as a program runs.
JVM-4
The Class Loader Subsystem
• The class loader performs three main functions
of JVM, namely: loading, linking & initialization.
• The linking process consists of three sub-tasks,
namely, verification, preparation, and resolution,
as shown in the following figure.
• These activities are performed in a strict order as shown
in the figure.
JVM-5
Class Loading
• Loading means reading the class file for a type, parsing it to get
its information, and storing the information in the method area.
• For each type it loads, the JVM must store the following kinds of
information in the method area:
– The fully qualified name of the type
– The fully qualified name of the type's direct superclass or if the type
is an interface, a list of its direct super interfaces .
– Whether the type is a class or an interface
– The type's modifiers ( public, abstract, final, etc)
– Constant pool for the type: constants and symbolic references.
– Field info : name, type and modifiers of variables (not constants)
– Method info : name, return type, number & types of parameters,
modifiers, bytecodes, size of stack frame and exception table.
JVM-6
Class Loading – Cont’d
• The end of the loading process is the creation of an instance of
java.lang.Class for the loaded type.
• The purpose is to give access to some of the information captured in
the method area for the type, to the programmer.
• Some of the methods of the class java.lang.Class are:
1.
2.
3.
4.
5.
6.
7.
public String getName()
public Class getSupClass()
public boolean isInterface()
public Class[] getInterfaces()
public Method[] getMethods()
public Fields[] getFields()
public Constructor[] getConstructors()
• Note that for any loaded type T, only one instance of java.lang.Class
is created even if T is used several times in an application.
• To use the above methods, we need to first call the getClass method
on any instance of T to get the reference to the Class instance for T.
JVM-7
Class Loading –Cont’d
1.
2.
3.
4.
5.
6.
7.
public abstract class Shape{
public String name(){
return getClass().getName();
}
public abstract double area();
public abstract double perimeter();
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
public class Circle extends Shape{
public double radius;
public Circle(double r){
radius = r;
}
public double area(){
return Math.PI * (radius * radius);
}
public double perimeter(){
return (2.0 * Math.PI )*radius;
}
}
JVM-8
Class Loading –Cont’d
1. public class Rectangle extends Shape{
2.
private double length;
3.
private double width;
4.
5.
public Rectangle(double length, double width){
6.
this.length = length;
7.
this.width = width;
8.
}
9.
public double area(){
10.
return length * width;
11.
}
12.
13.
14.
public double perimeter(){
return 2 * (length + width );
}
15. }
1. public class Square extends Rectangle{
2.
public Square(double length){
3.
super(length, length);
4.
}
5. }
JVM-9
Class Loading –Cont’d
1. import java.lang.reflect.Method;
2. public class TestClassClass{
3.
public static void main(String[] args){
4.
Circle circle = new Circle(10);
5.
Class circleClassInfo = circle.getClass();
6.
System.out.println("Class name is :"+circleClassInfo.getName());
7.
System.out.println("Parent is :"+circleClassInfo.getSuperclass());
8.
Method[] methods = circleClassInfo.getMethods();
9.
System.out.println("\nMethods are: ");
10.
for(int i = 0; i<methods.length; i++)
11.
System.out.println(methods[i]);
12.
}
13. }
JVM-10
Class Loading – Cont’d
•
What if we do not have an object of a class T? Use the following.
Public static Class forName(String className) throws ClassNotFoundException
1. import java.lang.reflect.Method;
2. public class TestClassClass2{
3.
public static void main(String[] args) throws ClassNotFoundException{
4.
Class test = Class.forName("TestClassClass2");
5.
System.out.println("\nClass name is:" +test.getName());
6.
System.out.println("Superclass is: d " + test.getSuperclass());
7.
System.out.println("\nMethods are: ");
8.
Method[] methods = test.getMethods();
9.
for(int i = 0; i<methods.length; i++)
10.
System.out.println(methods[i]);
11.
}
12. }
JVM-11
Linking : Verification
• The next process handled by the class loader is Linking. This
involves three sub-processes: Verification, Preparation and
Resolution.
• Verification is the process of ensuring that binary representation of a
class is structurally correct.
• The JVM has to make sure that a file it is asked to load was
generated by a valid compiler and it is well formed.
• Class B may be a valid sub-class of A at the time A and B were
compiled, but class A may have been changed and re-compiled.
• Example of some of the things that are checked at verification are:
– Every method is provided with a structurally correct signature.
– Every instruction obeys the type discipline of the Java language
– Every branch instruction branches to the start not middle of another
instruction.
JVM-12
Preparation
• In this phase, the Java virtual machine allocates memory for the
class (i.e static) variables and sets them to default initial values.
• Note that class variables are not initialized to their proper initial
values until the initialization phase - no java code is executed until
initialization.
• The default values for the various types are shown below:
JVM-13
Resolution
•
•
Resolution is the process of replacing symbolic names for types, fields
and methods used by a loaded type with their actual references.
Symbolic references are resolved into direct references by searching
through the method area to locate the referenced entity.
•
For the class below, at the loading phase, the class loader would have
loaded the classes: TestClassClass3, Circle, Shape, System, & Object.
1.
2.
3.
4.
5.
6.
7.
public class TestClassClass3{
public static void main(String[] args){
Circle circle = new Circle(10);
Class circleClassInfo = circle.getClass();
System.out.println("Parent is: "+circleClassInfo.getSuperclass());
}
}
•
The names of these classes would have been stored in the constant pool
for TestClassClass3.
In this phase, the names are replaced with their actual references.
•
JVM-14