Java Reflection

Java Reflection

1. Overview

In this article, we will be studying Java reflection at a basic level. This allows us to inspect and modify runtime attributes of classes, interfaces, fields and methods.

2. Setup

Here there is no big deal, we do not need to include any Maven dependencies or configuration, all we need to do is to import the package:

import java.lang.reflect.*;

3. Simple example

Firstly, we'll create a simple class that contains fields and methods with different access modifiers (we'll need them later to see Java Reflection in action)

public class Vehicle {
    private Double price;
    protected static Double capacity;
    public String company;


    public void sayHello(){
        System.out.println("Hello!");
    }

    private void sayHelloPrivate(){
        System.out.println("Hello, I am private!");
    }
}

You cannot create a Class instance, but there are patterns wherewith you can get an instance: a) Using getClass()

Vehicle v = new Vehicle();
Class<? extends Vehicle> myClass = v.getClass();
System.out.println(myClass);

b)By its name

Class<?> vehicleClassByName = Class.forName("com.company.Vehicle");
System.out.println(vehicleClassByName);

Now, we will use Java Reflection to discover attributes of our class.

a) Getting the Super Class

Class<?> vehicleSuperClass = vehicleClass.getSuperclass();

b) Getting the interfaces directly implemented by the class

Class<?>[] interfaces = vehicleClass.getInterfaces();

c) Getting the fields

There are 2 methods we can use to get the fields, as you can see below:

 Field[] fields = vehicleClass.getFields();
 Field[] declaredFields = vehicleClass.getDeclaredFields();

The difference between them is that the getField() method returns only the public fields (including inherited fields), while the getDeclaredFields() method returns all the fields within our class(regardless of the access modifier)

We can also get a method by using getField() method which we pass the name of the field we want to get.

 Field vehicleCompanyField = vehicleClass.getField("company");

d) Getting the methods

Just like in the case above, there are 2 methods:

Method[] declaredMethods = vehicleClass.getDeclaredMethods();
Method[] methods = vehicleClass.getMethods();

The difference between them is that the getMethods() method returns only the public methods (including inherited methods), while the getDeclaredMethods() method returns all the methods within our class(regardless of the access modifier) We can also get a method by using getMethod() method which we pass the name of the method we want to get and the type of parameters.

Method sayHelloPrivateMethod = Vehicle.class.getMethod("sayHello");

e)Getting the constructor

In contrast to the method case, we don't have access to the constructor/s of the superclass.

Constructor[] declaredConstructors = Vehicle.class.getDeclaredConstructors();

f) Getting modifiers for a field

Field vehicleCompanyField = vehicleClass.getField("company");
int modifiers = vehicleCompanyField.getModifiers();
System.out.println(modifiers);
System.out.println(Modifier.isPublic(modifiers));

We can use isPublic(), isPrivate(), isAbstract(), isFinal()..etc to check conditions based on modifiers type.