Java - Reflection Class Reflection

Introduction

List class access modifiers, its name, its superclass name, and all interfaces implemented

To get the simple class name, use the getSimpleName() method of the Class class:

String simpleName = c.getSimpleName();

getModifiers() method of the Class class returns all modifiers for the class.

To get the textual form of the modifiers, call the toString(int modifiers) from java.lang.reflect.Modifier class.

int mod = c.getModifiers() & Modifier.classModifiers();
String modifiers = Modifier.toString(mod);

getSuperclass() method of the Class class gets the reference of the superclass.

If the getSuperclass() method is invoked on the Object class, it returns null since Object class does not have super class.

To get the names of all interfaces implemented, use the getInterfaces() method.

Class[] interfaces = c.getInterfaces();

Demo

import java.io.Serializable;
import java.lang.reflect.Modifier;
import java.lang.reflect.TypeVariable;

class Person implements Cloneable, Serializable {
  private int id = -1;
  private String name = "Unknown";

  public Person() {
  }//from ww  w. j a va  2s.c o m

  public Person(int id, String name) {
    this.id = id;
    this.name = name;
  }

  public int getId() {
    return id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Object clone() {
    try {
      return super.clone();
    } catch (CloneNotSupportedException e) {
      throw new RuntimeException(e.getMessage());
    }
  }

  public String toString() {
    return "Person: id=" + this.id + ", name=" + this.name;
  }
}

public class Main {
  public static void main(String[] args) {
    // Print the class declaration for the Person class
    String classDesciption = getClassDescription(Person.class);
    System.out.println(classDesciption);
  }

  public static String getClassDescription(Class c) {
    StringBuilder classDesc = new StringBuilder();

    // Prepare the modifiers and construct keyword (class, enum, interface etc.)
    int modifierBits = 0;
    String keyword = "";

    // Add keyword @interface, interface or class
    if (c.isPrimitive()) {
      // We do not want to add anything
    } else if (c.isInterface()) {
      modifierBits = c.getModifiers() & Modifier.interfaceModifiers();

      // AN annotation is an interface
      if (c.isAnnotation()) {
        keyword = "@interface";
      } else {
        keyword = "interface";
      }
    } else if (c.isEnum()) {
      modifierBits = c.getModifiers() & Modifier.classModifiers();
      keyword = "enum";
    } else {
      modifierBits = c.getModifiers() & Modifier.classModifiers();
      keyword = "class";
    }

    // Convert modifiers to their string representation
    String modifiers = Modifier.toString(modifierBits);

    // Append modifiers
    classDesc.append(modifiers);

    // Append the construct keyword
    classDesc.append(" " + keyword);

    // Append simple name
    String simpleName = c.getSimpleName();
    classDesc.append(" " + simpleName);

    // Append generic parameters
    String genericParms = getGenericTypeParams(c);
    classDesc.append(genericParms);

    // Append super class
    Class superClass = c.getSuperclass();
    if (superClass != null) {
      String superClassSimpleName = superClass.getSimpleName();
      classDesc.append(" extends " + superClassSimpleName);
    }

    // Append Interfaces
    String interfaces = getClassInterfaces(c);
    if (interfaces != null) {
      classDesc.append(" implements " + interfaces);
    }

    return classDesc.toString();
  }

  public static String getClassInterfaces(Class c) {
    // Get a comma-separated list of interfaces implemented by the class
    Class[] interfaces = c.getInterfaces();
    String interfacesList = null;
    if (interfaces.length > 0) {
      String[] interfaceNames = new String[interfaces.length];
      for (int i = 0; i < interfaces.length; i++) {
        interfaceNames[i] = interfaces[i].getSimpleName();
      }
      interfacesList = String.join(", ", interfaceNames);
    }
    return interfacesList;
  }

  public static String getGenericTypeParams(Class c) {
    StringBuilder sb = new StringBuilder();
    TypeVariable<?>[] typeParms = c.getTypeParameters();

    if (typeParms.length > 0) {
      String[] paramNames = new String[typeParms.length];
      for (int i = 0; i < typeParms.length; i++) {
        paramNames[i] = typeParms[i].getTypeName();
      }

      sb.append('<');
      String parmsList = String.join(",", paramNames);
      sb.append(parmsList);
      sb.append('>');
    }
    return sb.toString();
  }
}

Result

Exercise