Java Reflection Example

0

Reflection is the ability to examine and change the behavior and structure of a class dynamically. The class’s constructor, its fields and methods are all available in form of metadata.
The metadata is obtained in form of objects called metaobjects. Class and Method are classes whose instances represents the program. These are called classes of metaobject and make up the Java Reflection API.

Introspection

A program examining itself through metaobjects is called introspection. Introspection is like viewing oneself in mirror and from a program’s perspective, it provides a reflection of the program in terms of metadata. By examining its self-representation about its structure and behavior it make important decisions, for example, we can do direct metaobject modification like invoking a method dynamically.

In this example, I will show you how we can create an instance of a class, access its method and invoke the method dynamically through reflection. We will first load the TestBean class, create an instance of it, set the name property and retrieve its name.

TestBean:

package javarticles.javareflection;

public class TestBean {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }    
}

Instance Creation

To create an instance of a class dynamically, we first need to get a hold of its Class object.

Class<?> testBeanClass = Class.forName("javarticles.javareflection.TestBean");

Next, we call newInstance() method to create an instance of the class.

Object testBean = testBeanClass.newInstance();

Method Invocation

Once the instance is obtained, our goal is to retrieve the Method object. The method we would like to invoke is setName(name).
Calling getMethod(methodname, parameter types) on the Class object returns us the Method object we are looking out for.

Note that our setName(name) has a String parameter so we need to pass the String type as the second parameter to the getMethod().

Class<?> strType[] = new Class[1];
strType[0] = String.class;
Method setNameMethod = testBeanClass.getMethod("setName", strType);

After obtaining the method, we need to invoke it and pass the parameter values.

setNameMethod.invoke(testBean, "hello");

Now we will call, getName() method again through reflection. The return value must be the one we have set.

Method getNameMethod = testBeanClass.getMethod("getName", new Class<?>[0]);
System.out.println(getNameMethod.invoke(testBean, new Object[0]));

Here is the entire example.
MethodInvokerExample:

 testBeanClass = Class.forName("javarticles.javareflection.TestBean");
        Object testBean = testBeanClass.newInstance();
        Class<?> strType[] = new Class[1];
        strType[0] = String.class;
        Method setNameMethod = testBeanClass.getMethod("setName", strType);
        setNameMethod.invoke(testBean, "hello");
        
        Method getNameMethod = testBeanClass.getMethod("getName", new Class<?>[0]);
        System.out.println(getNameMethod.invoke(testBean, new Object[0]));
    }
}

Output:

Hello

Download Source Code

In this article, I showed you a simple example of reflection. We have set the name and retrieved the name dynamically.
You can download the source code here: javareflection

Share.

Leave A Reply