Java - Reflection Constructor Reflection

Introduction

The following four methods in the Class class returns information about the constructors represented by a Class object:

Method
Description
Constructor[] getConstructors()
returns all public constructors.
Constructor[] getDeclaredConstructors()

returns all declared constructors. It does not return any constructors that are inherited from the
superclass.
Constructor<T> getConstructor(Class... parameterTypes)
get the Constructor object if you know the parameter types of the constructor.
Constructor<T> getDeclaredConstructor(Class... parameterTypes)

get the Constructor object if you know the parameter types of the constructor. It does not return any constructors that are inherited from the
superclass.

Demo

import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    Class<String> c = String.class;

    // Get the declared constructors
    System.out.println("Constructors for " + c.getName());
    Constructor[] constructors = c.getConstructors();
    ArrayList<String> constructDescList = getConstructorsDesciption(constructors);
    for (String desc : constructDescList) {
      System.out.println(desc);/*from  w w  w  . ja  va 2  s.c  o  m*/
    }
  }

  public static ArrayList<String> getConstructorsDesciption(Constructor[] constructors) {
    ArrayList<String> constructorList = new ArrayList<>();
    for (Constructor constructor : constructors) {
      String modifiers = ExecutableUtil.getModifiers(constructor);

      // Get the name of the constructor
      String constructorName = constructor.getName();

      // Get the parameters of the constructor
      ArrayList<String> paramsList = ExecutableUtil.getParameters(constructor);
      String params = ExecutableUtil.arrayListToString(paramsList, ",");

      // Get the Exceptions thrown by the constructor
      String throwsClause = ExecutableUtil.getThrowsClause(constructor);

      constructorList.add(modifiers + " " + constructorName + "(" + params + ") " + throwsClause);
    }
    return constructorList;
  }
}

class ExecutableUtil {
  public static ArrayList<String> getParameters(Executable exec) {
    Parameter[] parms = exec.getParameters();
    ArrayList<String> parmList = new ArrayList<>();
    for (int i = 0; i < parms.length; i++) {
      // Get modifiers, type, and name of teh parameter
      int mod = parms[i].getModifiers() & Modifier.parameterModifiers();
      String modifiers = Modifier.toString(mod);
      String parmType = parms[i].getType().getSimpleName();
      String parmName = parms[i].getName();
      String temp = modifiers + " " + parmType + " " + parmName;

      // Trim it as it may have leading spaces when modifiers are absent
      parmList.add(temp.trim());
    }
    return parmList;
  }

  public static ArrayList<String> getExceptionList(Executable exec) {
    ArrayList<String> exceptionList = new ArrayList<>();
    for (Class<?> c : exec.getExceptionTypes()) {
      exceptionList.add(c.getSimpleName());
    }
    return exceptionList;
  }

  public static String getThrowsClause(Executable exec) {
    ArrayList<String> exceptionList = getExceptionList(exec);
    String exceptions = ExecutableUtil.arrayListToString(exceptionList, ",");
    String throwsClause = "";
    if (exceptionList.size() > 0) {
      throwsClause = "throws " + exceptions;
    }

    return throwsClause;
  }

  public static String getModifiers(Executable exec) {
    // Get the modifiers for the class
    int mod = exec.getModifiers();
    if (exec instanceof Method) {
      mod = mod & Modifier.methodModifiers();
    } else if (exec instanceof Constructor) {
      mod = mod & Modifier.constructorModifiers();
    }
    return Modifier.toString(mod);
  }

  public static String arrayListToString(ArrayList<String> list, String saparator) {
    String[] tempArray = new String[list.size()];
    tempArray = list.toArray(tempArray);
    String str = String.join(saparator, tempArray);
    return str;
  }
}

Result