“Reflection
is the process of obtaining runtime information
about the class or interface.”
Runtime information is nothing but deal
with the following:
1. Finding the name of the class or interface.
2. Finding the data members of the class or
interface.
3. Finding number of constructors (default
constructor and number of parameterized constructors).
4. Number of instance methods.
5. Number of static methods.
6. Determining modifiers of the class (modifiers
of the class can be public, final, public +
final, abstract and public + abstract).
7. Obtaining super class of a derived
class.
8. Obtaining the interfaces
which are implemented by various classes.
Real time applications of reflection:
1. Development of language complier,
debuggers, editors and browsers.
2. In order to deal with reflection in
java, we must import a predefined package called java.lang.reflect.*
3. The package reflect contains
set of predefined classes and interfaces which are used by the programmer to
develop reflection applications. Number of ways to obtain
runtime information about a class (or)
number
of ways to get an object of
a class called Class:
The predefined class called Class is
present in a package called java.lang.Class
(fully qualified name of a class called Class is java.lang.Class). In java we have 4 ways to deal with or
to create an object of java.lang.Class, they are:
1) When we know that class name at compile
time and to get runtime information about the
class, we must use the following:
Ex1: Class c=c1.Class (c1 – userdefined class)
Ex2: Class c=java.long.string.class(String –
Predefind class)
2) When we know the object name at runtime,
to get the class name or class type of the runtime object, we must use the
following:
Ex1 : c1 o1=new c1();
Class c=o1.getClass();
Ex2
: String s=new String (“hello”);
Class c=s.getClass();
getClass is the predefined method present in a predefined class called java.lang.Object and whose prototype is given below:
3) When an object is given at runtime, we
must find runtime information about current class and its super class.
4) We know the class name at runtime and we
have to obtain the runtime information about the class.
To perform the above
we must use the method java.lang.Class and whose prototype is given below:
When we use the forName as
a part of java program it performs the following operations:
• It
can create an object of the class which we pass at runtime.
• It
returns runtime information about the object which is created.
For example:
try
{
Class c=Class.forName (“java.awt.Button”);
}
catch (ClassNotFoundException cnfe)
{
System.out.println
(“CLASS DOES NOT EXIST...”);
}
forName is
taking String as an argument. If the class is not found forName method
throws an exception called ClassNotFoundException. Here, forName
method is a factory method (a factory
method is one which return type is similar to name
of the class where it presents).
Every factory method must be static and
public. The class which contains a factory method is known as Singleton
class (a java class is said to be Singleton class through which we can create single object per JVM).
For example:
java.lang.Class is called Singleton class
Write
a java program to
find name of the class and its
super class name by passing the class name at runtime?
Q)
Write a java program to
print super
class hierarchy at a current class which is passed fromcommand prompt?
Obtaining information about
CONSTRUCTORS which are present in a class:
In order to get the constructor of the
current class we must use the following method:
For example:
Constructor cons []=c.getConstructors
();
System.out.println
(“NUMBER OF CONSTRUCTORS = ”+cons.length);
In order to get the parameters of the
constructor we must use the following method:
For example:
Class ptype []=cons [0].getParameterTypes
();
Obtaining METHODS information:
In order to obtain information about methods
we must use the following methods:
For example:
Method m []=c.getMethods ();
System.out.println
(“NUMBER OF METHODS = ”+m.length);
Associated
with methods we have return type of the method, name of the method and types of
parameters passed to a method.
The Method class contains the following
methods:
1. public Class getReturnType ();
2. public String getName ();
3. public Class [] getParameterTypes ();
Method-1 gives return type of the method,
Method-2 gives name of the method and
Method-3 gives what parameters the method
is taking.
Obtaining FIELDS or DATA MEMBERS of a
class:
In order to obtain information about fields
or data members of the class we must use the following method.
For example:
Field f []=c.getFields ();
System.out.println
(“NUMBER OF FIELDS = ”+f.length);
Associated
with field or data member there is a data type and field name:
The Field class contains the following
methods:
1. public Class getType ();
2. public String getName ();
Method-1 is used for obtaining data type of
the field and Method-2 is used for obtaining
name of the field.