Basically the reflection API includes two components: objects representing various parts of a class file and provides a way to extract those objects in a safe and secure way.





Basically the reflection API includes two components: objects representing various parts of a class file and provides a way to extract those objects in a safe and secure way. Java supports several security safeguards and therefore it make sense to provide a set of classes in order to invalidate those safeguards.

The first component provided by the Reflection API provides a mechanism that fetches information about a class. This mechanism is developed inside the class named Class. The class Class is a special type of the class that provides the universal type for the meta information to describe objects within the Java system. With in the Java system Class loaders return objects of type Class. Till now this class includes three most interesting methods:

  • forName: This class loads a class of the given name by using the current class loader.
  • getName: This class returns the name of the class as a String object that identifies object references by their class name.
  • newInstance: newInstance method of the Class class invokes the null constructor and returns an object of that class.

Along with these methods some more methods are also added to the class Class. Here is the listing of these methods:

  • getConstructor, getConstructors, getDeclaredConstructor
  • getMethod, getMethods, getDeclaredMethods
  • getField, getFields, getDeclaredFields
  • getSuperclass
  • getInterfaces
  • getDeclaredClasses

Several new classes were added in order to represent the objects that these methods would return. Most of these classes are the part of the package java.lang.reflect, while some of the new basic type classes such as Void, Byte and so on are kept in java.lang package. Decision was taken to put the new classes where they are by putting classes that represented meta-data in the reflection package and classes that represented types in the language package.

The Reflection API made a number of changes to class Class in order to give the answer of the question regarding the internals of the class, and a bunch of classes representing the answers given by these new methods.

Uses of Reflection

Reflection is generally used by programs that examine or modify the runtime behavior of applications running in the Java virtual machine. This relatively advanced feature should be used only by developers having a strong grasp of the fundamentals of the language. Or in other words we can say that reflection is a powerful technique that is capable of enabling the applications to perform operations which would otherwise be impossible.

Extensibility Features

An application can use user-defined classes simply by creating instances of extensibility objects by using their fully-qualified names.

Class Browsers and Visual Development Environments

A class browser needs to be able for enumerating the members of classes. Visual development environment can benefit by using type information available in reflection to aid the developer in writing correct code.

Debuggers and Test Tools

Debuggers needs to be able for examining the private members on classes. Reflection can be used by test harnesses to systematically call a discoverable set APIs defined on a class, to make sure a high level of code coverage in a test suite.



Share on Google+Share on Google+


Posted on: November 16, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Reflection  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image