Guice binding for generic types not working with ".class"

307 Views Asked by At

I am trying to bind a Generic class dependencies using Guice

public class Myclass<T> {
    @Inject
    public Myclass(Class<T> clazz) {
    ...
    }
} 

I am trying to create a Myclass<List<Integer>> but the binding fails for me

private static class ListClassType extends TypeLiteral<Class<List<Integer>>> {}

bind(new ListClassType()).toInstance(List<Integer>.class);

But this gives me a compilation error since List<Integer>.class is invalid. Without it I get a runtime exception since Guice is not able to bind generics.

Can someone please help me figure out how to solve the problem? I need to get the binding working.

0

There are 0 best solutions below