Reflection – Constructors and array params

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?

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;    }  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.