mercredi 14 juillet 2021

How to get the class of generic T of an interface

So i want to execute the following method, but i cannot find a way to access the Model.class and specific the Item.class

public interface DataRepository<Model> {

default List<Model> getAll() throws Exception{

    return (List<Model>) getQueryManager().getAll( -Model.class- );

}

The interface is implemented this way:

public class ItemService implements DataRepository<Item> {

public ItemService() throws Exception {

    getAll();

}

I have tried the following :

public class ItemService extends Service<Item> implements DataRepository<Item> {

public ItemService() throws Exception {

    super(Item.class);

    getAll();

}

public abstract class Service<T> {

Class<T> clazz;

protected Service(Class<T> clazz){

    this.clazz = clazz;

}

protected Class<T> getT(){

    return clazz;

};

}

And then use the getMethod("getT").invoke() method but i get NoSuchMethodException





Aucun commentaire:

Enregistrer un commentaire