Reflection API : A Brief Introduction

In
this section you will learn about the Reflection API.
Reflection
API is a powerful technique (that provides the facility) to find-out its environment as well as to inspect the
class itself. 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. It also provides an easy
way to create a Java Application that was not possible before Java 1.1. You can create
methods like event handlers, hash code etc and also find-out the objects and classes.
With the help of Reflection
API you can get the information about any class of the java.lang package. There are some useful methods like getName() and
getInterfaces(), which allows us to retrieve the name of the class and the
interfaces of the package respectively.
Avoid using 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.
Here
we are providing few of the examples to give a better overview of the Reflection
API :
1.
Getting
the implemented Interfaces
In this section you will learn how to
retrieve an Interface (that included in the program) by using the
getInterfaces() method. Here is an example that provides the
usage of the getInterfaces() method in more detail.
2.
Retrieving the
class name through Reflection API
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. Here is an example that provides the
proper way to use the getName() method.
3. Finding
out the superclass name of the Class
Here we show you
the way to
find out the Superclass name by using the getSuperclass() method. The given example demonstrates
the use of getSuperclass() method in more detail.
4. Getting
the method name used in the Application
In this section we describe how to retrieve method name
by using the getMethods() method. Here is an example that
demonstrates the use of the getMethods() method in more detail.
5. Finding
out the object of the Class
This section illustrates how to
retrieve an object name that reflects the package name by using the
object.getClass() method. Here is an example that provides the
proper usage of the object.getClass() method in more detail.
6. Finding
out the class fields
This section explores you, how
to retrieve the fields of the class by using
the getFields() method. For this we are taking an example that
provides the use of the getFields() method in detailed way.
7. Getting information about Constructor
In this section you will learn,
how to
retrieve the information about the constructor by using the
getConstructors() method. Here is an example that provides the
usage of the getConstructors() method.

|