Search code examples
javagenericsnested-generics

Java - issue initializing class with type parameters


I am having an issue initializing a class with type parameter. It seems to be a shortcoming of Java's type inference and I would like to know if there's a way around this or a better way of achieving this.

public class ParentModel {}

public class ChildModel extends ParentModel {}

public class Service<E extends ParentModel, T extends Collection<E>> {
    private Class<T> classOfT;
    private Class<E> classOfE;

    public Service(Class<E> classOfE, Class<T> classOfT) {
        this.classOfE = classOfE;
        this.classOfT = classOfT;
    }
}

public class BusinessLogic {
    public void someLogic() {
        Service<ChildModel, ArrayList<ChildModel>> service = new 
            Service<ChildModel, ArrayList<ChildModel>>(ChildModel.class, ArrayList.class);
    }
}

Compile-time error is in BusinessLogic::someLogic():

The constructor Service<ChildModel, ArrayList<ChildModel>>(Class<ChildModel>, Class<ArrayList>) is undefined

Compiled to Java 7.


Solution

  • Because generics in Java are implemented "by erasure", there is no Class<ArrayList<ChildModel>>>, only a Class<ArrayList>.

    What you can do is to allow supertypes.

    Class<? super T> classOfT;
    Class<? super E> classOfE;
    public Service(Class<? super E> classOfE, Class<? super T> classOfT) {
    

    alternatively, you can double-cast the class:

    Class<ArrayList<Integer>> clazz =
        (Class<ArrayList<Integer>>) (Class<? super ArrayList>) 
        ArrayList.class;
    

    But beware: the class is just ArrayList - Java will not perform additional type checks at runtime on the generics. See for yourself:

    ArrayList<Integer> a1 = new ArrayList<>();
    ArrayList<Double> a2 = new ArrayList<>();
    System.out.println(a1.getClass() == a2.getClass());
    

    It is the same class. At runtime, the generics are virtually gone