vendredi 2 mars 2018

Java Reflection: obtain a Array class from an object class

Supposing I have MyObject class, i need a generic array from a generic type.

MyObject object;
MyObject array[] = (MyObject) genericFunction(data,object.getClass()) ;


public Object[] genericFunction(Object someData, Class<?> objectClass) ;

There is a way to get MyObject[].class from MyObject.class via reflection? Actually I call my method as

MyObject array[] = (MyObject[]) genericFunction(data,MyObject[].class, MyObject.class()) 

and I would remove this sort of redundancy





Aucun commentaire:

Enregistrer un commentaire