Using Reflection to run methods

14 November 2007

Reflection is used when we want examine or modify the runtime behavior of applications running in the Java virtual machine. Java’s Reflection API provides very useful functionality for programmers. Here we will go through an example to understand what can be done with Reflection.

Consider the following class called Countries:

 
public class Countries {
 
String name;
String capital;
 
public void setName(String name) {
	this.name = name;
}
 
public void setCapital(String capital) {
	this.capital = capital;
}
 
public void _getCountry(){
	System.out.println("Country: " + name);
} 
 
public void _getCapital(){
	System.out.println("Capital: " + capital + " - " + name);
} 
 
}

It has two setter methods that are used to set country name and its capital. Then we have two methods that are used to print useful information. These methods are not actually getters as they are not returning any thing. Please note that their names are starting with _get. This is done on purpose to distinguish these methods from other.

Now lets come to the main class named: RefTest.

import java.lang.reflect.*;
import java.util.Iterator;
import java.util.Vector;
public class RefTest {
 
 
public static void main(String[] args) throws Exception {
 
Vector <Countries> vector= new Vector<Countries>();
Countries obj = new Countries();
obj.setName("France");
obj.setCapital("Paris");
vector.add(obj);
 
Countries obj1 = new Countries();
obj1.setName("Germany");
obj1.setCapital("Berlin");
vector.add(obj1);
 
Countries obj2 = new Countries();
obj2.setName("Itlay");
obj2.setCapital("Rome");
vector.add(obj2);
 
 
for(Method m: (Class.forName("Countries")).getMethods())
{
if(m.getName().startsWith("_get"))
{
	Iterator it = vector.iterator();
	while(it.hasNext())
		m.invoke(it.next());	
} //if
 
} //for
 
} //main
 
} //class

We made three objects of class Countries and used setter methods to set values. We also added these objects into Vector called vector. This vector can store only objects of class Countries. This is generics introduced in Java 5.0.

Now we come to Reflection. We will get through all the methods of class Countries and will run only those that are starting with _get. Once we have the required method, we will use Iterator to call that method for all the objects stored in the Vector named vector. Invoke method is used to execute the method.

Output:

Country: France
Country: Germany
Country: Itlay
Capital: Paris - France
Capital: Berlin - Germany
Capital: Rome - Itlay

This is just a simple example of Reflection. You can do really interesting stuff using Reflection.

del.icio.us:Using Reflection to run methods  digg:Using Reflection to run methods  spurl:Using Reflection to run methods  wists:Using Reflection to run methods  simpy:Using Reflection to run methods  newsvine:Using Reflection to run methods  blinklist:Using Reflection to run methods  furl:Using Reflection to run methods  reddit:Using Reflection to run methods  fark:Using Reflection to run methods  blogmarks:Using Reflection to run methods  Y!:Using Reflection to run methods  smarking:Using Reflection to run methods  magnolia:Using Reflection to run methods  segnalo:Using Reflection to run methods  gifttagging:Using Reflection to run methods

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Using Reflection to run methods

Leave a Reply