Introduction To Reflection API

Introduction

The reflection API represents, or reflects the classes, interfaces, and objects in the current java virtual machine,

With the reflection API you can,

  • Determine the class of an object
  • Get information about a class’s modifiers, fields, methods, constructors and super classes.
  • Find out what constants and method declarations belong to an interface.
  • Create an instance of a class whose name is not known until runtime.
  • 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.
  • Create a new array, whose size and component type are not known until runtime, and then modify the array’s components.

Examining Classes

There may be situations where you need to get information about the classes at run time. For example, you want to display the names of the class fields, methods, and constructors or, you want to show which interfaces are implemented by a class. To get this information, need to get the Class object that reflects the class.

For each class, the JRE maintains an immutable class object that contains information about the class. Class object represents or reflects, the class. With the reflection API, can invoke methods on a Class object, which returns Constructor, Method, and Field objects. Also use these objects to get information about the corresponding constructors, methods & fields defined in the class. Class objects also represents interfaces.  Invoke the Class methods to find out about an interface’s modifiers, methods, and public constants.

At runtime, determine the name of a Class object by invoking the getName method. The String returned by getName() is the fully-qualified name of the class. The following program gets the class name of an object. First, it retrieves the corresponding Class object, and then it invokes the getName() method on that Class object.

//TestName.java
import java.lang.reflect.*;
import java.awt.*;
class TestName {
    public static void main(String args[]) {
        Button b = new Button();
        displayName(b);
    }
    static void displayName(Object o) {
        Class c = o.getClass(); //get the Class object
        String s = c.getName(); //get the Class Name
        System.out.println(s);
    }
}

Compile the file as javac TestName.java.

Run the file as java TestName

Output

Let’s write another program that displays modifiers of a class on the screen. The class used here is the Button class. The getModifiers() method is used to get the modifiers of the class. The class Modifiers is used to check if the modifier retrieved by the getModifiers() method is public, final, or abstract.

//ModifierTest.java
import java.lang.reflect.*;
import java.awt.*;
class ModifierText {
    public static void main(String[] args) {
        Button b = new Button();
        displayModifiers(b);
    }
    public static void displayModifiers(Object o) {
        Class c = o.getClass();
        int m = c.getModifiers();
        if (Modifier.isPublic(m)) System.out.println("public");
        if (Modifier.isAbstract(m)) System.out.println("abstract");
        if (Modifier.isFinal(m)) System.out.println("final");
    }
}

The program will give the following output

Also, we have summarized the classes that compose the reflection API. The class and Object classes are in the java.lang.package. The other classes are contained in the java.lang.reflect package.

Array

It provides static methods to dynamically create and access arrays.

Class

It represents or reflects, classes and interfaces.

Constructor

It provides information about, and access to, a constructor for a class. Allows you to instantiate a class dynamically.

Field

It provides information about and dynamic access to ,a field of a class or an interface.

Method

It also provides information about, and access to, a single method on a class or interface. It allows you to invoke the method dynamically.

Modifier

It provides static methods and constants that allow you to get information about the access modifiers of a class and its members.

Object

Also provides the GetClass method.

Summary

The reflection API represents or reflects, the classes, interfaces, and objects in the current Java Virtual Machine. A class object represents or reflects, the class. With the reflection API, you can invoke methods on a Class object, which returns Constructor, Method, and Field objects.


Similar Articles