Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Get Class Modifiers and Interfaces through Reflection

1. About this Java Reflection Example

In this example, we will retrieve the access modifiers of a Type through Java Reflection. Then, we will also retrieve base class of a type followed by all the interfaces implemented by it. Now, we will look at the depiction below:

Java Type Modifiers

Java Type Modifiers

Here, the type we will use is Products class. This class has two modifiers set to it. They are public and final. Public states the class can be instantiated anywhere with no restriction and final states that Products class allows no further inheritance. We derived the Products class from Organization class and we retrieve that information also through java reflection. Finally, we will retrieve all the interface implemented in the Products class. In our case, the implemented interfaces are IOrg and IProduct.

2. IOrg and IProduct Interfaces

First, let us define the interfaces for this example. Note, all the code that we will write here do nothing as it is just for exploring java reflection. The IOrg is defined in the IOrg.java and IProduct is in the IProduct.java. Below is the interface code:

//IOrg.java
public interface IOrg
{
    public void IOrgMethod();
}
 
//IProduct.java
public interface IProduct
{
    public void IProductMethod();
}

3. Class Modifiers of Organization and Products Class

We define Organization class in the file Organization.java. This Organization class acts as the base class for the Products and we will define it next. Code for the Organization class is below:

//Organization.java
public class Organization
{
    int SomeMember;
}

The Products class has Organization as a base class. It also implements two interfaces which we defined in the earlier section. We will use Java Reflection on the Products class and will retrieve below details:

  • Type Modifiers such as public and final
  • Base class and in our case, it is Organization
  • All the interfaces it implements.

We define the Products class in a file called Products.java and code is given below:

//Products.java
public final class Products extends Organization
    implements IOrg, IProduct
{
    private int SomeMember;
    public void IOrgMethod() {
 
    }
    public void IProductMethod() {
 
    }
}

The complete Type relationship is shown below:

Type Relationship

Type Relationship

4. Get Class Modifiers Using getModifiers()

We use the getModifier function of the Java Reflection Class to get all the modifier of a class object. It returns a number and we need to do bit-wise operation to test against a specific modifier. In our case, we will test object of the product class is public or not. Similarly, we will do a test and take a judgment of if the class is inheritable. Recall, the class marked with Final modifier is not inheritable.

First, we create the instance of a Products class and store that in a reference called, prod. Below is the code for it:

public static void main(String[] args)
{
    //Sample 01: Create Product class instance
    Products prod = new Products();

We use the prod reference to get the Java Reflection Class for Products by calling the getClass method on it. Once we have the Class, we call getModifiers method to get all the Modifier bits packed in a single integer variable. In our example, we are storing it in the variable called Modifiers and later we will do our bit-wise test on this variable. The code for this is below:

//Sample 02: Get Modifiers for the Class
Class<?> prodClass = prod.getClass();
int Modifiers = prodClass.getModifiers();

The Modifier class of the Java Reflection package declares all the Modifier Constants. For example, if we want to check whether a class instance is public, we need to perform Bit-wise AND on the number returned by the getModifiers call with the constant PUBLIC. If the integer has public bit set in it; we get a value which is greater than zero. This kind of test will tell us a class has a modifier or not.

In the below code snippet, we check the Products class is public and also Final. Note that while performing a Wit-wise AND, we are making use of the constants like Modifier.FINAL to check the class is Final or not. In the same way, we make use of Modifier.PUBLIC to decide a class public or private or protected.

//Sample 03: Check Product class is Public or Not
if ( (Modifiers & Modifier.PUBLIC) > 0)
    System.out.println("Product Class is Public");
if ( (Modifiers & Modifier.FINAL) > 0)
    System.out.println("Product Class is Final");
else
    System.out.println("Product Class can be Inherited");

5. Getting Base Class Modifiers Using getSuperclass

We can get a base class of a class by calling the getSuperclass method on the Reflection Class instance. In the below code we use prodClass Class instance and get its base class by calling the getSuperclass. Once we have the base Class, we are using the same modifier test which we used in the previous section. The code is below:

//Sample 04: Get the Organization class Modifiers
Class<?> orgClass = prodClass.getSuperclass();
Modifiers = orgClass.getModifiers();
if ( (Modifiers & Modifier.PUBLIC) > 0)
    System.out.println("Organization Class is Public");
if ( (Modifiers & Modifier.FINAL) > 0)
    System.out.println("Organization Class is Final");
else
    System.out.println("Organization Class can be " +
            "Inherited");

6. Get all Implemented Interfaces & Its Type Modifiers

One can call the getInterfaces method on the run-time class using Java Reflection. It returns the array of interfaces implemented by the run-time Class object. For example, the Products class implements two interfaces and when we call this method on it, we get these two interfaces packed in an array.

Before we proceed further, we will have a look at the code given below. In the below code we get the Interface array of the Products class. After that we iterate through it to list all the modifiers. Note, this time we are not checking for a specific Modifier like we did earlier and despite that, we are getting the modifiers as usual by calling getModifiers. And then we give that to Modifier.toString method to list all its modifiers. Below is the code snippet:

//Sample 05: Let us get all the interface implemented by
//the Product class
Class<?>[] lstInterfaces = prodClass.getInterfaces();
for (Class<?> Interface:lstInterfaces)
{
    System.out.println("Interface :" +
            Interface.getSimpleName());
    System.out.println("List of Modifiers =>" +
            Modifier.toString(Interface.getModifiers()));
}

The complete code example from Main.java is below:

import java.lang.reflect.Modifier;
 
public class Main
{
    public static void main(String[] args)
    {
        //Sample 01: Create Product class instance
        Products prod = new Products();
 
        //Sample 02: Get Modifiers for the Class
        Class<?> prodClass = prod.getClass();
        int Modifiers = prodClass.getModifiers();
 
        //Sample 03: Check Product class is Public or Not
        if ( (Modifiers & Modifier.PUBLIC) > 0)
            System.out.println("Product Class is Public");
        if ( (Modifiers & Modifier.FINAL) > 0)
            System.out.println("Product Class is Final");
        else
            System.out.println("Product Class can be " +
                    "Inherited");
 
        //Sample 04: Get the Organization class Modifiers
        Class<?> orgClass = prodClass.getSuperclass();
        Modifiers = orgClass.getModifiers();
        if ( (Modifiers & Modifier.PUBLIC) > 0)
            System.out.println("Organization Class " +
                    "is Public");
        if ( (Modifiers & Modifier.FINAL) > 0)
            System.out.println("Organization Class " +
                    "is Final");
        else
            System.out.println("Organization Class " +
                    "can be Inherited");
 
        //Sample 05: Let us get all the interface 
        // implemented by
        //the Product class
        Class<?>[] lstInterfaces = 
                prodClass.getInterfaces();
        for (Class<?> Interface:lstInterfaces)
        {
            System.out.println("Interface :" +
                    Interface.getSimpleName());
            System.out.println(
                    "List of Modifiers =>" + 
                    Modifier.toString(
                            Interface.getModifiers())
            );
        }
    }
}

Output

Output of the Example Code

Output of the Example Code

Categories: Java

Tags: , , , ,

Do you like this Example? Share your thoughts!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.