Creating an object variable out of its meta object

I have a method (in a class which represent a set of objects) that receive as a parameter a Class object, and I need to find all the objects in the set that have this Class object as their meta-object.

The problem is that i need to return them as an array of their type, and i don't know how to get the objects type.

So in other words, i need to create an array, which his type is depends on the Class object, for example:

public Object[] getAllFromType(Class meta){
    Object[] temp = new meta.getDynamicType[fullCells];

    ...
    ...
    ...
    return temp;
}

Is it possible?

Thanks.

Jon Skeet
people
quotationmark

Well, as you've shown you're going to return it as Object[] anyway, so you won't get a compile-time benefit here, but you can create an array of the right type easily enough:

Object[] array = (Object[]) Array.newInstance(meta, fullCells);

Note that the cast to Object[] is required because Array.newInstance returns Object - the cast will fail for int[] for example, because an int[] isn't an Object[]. It will work if the array element type is any class or interface type, however.

people

See more on this question at Stackoverflow