Is there any built-in Java6 method (perhaps in lang or reflection?) for performing:
private Class[] getTypes(final Object[] objects) {
final Class[] types = new Class[objects.length];
for (int i = 0; i < objects.length; i++) {
types[i] = objects[i].getClass();
}
return types;
}
Which takes an Object array and returns an array containing the type of each element?
No, there’s no built-in facility for this in JavaSE.
Not much of a burden, surely, it’s easily unit-testable and only a few lines.
If you really wanted something you don’t write yourself, there are various 3rd-party libraries that will do it for you (e.g. Apache Commons Lang’s
ClassUtils, CGLIB’sReflectUtils), so if you already have one of those, you can use them.