Thursday, September 23, 2010

Reflections in java

 The reflection API represents, or reflects, the classes, interfaces, and objects in the current Java Virtual Machine. You'll want to use the reflection API if you are writing development tools such as debuggers, class browsers,junit testing and GUI builders. With the reflection API you can:
  • Determine the class of an object.
  • Get information about a class's modifiers, fields, methods, constructors, and superclasses.
  • Find out what constants and method declarations belong to an interface.
  • Create an instance of a class whose name is not known until run time.
  • Get and set the value of an object's field, even if the field name is unknown to your program until run time.
  • Invoke a method upon an object, even if the method is not known until run time.
  • Create a new array, whose size and component type are not known until run time, and then modify the array's components.
Metadata
Metadata is data (information) about oneself

Accessing Metadata
Java stores metadata in these classes:
Metadata for a class:                java.lang.Class
Metadata for a constructor:       java.lang.reflect
Metadata for a field:                 java.lang.reflect
Metadata for a method:            java.lang.reflect

Organisation of Class:
class Class {
             Constructor[] getConstructors();
             Field         getDeclaredField(Strin
             Field[]       getDeclaredFields();
             Method[]      getDeclaredMethods();
...
}
class Field {
             Class getType();
...
}
class Method {
             Class[] getParameterTypes();
             Class   getReturnType();
...
}

These are the classes we require to get metadata of class.

Note that "member variable" is synonymous to "field".
So for each class, the Java Runtime Environment maintains an immutable Class object that contains information about the class. A Class object represents, or reflects, the class. With the reflection API, you can invoke methods upon a Class object which return Constructor , Method , and Field objects. Using these objects, you can get information about the corresponding constructors, methods, and fields defined in the class.
Class objects also represent interfaces. You invoke Class methods to find out about an interface's modifiers, methods, and public constants. Not all of the Class methods are appropriate when a Class object reflects an interface. For example, it doesn't make sense to invoke getConstructors when the Class object represents an interface. Examining Interfaces explains which Class methods you may use to get information about interfaces.
The introspection or reflection or examination of classes can be done as follows: Examining Classes explains how to determine the class of an object, and how to get information about classes and interfaces.
Manipulating Objects shows you how to instantiate classes, get or set field values, and invoke methods. With the reflection API, you can perform these tasks even if the names of the classes, fields, and methods are unknown until run time.
Working with Arrays describes the APIs used to create and modify arrays whose names are not known until run time.
Summary of Classes lists the classes that comprise the reflection API, and provides links to the apppropriate API documentation.

No comments:

Post a Comment

Chitika