devxlogo

Reflection – Constructors and array params

Reflection – Constructors and array params

Question:
I need to call a constructor of a dynamically loaded class, and thatconstructor takes a single parameter which is an array of strings,(i.e., constructor(String [] args). How can I do this using reflection?

Answer:
Reflection can be a tiresome business, involving some arcane method calls and lots of exception catching. Getting a constructor with reflection works just about the same way as getting a method. Once you have your class reference, you have to set up the parameter signature of the constructor by constructing an array containingreferences to the class types of the parameters. This can be done rather easily by using Class.forName(String classname).The only catch is that array parameters require a funky naming format that is not documented in the API docs, but can be discovered throughexperimentation or looking at the Java Language Specification. Array class names are represented by a sequence of [‘s, one for each dimension of the array, then a capital L followed by the class name of type stored by the array, ended with a semicolon. For example, a String array is referenced as “[Ljava.lang.String;” Once you knowthis, life becomes simpler. Just useClass.getConstructor to fetch the constructor reference, using your parameter type array as an argument. New object instancescan be created by invoking the contructors newInstance method, passing it an array of parameters expected by the constructor.The following listing demonstrates all of these steps, in addition to also dynamically invoking a method that prints the values of the parameter passed to the constructor.

public final class ConstructorReflection {  private String[] __strArray;  public ConstructorReflection(String[] strArray) {    __strArray = strArray;  }  public void print() {    for(int i=0; i < __strArray.length; i++)      System.out.println(__strArray[i]);  }  public static final void main(String[] args) {    Constructor constructor;    Method method;    Class[] parameterTypes = new Class[1];    Object[] parameters;     Class clss;    Object obj;    try {      // Get the class corresponding to java.lang.String[], which      // is the parameter type for the contructor.      parameterTypes[0] = Class.forName("[Ljava.lang.String;");    } catch(ClassNotFoundException e) {      e.printStackTrace();      return;    }    clss = ConstructorReflection.class;    try {      constructor = clss.getConstructor(parameterTypes);    } catch(NoSuchMethodException e) {      e.printStackTrace();      return;    }    // Set up the constructor parameters before creating a new instance    parameters    = new Object[1];    parameters[0] = new String[] { "One", "Two", "Three", "Four" };    try {      obj = constructor.newInstance(parameters);    } catch(InstantiationException e) {      e.printStackTrace();      return;    } catch(IllegalAccessException f) {      f.printStackTrace();      return;    } catch(InvocationTargetException g) {      g.printStackTrace();      return;    }    try {      method = obj.getClass().getMethod("print", null);    } catch(NoSuchMethodException e) {      e.printStackTrace();      return;    }    try {      method.invoke(obj, null);    } catch(IllegalAccessException e) {      e.printStackTrace();      return;    } catch(InvocationTargetException f) {      f.printStackTrace();      return;    }  }}
See also  Does It Make Sense to Splurge on a Laptop?
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist