How do I bind a Generic class?

5k Views Asked by At

When I try to mock a Dao using Jukito I get the following exception:

java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
    at org.jukito.JukitoModule.addKeyDependency(JukitoModule.java:338)
    at org.jukito.JukitoModule.addInjectionPointDependencies(JukitoModule.java:330)
    at org.jukito.JukitoModule.addDependencies(JukitoModule.java:313)

The object I try to mock is a ConcreteDao.

public class ConcreteDao extends AbstractDao<MyDomain> {
}

public abstract class AbstractDao<T extends DatastoreObject> {
}

I read several posts on SO about this binding generics but I can't figure out a way to use TypeLiteral for my binding.

This is what I tried:

bind(new TypeLiteral<AbstractDao<MyDomain>>(){}).to(ConcreteDao.class);
1

There are 1 best solutions below

0
Jamol On

You need to bind like this:

bind(new TypeLiteral<AbstractDao<MyDomain>>(){}).to(new TypeLiteral<ConcreteDao<MyDomain>>(){});

This is how you can retrieve the generic class:

class AbstractDao {
  protected final Class<T> clazz;

  @Inject
  public AbstractDao(TypeLiteral<T> type) {
    clazz = (Class<T>) type.getRawType();
  }
}

Subclasses of AbstractDao will need to pass entity specific TypeLiterals to the parent class (AbstractDao):

class ConcreteDao extends AbstractDao<MyDomain> {
  @Inject
  public ConcreteDao(TypeLiteral<MyDomain> type) {
    super(type);
  }
}

Note that you can make your AbstractDao class non-abstract and implement basic CRUD operations, so that you can use it without the need to extend AbstractDao for each entity. You will just need a binding for each entity like this:

bind(new TypeLiteral<GenericDao<User>>(){}).in(Scopes.SINGLETON);

See my blog-post for more information.