Search code examples
javaguice

Can't bind TypeLiteral using Guice


I am beginning to learn Guice, but I have already encountered a problem. When I try to bind TypeLiteral I receive Cannot resolve method 'to(anonymous com.google.inject.TypeLiteral<>. I tried to look for cause of the problem, but no effect. Maybe anyone knows how to solve it?

Here is the class where I want to use bind().

public class MainModule extends AbstractModule {
    protected void configure(){
        bind(DeckFactory.class).to(BlackjackDeckCreator.class);
        bind(CardFactory.class).to(BlackjackCardCreator.class);         
        bind(PointsCalculator.class)
          .to(BlackjackPointsCalculator.class);           
        bind(Logic.class)
          .toProvider(CompositeGameLogicStrategyProvider.class);
// Problem
        bind(new TypeLiteral<Randomizer<BlackjackCard>>() { })
          .to(new TypeLiteral<BlackjackCardRandomizer>() {}); 
//    
        bind(DecisionTaker.class)
          .toProvider(CompositeDecisionTakerProvider.class);
        bind(StatisticPrinter.class)
          .to(ConsoleStatisticPrinter.class);        
        bind(HitGameLogicStrategy.class)
          .toProvider(HitGameLogicProvider.class);      
        bind(StandGameLogicStrategy.class)
          .toProvider(StandGameLogicProvider.class);
        install(new FactoryModuleBuilder().build(GameFactory.class));
        install(new FactoryModuleBuilder()
          .build(PlayerFactory.class));       
        bind(StatisticsTemplate.class)
          .toProvider(StatisticsTemplateProvider.class);
    }
}

Randomizer.java

public interface Randomizer<T extends Card> {
    T randomizeCard(List<T> deck);
}

BlackjackCardRandomizer.java

public class BlackjackCardRandomizer implements Randomizer {
    private static final Random RANDOM = new Random();

    @Override
    public Card randomizeCard(List deck) {
        Integer randIndex = RANDOM.nextInt(deck.size());
        return (Card) deck.get(randIndex);
    }
}

Thanks in advance!


Solution

  • You should use the following instead:

    bind(new TypeLiteral<Randomizer<BlackjackCard>>() { })
          .to(BlackjackCardRandomizer.class); 
    

    Just because you have a TypeLiteral as interface doesn't mean you have to use type literals as implementation.

    Also, change your extends:

    public class BlackjackCardRandomizer implements Randomizer<BlackjackCard> {
        private static final Random RANDOM = new Random();
    
        @Override
        public BlackjackCard randomizeCard(List<BlackjackCard> deck) {
            Integer randIndex = RANDOM.nextInt(deck.size());
            return (Card) deck.get(randIndex);
        }
    }