Lecture 25: Run-Time Type Identification and Introspection

嚜澧OMP 144

Programming Language Concepts

Lecture 25: Run-Time Type Identification and Introspection

March 22, 2002

The University of North Carolina at Chapel Hill

COMP 144 Programming Language Concepts

Spring 2002

Lecture 25: Run-Time Type

Identification and Introspection

Felix HernandezHernandez-Campos

March 22

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

1

RTTI and Introspection

? Run-time type identification make it possible to

determine the type of an object

每 E.g. given a pointer to a base class, determine the derived

class of the pointed object

每 The type (class) must be known at compile time

? Introspection makes general class information

available at run-time

每 The type (class) does not have to be known at compile

time

每 This is very useful in component architectures and visual

programming

每 E.g. list the attributes of an object

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

2

Felix Hernandez-Campos

1

COMP 144

Programming Language Concepts

Lecture 25: Run-Time Type Identification and Introspection

March 22, 2002

RTTI and Introspection

? RTTI and introspection are powerful programming

language features

每 They enables some powerful design techniques

每 We will discuss them in the context of Java

? This discussion will follow Chapter 11 in Thinking in

Java by Bruce Eckel



.com/java/tij

tij/tij0119.

/tij0119.shtml

shtml



每 By the way, this is an excellent book freely available ononline

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

3

The need for RTTI

Polymorphism Example

class Triangle implements Shape {

//: Shapes.java

public void draw() {

package c11;

System.out.println

("Triangle.draw()");

System.out.println("Triangle.draw()");

import java.util.*;

}

interface Shape {

}

void draw();

public class Shapes {

}

public static void main(String[] args)

args) {

class Circle implements Shape {

Vector s = new Vector();

public void draw() {

}

}

PolyPolymorphism

s.addElement

(new Circle());

s.addElement(new

System.out.println

("Circle.draw()");

System.out.println("Circle.draw()");

Upcasting (Type Safe in Java)

s.addElement

(new Square());

s.addElement(new

s.addElement

(new Triangle());

s.addElement(new

Enumeration e = s.elements();

class Square implements Shape {

while(e.hasMoreElements

())

while(e.hasMoreElements())

public void draw() {

((Shape)e.nextElement

()).draw();

((Shape)e.nextElement()).draw();

System.out.println

("Square.draw()");

System.out.println("Square.draw()");

}

}

}

}

What if you want to known the exact type at runrun-time?

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

4

Felix Hernandez-Campos

2

COMP 144

Programming Language Concepts

Lecture 25: Run-Time Type Identification and Introspection

March 22, 2002

The Class Object

? Type information is available at run-time in Java

? There is a Class object for each class in the program

每 It stores class information

? Class objects are loaded in memory the first time

they are needed

每 A Java program is not completely loaded before it begin!

? The class Class provides a number of useful

methods for RTTI



COMP 144 Programming Language Concepts

Felix Hernandez-Campos

5

Example

public class SweetShop {

class Candy {

public static void main(String[] args)

args) {

static {

System.out.println

("inside main");

System.out.println("inside

System.out.println

("Loading Candy");

System.out.println("Loading

new Candy();

}

System.out.println

("After creating

System.out.println("After

Candy");

}

class Gum {

try {

static {

Class.forName

("Gum");

Class.forName("Gum");

System.out.println

("Loading Gum");

System.out.println("Loading

} catch(

catch(ClassNotFoundException e) {

}

e.printStackTrace

();

e.printStackTrace();

Executed at Load Time

}

class Cookie {

static {

System.out.println

("Loading

System.out.println("Loading

Cookie");

}

}

System.out.println

( "After

System.out.println(

Class.forName

(\"Gum\

Class.forName(

"Gum\")");

new Cookie();

System.out.println

("After creating

System.out.println("After

Cookie");

}

}

}

Returns a reference to class Gum

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

6

Felix Hernandez-Campos

3

COMP 144

Programming Language Concepts

Lecture 25: Run-Time Type Identification and Introspection

March 22, 2002

Example

? Output

每 JVMJVM-1

inside main

Loading Candy

After creating Candy

Loading Gum

After Class.forName

("Gum")

Class.forName("Gum")

Loading Cookie

After creating Cookie

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

7

Example

? Output

每 JVMJVM-2

Loading Candy

Loading Cookie

inside main

After creating Candy

Loading Gum

After Class.forName

("Gum")

Class.forName("Gum")

After creating Cookie

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

8

Felix Hernandez-Campos

4

COMP 144

Programming Language Concepts

Lecture 25: Run-Time Type Identification and Introspection

March 22, 2002

The Class Object

? Class literals also provide a reference to the Class

object

每 E.g. Gum.class

? Each object of a primitive wrapper class has a

standard field called TYPE that also provides a

reference to the Class object



ml

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

9

RTTI

? The type of a object can be determined using the

instanceof keyword

每 See PetCount.java

PetCount.java

每 It can be rewritten using Class literal, see PetCount2.java

每 Notice that an object of a derived class is an instance of the

its base classes (i.e.

(i.e. any predecessor in the inheritance

hierarchy)

? RTTI is very useful when reusing classes without

extending them

? Class.isInstance

() also implements the

Class.isInstance()

instanceof functionality

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

10

Felix Hernandez-Campos

5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download