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's ReflectUtils
), so if you already have one of those, you can use them.
In JDK - no. There is in apache commons-lang:
ClassUtils.toClass(Object[] objects)
But writing it yourself isn't painful at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With