Object [] to class [] in Java

Is there a built-in Java6 method (possibly in lang or reflection?) To execute:

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;
}

What takes an array of Object and returns an array containing the type of each element?

+5
source share
4 answers

No, there is no built-in tool in JavaSE for this.

Not much of a burden, of course, you can easily test with just a few lines.

If you really want something that you don’t write, there are different third-party libraries that will do this for you (for example, Apache Commons Lang ClassUtils, CGLIB ReflectUtils), so if you already have one of those, you can use them.

+5
source
+2
+1

, . , , . . , .

, ab . , , . , "" JRE.

+1

All Articles