August 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

What's Going On in There? Java Reflection for Program Insight

  • May 12, 2009
  • By Sridhar M S
  • Send Email »
  • More Articles »

In programming terms, reflection is a feature that allows you to see the internal composition of a program—everything from its variables to its method declarations. As its name indicates, the Java Reflection API enables this functionality in Java.

The Java Reflection API provides insight into the classes, interfaces, and objects in a given Java Virtual Machine (JVM). Developers commonly use the API to accomplish the following tasks, which explains why it is so often used to develop tools such as debuggers and Integrated Development Environments (IDEs):

  • Determine the class of an object.
  • Get information about a class's modifiers, fields, methods, constructors, etc.
  • Get information about an interface's constants and method declarations.
  • Create an instance of a class whose name is not known until runtime, but rather available at design time or provided as a runtime parameter.
  • Get and set the value of an object's field, even if the field name is unknown to your program until runtime.
  • Invoke a method on an object, even if the method is not known until runtime.
One tangible use of reflection is in JavaBeans, where you can manipulate software components visually via a builder tool. The tool uses reflection to obtain the properties of Java components (classes) as they are dynamically loaded.

Using Reflection to Retrieve Class Behavior

To understand how you can use reflection to ascertain a class's behavior, consider the simple example of an Employee class:
public class Employee
{
   public String empNum;
   public String empName;
 
   public Employee()
   {
      this( "1", "King");
   }
 
   public Employee(String empNum, String empName)
   {
      empNum = empNum;
      empName = empName;
   }
 
   public String toString()
   {
      return "Employee Details: EmpNumber: " + empNum + ", 
EmpName: "+ empNum; } } import java.lang.reflect.Modifier; public class AnalyzeClass { public static void main(String[] args) { Employee employee = new Employee(); Class klass = employee.getClass(); System.out.println( "Class name: " + klass.getName()); System.out.println( "Class super class: " + klass.getSuperclass()); int mods = klass.getModifiers(); System.out.println( "Class is public: " + Modifier.isPublic(mods)); System.out.println( "Class is final: " + Modifier.isFinal(mods)); System.out.println( "Class is abstract: " + Modifier.isAbstract(mods)); } }
Compiling the Modifier classes and executing the AnalyzeClass produces the following output:
Class name: Employee
Class super class: class java.lang.Object
Class is public: true
Class is final: false
Class is abstract: false
This is basically an extract of the Employee class, so you are able to retrieve all the properties that it has. Need more convincing? Consider another example involving a Method class:
import java.lang.reflect.*;
 
public class DumpMethods {
  public static void main(String args[])
  {
     try {
       Class c = Class.forName(args[0]);
       Method m[] = c.getDeclaredMethods();
       for (int i = 0; i < m.length; i++)
        System.out.println(m[i].toString());
     }
     catch (Throwable e) {
        System.err.println(e);
     }
  }
}
When you compile this code and invoke it with an argument of a known class...
java DumpMethods java.util.Stack
...You receive the following result:
public synchronized java.lang.Object java.util.Stack.pop()
public java.lang.Object java.util.Stack.push(java.lang.Object)
public boolean java.util.Stack.empty()
public synchronized java.lang.Object java.util.Stack.peek()
public synchronized int java.util.Stack.search(java.lang.Object)
The preceding output lists all the methods of the java.util.Stack class. Method is a class in java.lang.reflect, which is responsible for yielding this result.





Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel