Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics from interface method are stripped

Tags:

java

generics

I have this interface:

public interface Persistant<T extends BaseDaoEnabled<T, UUID>> extends Identifiable {

T getSelf();

default <P> P getInstance(DataKey<P> key) {
    return key.getDefaultInstance();
}

and a class that implements it. My problem is that when I call obj.getInstance(DataKeys.X), X being a DataKey<X> object, the return type of this method is Object and not X, the generic type is stripped. When I put the method inside my object class (not interface), it works just fine. See the screenshot below:

First method is from the interface, 2nd method is from the class. They both have the SAME signature

enter image description here

like image 856
Eufranio Avatar asked Nov 08 '22 05:11

Eufranio


1 Answers

The interface has a type parameter (Persistant<T...>) that I forgot to include on my Resident/obj class. I was implementing it by just using implements Persistant, implementing it using implements Persistant<Resident> fixed my issue!

like image 179
Eufranio Avatar answered Nov 14 '22 22:11

Eufranio