Java Reflection

In Java, reflection provides a means to examine a class to find out details regarding its constructors, fields, or properties, as they are sometimes known, and methods. It also allows for the methods of the class to be invoked. Below is an example of what can be done with reflection.

The 'Person' class below doesn't really do anything useful but provides a basis on which to demonstrate on. It contains two fields, or properties, a constructor and two methods.

public class Person {

    // Fields.
    private String name;
    private int age;

    // Constructor.
    public Person() {
        name = "Fred Bloggs";
        age = 30;
    }

    // Talk method.
    public static void talk() {
        System.out.println("I am talking");
    }

    // Walk method.
    public static void walk() {
        System.out.println("I am walking");
    }
}

First of all the 'Person' class is instantiated and details of its constructors, fields and methods are retrieved into arrays. The class name is then displayed, followed by details of its constructors, fields and methods.

import java.lang.reflect.*;

public class Demo {

    public static void main(String[] args) {

        try {

            // Instantiate the 'person' class and get the class details.
            Object personObj = new Person();
            Class personCls = personObj.getClass();

            // Extract the details of the class constructors, fields and methods.
            Constructor[] constructors = personCls.getDeclaredConstructors();
            Field[] fields = personCls.getDeclaredFields();
            Method[] methods = personCls.getDeclaredMethods();

            // Display the class name.
            System.out.println("Class Name: " + personCls.getName());

            System.out.println("");

            // Constructors in class.
            System.out.println("Constructors in Class:\n");
            for (Constructor constructor : constructors)
            {
                // Display details of constructor.
                System.out.println(constructor.toString());
            }

            System.out.println("");

            // Fields in class.
            System.out.println("Fields in Class:\n");
            for (Field field : fields)
            {
                // Display details of field.
                System.out.println(field.toString());
            }

            System.out.println("");

            // Methods in class.
            System.out.println("Methods in Class:");
            for (Method method : methods)
            {

                System.out.println("");

                // Display method name.
                System.out.println(method.toString());

                // Invoke method.
                System.out.print("Invoke method: ");
                method.invoke(personObj);

            }

        } catch (Exception e) {

            System.out.println("Unable to get class information.");

        }

    }

}

The resulting output is as follows.

Class Name: Person

Constructors in Class:

public Person()

Fields in Class:

private java.lang.String Person.name
private int Person.age

Methods in Class:

public static void Person.walk()
Invoke method: I am walking

public static void Person.talk()
Invoke method: I am talking

Further Reading