Reflection of Java Classes
In Java, reflection allows us to inspect and manipulate classes, interfaces, constructors, methods, and fields at run time.
There exists three ways to create objects of Class:
1. Using forName() method
1 2 3 4 5 | class Dog {...} // create object of Class // to reflect the Dog class Class a = Class.forName("Dog"); |
Here, the forName()
method takes the name of the class to be reflected as its argument.
2. Using getClass() method
1 2 3 4 5 6 | // create an object of Dog class Dog d1 = new Dog(); // create an object of Class // to reflect Dog Class b = d1.getClass(); |
Here, we are using the object of the Dog class to create an object of Class.
3. Using .class extension
1 2 3 | // create an object of Class // to reflect the Dog class Class c = Dog.class; |
Now that we know how we can create objects of the Class
. We can use this object to get information about the corresponding class at runtime.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import java.lang.Class; import java.lang.reflect.*; class Animal { } // put this class in different Dog.java file public class Dog extends Animal { public void display() { System.out.println("I am a dog."); } } // put this in Main.java file class Main { public static void main(String[] args) { try { // create an object of Dog Dog d1 = new Dog(); // create an object of Class // using getClass() Class obj = d1.getClass(); // get name of the class String name = obj.getName(); System.out.println("Name: " + name); // get the access modifier of the class int modifier = obj.getModifiers(); // convert the access modifier to string String mod = Modifier.toString(modifier); System.out.println("Modifier: " + mod); // get the superclass of Dog Class superClass = obj.getSuperclass(); System.out.println("Superclass: " + superClass.getName()); } catch (Exception e) { e.printStackTrace(); } } } |
Output:
1 2 3 | Name: Dog Modifier: public Superclass: Animal |
Reflecting Fields, Methods, and Constructors
The package java.lang.reflect
provides classes that can be used for manipulating class members. For example,
- Method class – provides information about methods in a class
- Field class – provides information about fields in a class
- Constructor class – provides information about constructors in a class
Reflection of Java Methods
The Method
class provides various methods that can be used to get information about the methods present in a class.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | import java.lang.Class; import java.lang.reflect.*; class Dog { // methods of the class public void display() { System.out.println("I am a dog."); } private void makeSound() { System.out.println("Bark Bark"); } } class Main { public static void main(String[] args) { try { // create an object of Dog Dog d1 = new Dog(); // create an object of Class // using getClass() Class obj = d1.getClass(); // using object of Class to // get all the declared methods of Dog Method[] methods = obj.getDeclaredMethods(); // create an object of the Method class for (Method m : methods) { // get names of methods System.out.println("Method Name: " + m.getName()); // get the access modifier of methods int modifier = m.getModifiers(); System.out.println("Modifier: " + Modifier.toString(modifier)); // get the return types of method System.out.println("Return Types: " + m.getReturnType()); System.out.println(" "); } } catch (Exception e) { e.printStackTrace(); } } } |
Output:
1 2 3 4 5 6 7 | Method Name: display Modifier: public Return Types: void Method Name: makeSound Modifier: private Return Types: void |
Reflection of Java Fields
Like methods, we can also inspect and modify different fields of a class using the methods of the Field
class.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import java.lang.Class; import java.lang.reflect.*; class Dog { public String type; } class Main { public static void main(String[] args) { try { // create an object of Dog Dog d1 = new Dog(); // create an object of Class // using getClass() Class obj = d1.getClass(); // access and set the type field Field field1 = obj.getField("type"); field1.set(d1, "labrador"); // get the value of the field type String typeValue = (String) field1.get(d1); System.out.println("Value: " + typeValue); // get the access modifier of the field type int mod = field1.getModifiers(); // convert the modifier to String form String modifier1 = Modifier.toString(mod); System.out.println("Modifier: " + modifier1); System.out.println(" "); } catch (Exception e) { e.printStackTrace(); } } } |
Output:
1 2 | Value: labrador Modifier: public |
Reflection of Java Constructor
We can also inspect different constructors of a class using various methods provided by the Constructor
class.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import java.lang.Class; import java.lang.reflect.*; class Dog { // public constructor without parameter public Dog() { } // private constructor with a single parameter private Dog(int age) { } } class Main { public static void main(String[] args) { try { // create an object of Dog Dog d1 = new Dog(); // create an object of Class // using getClass() Class obj = d1.getClass(); // get all constructors of Dog Constructor[] constructors = obj.getDeclaredConstructors(); for (Constructor c : constructors) { // get the name of constructors System.out.println("Constructor Name: " + c.getName()); // get the access modifier of constructors // convert it into string form int modifier = c.getModifiers(); String mod = Modifier.toString(modifier); System.out.println("Modifier: " + mod); // get the number of parameters in constructors System.out.println("Parameters: " + c.getParameterCount()); System.out.println(""); } } catch (Exception e) { e.printStackTrace(); } } } |
Output:
1 2 3 4 5 6 7 | Constructor Name: Dog Modifier: public Parameters: 0 Constructor Name: Dog Modifier: private Parameters: 1 |