It is a bit stupid that Class
has no method to give the corresponding array class, only the other way around. Here is a workaround:
<E> public static Class<E[]> arrayClass(Class<E> elementClass) {
@SuppressWarnings("unchecked")
Class<E[]> arrayClass = (Class<E[]>) Array.newInstance(elementClass, 0).getClass();
return arrayClass;
}
With this method you could write
Class cls = Foo.class;
Class[] types = { arrayClass(Integer.class) };
cls.getMethod("bar", types);
Of course, if you can write Integer.class
, you could also write Integer[].class
. So this is only useful if you know your method takes an array of some class which you only get as a class object, otherwise use the answers given by Bozho.