View RSS Feed

My Java Tips

Java Reflection API - Overview

Rate this Entry
by , 11-08-2011 at 06:08 PM (826 Views)
Concept of Java reflection is very powerful. It gives you access to internal information about the classes (sub classes, methods, attributes) loaded into the JVM. This concept is not available in other programming languages like C and C++.

Consider the following example. I have displayed the complete signatures of MyClass " methods using reflection. First of all, you should obtain a java.lang.Class object for the class that you want to manipulate. java.lang.Class " is used to represent classes in a running Java program. Then we will use getDeclaredMethods() to get the list of methods in an array.




Java Code:
import java.lang.reflect.*;

public class Test {

	public static void main(String[] args) {

		try {
			Class c = Class.forName("MyClass");
			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);
		}
	}
}

public class MyClass {

	int getAge(String name){
		return 25;
	}

	String getLoc(String name){
		return "Paris";
	}

}
Output:
int MyClass.getAge(java.lang.String)
java.lang.String MyClass.getLoc(java.lang.String)

You may also get the names and types of attributes from a class. getType() and getName() methods are used for this purpose. But before using these, you have to get the class using Class.forName() and after that use getDeclaredFields() to get the list of fields/attributes.

Review the following code:

Java Code:
public class BaseClass {
private String company_name;
private int company_code;
}

public class MyClass extends BaseClass {
private int age;
private String name;
}

import java.lang.reflect.*;

public class Test {

	public static void main(String[] args) {

		try {
			Class c = Class.forName("MyClass");

			Field list[] = c.getDeclaredFields();
			for (int i = 0; i < list.length; i++) {
				Field fld = list[i];
				System.out.println("Class: " +fld.getDeclaringClass());
				System.out.println("Super Class: " +fld.getDeclaringClass().getSuperclass());
				System.out.println("Attribute name: " + fld.getName());
				System.out.println("Data type: " + fld.getType());
				int mod = fld.getModifiers();
				System.out.println("Modifiers: " +Modifier.toString(mod));
				System.out.println("======================================");
			}
		}
		catch (Throwable e) {
			System.err.println(e);
		}
	}
}
Output:
Class: class MyClass
Super Class: class BaseClass
Attribute name: age
Data type: int
Modifiers: private
======================================
Class: class MyClass
Super Class: class BaseClass
Attribute name: name
Data type: class java.lang.String
Modifiers: private
======================================

The getSuperclass() " method retrieves the name of super class. In the example discussed above, super class was BaseClass. If it has no explicit super class then it will return java.lang.Object as super class as every class in java is inherited from it.

Reflection also allows you to get the list of constructors.You can get information about a class's constructors by invoking the getConstructors() method. It will return an array of Constructor objects. Now you can use the methods provided by the Constructor class to get the constructor's name, set of modifiers, parameter types, and set of throwable exceptions.

You may also call methods of a class using reflection and you may also change values of attributes using reflection.

Submit "Java Reflection API - Overview" to Facebook Submit "Java Reflection API - Overview" to Digg Submit "Java Reflection API - Overview" to del.icio.us Submit "Java Reflection API - Overview" to StumbleUpon Submit "Java Reflection API - Overview" to Google

Comments