RTTI, Class & Reflection
Lectures 22
- RTTIs & Reflection (introspection) are powerful programming tools.
- RTTIs (Run Time Type Identification) in Java
- It denotes a mechanism to find / program the type of an object at run time.
- 2 types of RTTI in Java
- Down-casting to a Child class: Required for compile time and not run time
((Circle)S).getRadius(); // method is not a member of Shape class
((Square)S).getCorner();
- Programmed at compile-time, it facilitates compilation.If invalid cast, JVM throws ClassCastException.
- Drawback: Need to know the dynamic type while writing the code and system is not ‘open’.
- Checking an object type by : Instanceof operator or isInstance() Method.
- They are used for safe downcasting
if (x instanceof Circle)
((Circle)S).getRadius();
if (x instanceof Square)
((Square)S).getCorner();
No Runtime ClassCastException
- In C, there is no way to know the type of the variable.
- In Java, RTTIs are facilitated by ‘Class’ Class. It is class that has information of the class also called as meta class. It cannot be overridden by the programmer.
- In C++, RTTIs are facilitated by <typeinfo>
- instanceof Operator
- operator instanceof determines if an object is an instance of
- a class,any of its subclasses, or implements an interface:
- object_ref instanceof class_name
Shape c = new Circle()
c instanceof Circle //true
c instanceof Shape //true
c instanceof Square //false
- object_ref instanceof interface_name
- It returns a boolean and does not work on primitives
- The java.lang.Class.isInstance()determines if the specified Object is assignment-compatible with the object represented by this Class.
public boolean isInstance(Object obj)
- It is equivalent of the Java’s instanceof operator.
- Usages:
Class cls = Shape.class; // no constructor
Shape s = new Circle();
// checking for an Shape instance
boolean retval = cls.isInstance(s);
- RTTI vs. Reflection (Introspection) in Java
- The Java RTTI mechanism allows you to discover an objects type at run time.
- However, it requires full knowledge of the needed types at compile time.
- Reflection (Introspection) Class: It requires type information at run time.
- Java does not support all types of reflection, here we consider only introspection class.
- Java’s ‘Class’ Class : An Introspection Class
- For each class in a program, Java stores its information in a Class object
- public final class Class<T>.T - type of the class modelled by the Class object. For example, the type of String.class is Class<String>. Use Class<?> if the class is unknown.
- Instances of the class Class represent classes and interfaces in a running Java application.
- Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.
- At compile time, Java creates the Class objects and stores them in .class files. At runtime, when an object of a class is created for the first time, JVM loads the Class object into memory by a call to defineClass method in the class loader.
- Class has no public constructor. Instead Class objects are constructed automatically by JVM as classes are loaded. When the Class object of a given type is loaded, it is used to create any object of that type.
- Introspection makes general class information available at run-time.The type (class) need not to be known at compile time. Supports reusability
- Usages:
- 1. method of Object (at run time)
Class x_class = x.getClass();
Class super_class = x.getSuperclass();
- 2. static method of Class (at run time):
Class circle_class = Class.forName(“Circle“);
- 3. class literal (at compile time):
Class square_class = Square.class;
- 4. Get class name from Class object
String c_name = x_class.getName();
- 5. method of Object (at run time) to get the interfaces:
Class[] x_ifaces = x.getInterfaces();
for (int i = 0; i < x_ifaces.length; i++) {
String ifaceName = x_ifaces[i].getName();
- 6. To check if a Class object is an interface, use the
isInterface() method
isPrimitive();
Field [] get Declared Fields();
isArray();
Methods[] getDeclaredMethods();
isLocalClass(); …
isMemberClass();
- Reflection (Introspection) in Java : Summary
- Simplest form of reflection finds out the type of an object
- instanceof
- isInstance
- getClass
- Reflection mechanism is supported by the Class objects and the java.lang.reflect library. It works by loading the appropriate Class object from a .class file, which must be available at run time.
- The library provides Field, Method and Constructor classes, all implementing the interfaces.