When using reflection, how do I locate a method / constructor that requires an array argument, as in fooBar(String[])?
Created May 4, 2012
Array type names in Java uses '[' to denote a dimension. So for a one dimensional String (String[]) the type name of the array is "[Ljava.lang.String;" for a 2D array (eg String[][]), it is "[[Ljava.lang.String;"
Note that for classes of objects, you need to terminate the name of the type with a ';'.
If the array is an array of a primitive type eg (int, float, etc), then you need to use a special character to denote the type. The following are mappings for common primitive types:
int: I float: F boolean: Z byte: B char: C long: J double: D short: S
Also note that the type name for these arrays do not terminate with the ';' character. For example, the type name for "int[]" will be "[I".
The following example illustrates how methods that requires array arguments can be located and invoked with reflection:
import java.lang.reflect.*; // Example class class MyTest{ public void oneDExample(String[] strs){ System.out.println(strs[0]); } public void twoDExample(String[][] strs){ System.out.println(strs[0][0]); } public void threeDExample(int[][][] ints){ System.out.println("Three Dimension Example Invoked: " + ints[0][0][0]); } } public class Test{ static public void main(String []args){ try{ MyTest aTest = new MyTest(); // Instance of MyTest Class aClass = Class.forName("MyTest"); // Class object of MyTest Class[] para = new Class[1]; // Parameter List Object[] arg = new Object[1]; // Argument List Method m = null; // Method reference // One Dimension String Example String[] oneDStr = new String[1]; oneDStr[0] = "One Dimension Example Invoked"; para[0] = Class.forName("[Ljava.lang.String;"); m = aClass.getMethod("oneDExample", para); arg[0] = oneDStr; m.invoke(aTest, arg); // Two Dimension String Example String[][] twoDStr = new String[1][1]; twoDStr[0][0] = "Two Dimension Example Invoked"; para[0] = Class.forName("[[Ljava.lang.String;"); m = aClass.getMethod("twoDExample", para); arg[0] = twoDStr; m.invoke(aTest, arg); // Three Dimension Int Example int[][][] threeDInt = new int[1][1][1]; threeDInt[0][0][0] = 10000; para[0] = Class.forName("[[[I"); // note no ';' at the end of name! m = aClass.getMethod("threeDExample", para); arg[0] = threeDInt; m.invoke(aTest, arg); } catch(Exception e){System.out.println(e); } } }