How do we create generic object where the type of the object is a variable in java?

Viewed 35

I have a map like this - Map<Class<? extends Entity>, Config<? extends Entity>> entityClassToConfigMap

Entries in this class are like: <EntityA.class, Config<EntityA>> where EntityA extends Entity. Now I want to iterate over this map and based on the keys in the map I want to create more generic objects like:

switch (entityKey.class) {
   case (EntityA.class):
       return Repository<EntityA>
    case (EntityB.class):
       return Repository<EntityB>
...
}

I know switch cannot work like that. Is there any other alternative? I am using dagger for dependency injection so also tried building providers using @IntoMap and @ClassKey but that too doesn't work.

Any pointers will be appreciated. Thanks!

0 Answers
Related