Sei sulla pagina 1di 18

Finishing

School
Reflections in Java

an Abhyaas Edu Corp initiative

Reflection

Finishing
School

Reflection is commonly used by programs which


require the ability to examine or modify the
runtime behaviour of applications running in the
Java virtual machine. "

an Abhyaas Edu Corp initiative

Reflection API

Finishing
School

Reflection API is a powerful technique (that provides the facility) to


find-out its environment as well as to inspect the class itsel
Reflection API was included in Java 1.1.
The classes of Reflection API are the part of the package java.lang.reflect
and the methods of Reflection API are the parts of the package
java.lang.class.
It allows the user to get the complete information about interfaces,
classes, constructors, fields and various methods being used.
NOTE : We should not use Reflection API in those applications wherever it
affects the application's performance, security related code of the
application such as in Applet programming.
Reflection API also affects the application if the private fields and
methods are there.

an Abhyaas Edu Corp initiative

Getting the implemented Interfaces

Finishing
School

getInterfaces() : This method is used to retrieve an


Interface (that included in the program).
Ex: Class cls = java.util.List.class;
Class[] intfs = cls.getInterfaces();
int len = intfs.length;
for (int i =0; i < len; i++) {
System.out.println(intfs[i]);
}

an Abhyaas Edu Corp initiative

Retrieving the class name through


Reflection API

Finishing
School

A more generic way, how to retrieve the name of


the class (that is used in the program) that reflects
the package name by using the getName() method.
Ex:
Class cls = java.lang.Integer.class;
String info;
info = cls.getName(); // It will show
java.lang.Integer
System.out.println(info);

an Abhyaas Edu Corp initiative

Finding out the super class name of


the class

Finishing
School

We can find the Superclass name by using the


getSuperclas s() method.
Class sup = cls.getSuperclass();
System.out.println(sup);

an Abhyaas Edu Corp initiative

Getting the method name used in the


Application

Finishing
School

We can retrieve method name by using the


getMethods() method.
Ex :
Class cls = java.lang.Integer.class;
Method method = cls.getMethods()[0];
String info;
info = method.getName();
System.out.println(info);

an Abhyaas Edu Corp initiative

Finding out the object of the class

Finishing
School

to retrieve an object name that reflects the


package name by using the object.getClass()
method.
Example :
Class cls = objct.getClass();
String strng = cls.getName();
System.out.println(strng);

an Abhyaas Edu Corp initiative

Finishing
School

Finding out the class fields


To retrieve the fields of the class by using the
getFields() method.
ex :

Class cls = java.lang.String.class;


Field field = cls.getFields()[0];
String name;
name = field.getName();
// It'll show CASE_INSENSITIVE_ORDER
System.out.println(name);
an Abhyaas Edu Corp initiative

Getting information about


Constructor

Finishing
School

To retrieve the information about the constructor


we use getConstructors() method.
Ex :
Class cls = java.lang.String.class;
Constructor con = cls.getConstructors()[0];
String name;
name = con.getName();
//It'll show java.lang.String
System.out.println(name);
an Abhyaas Edu Corp initiative

What is Introspection?

Finishing
School

This concept is often mixed with introspection.


Introspection is the ability of a program to examine the
type or properties of an object at runtime.
introspection is a subset of reflection. Some languages support
introspection, but do not support reflection, e.g., C++.

an Abhyaas Edu Corp initiative

Introspection Example

Finishing
School

The instance of operator determines whether an


object belongs to a particular class.

In Java, reflection is more about introspection,


because you can not change structure of an object.
There are some APIs to change accessibilities of
methods and fields, but not structures.

an Abhyaas Edu Corp initiative

Why do we need reflection?

Finishing
School

Reflection enables us to:


Examine an object's class at runtime
Construct an object for a class at runtime
Examine a class's field and method at runtime
Invoke any method of an object at runtime
Change accessibility flag of Constructor, Method
and Field etc.

an Abhyaas Edu Corp initiative

Finishing
School

Java Reflection makes it possible to inspect classes, interfaces, fields and


methods at runtime, without knowing the names of the classes,
methods etc. at compile time.
It is also possible to instantiate new objects, invoke methods and
get/set field values using reflection.
Inspecting classes is often the first thing you do when using Reflection.
From the classes you can obtain information about

Class Name
Class Modifies (public, private, synchronized etc.)
Package Info
Superclass
Implemented Interfaces
Constructors
Methods
Fields
Annotations
an Abhyaas Edu Corp initiative

java.lang.Class

Finishing
School

In java, every object is either a primitive type or reference.


All the classes, enums, arrays are reference types and inherit
from java.lang.Object.
Primitive types are boolean, byte, short, int, long, char, float, and
double.
java.lang.Class is the entry point for all the reflection operations.
For every type of object, JVM instantiates an immutable instance
of java.lang.Class that provides methods to examine the runtime
properties of the object and create new objects, invoke its method and
get/set object fields.

an Abhyaas Edu Corp initiative

How to use reflection?

Finishing
School

Example 1: Get class name from object

Output : myreflection.Foo
an Abhyaas Edu Corp initiative

Finishing
School

Invoke method on unknown object

an Abhyaas Edu Corp initiative

Finishing
School

Create object from Class instance

an Abhyaas Edu Corp initiative

Potrebbero piacerti anche