Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java Language
Expertise: Beginner
Dec 4, 1998

Reflection - Constructors and array params

Question:
I need to call a constructor of a dynamically loaded class, and that constructor 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 containing references 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 through experimentation 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 know this, life becomes simpler. Just use Class.getConstructor to fetch the constructor reference, using your parameter type array as an argument. New object instances can 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;
    }
  }
}

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date