Guice AssistedInject 找不到匹配的构造函数

Guice AssistedInject can not find matching constructor

我刚开始学习Guice,但是我已经遇到了一个问题。我有一个接口 PlayerFactory 和一个实现 BlackjackPlayer

PlayerFactory.java

public interface PlayerFactory {
    Player createPlayer(String name);
    Player createPlayer(String name, boolean isDealer);
}

BlackjackPlayer.java

public class BlackjackPlayer implements PlayerFactory {
    private PointsCalculator pointsCalculator;

    public BlackjackPlayer(){
        pointsCalculator = new BlackjackPointsCalculator();
    }

    @Override
    public Player createPlayer(String name) {
        return new Player(pointsCalculator, name);
    }

    @Override
    public Player createPlayer(String name, boolean isDealer) {
        return new Player(pointsCalculator, name, isDealer);
    }
}

Player.class

public class Player{
    private PointsCalculator pointsCalculator;
    private List<Card> cardsInHand;
    private Integer points;
    private String name;
    private boolean isDealer;
    private boolean endedTurn;

    @AssistedInject
    public Player(PointsCalculator blackjackPointsCalculator, String name){
        pointsCalculator = blackjackPointsCalculator;
        cardsInHand = new ArrayList<>();
        points = 0;
        this.name = name;
        isDealer = false;
        endedTurn = false;
    }

    @AssistedInject
    public Player(PointsCalculator blackjackPointsCalculator, String name, boolean isDealer){
        pointsCalculator = blackjackPointsCalculator;
        cardsInHand = new ArrayList<>();
        points = 0;
        this.name = name;
        this.isDealer = isDealer;
        endedTurn = false;
    }

    public void addCardToHand(Card card) {
        cardsInHand.add(card);
        updatePoints();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Player)) return false;
        Player player = (Player) o;
        return getPoints() == player.getPoints() &&
            isDealer() == player.isDealer() &&
            isEndedTurn() == player.isEndedTurn() &&
            Objects.equals(pointsCalculator, player.pointsCalculator) &&
            Objects.equals(getCardsInHand(), player.getCardsInHand()) &&
            Objects.equals(getName(), player.getName());
    }

    @Override
    public int hashCode() {
        return Objects.hash(pointsCalculator, getCardsInHand(), getPoints(), getName(), isDealer(), isEndedTurn());
    }

    public void updatePoints() {
        points = pointsCalculator.calculatePoints(cardsInHand);
    }

    public List<Card> getCardsInHand(){
        return cardsInHand;
    }

    public Integer getPoints(){
        return points;
    }

    public String getName(){
        return name;
    }

    public boolean isDealer() {
        return isDealer;
    }

    public boolean isEndedTurn() {
        return endedTurn;
    }

    public void setName(String name){
        this.name = name;
    }

    public void setDealer(boolean isDealer){
        this.isDealer = isDealer;
    }

    public void setEndedTurn(boolean endedTurn){
        this.endedTurn = endedTurn;
    }
}

我想用Guice辅助注入创建Player。之前我是这样操作的:

install(new FactoryModuleBuilder().build(PlayerFactory.class));

我知道这是错误的方法,因为我收到错误消息:

1) com.github.blackjack.model.Player has @AssistedInject constructors, but none of them match the parameters in method com.github.blackjack.factory.PlayerFactory.createPlayer().  Unable to create AssistedInject factory.
while locating com.github.blackjack.model.Player
at com.github.blackjack.factory.PlayerFactory.createPlayer(PlayerFactory.java:1)

2) com.github.blackjack.model.Player has @AssistedInject constructors, but none of them match the parameters in method com.github.blackjack.factory.PlayerFactory.createPlayer().  Unable to create AssistedInject factory.
while locating com.github.blackjack.model.Player
at com.github.blackjack.factory.PlayerFactory.createPlayer(PlayerFactory.java:1)

我尝试添加构造函数 Player(String name)Player(String name, boolean isDealer) 但没有帮助。有人知道我应该怎么做才能解决这个问题吗?

提前致谢!

您需要在注入参数上使用 @Assisted 注释:

PlayerFactory.java

public interface PlayerFactory {
  Player createPlayer(String name);
  Player createPlayer(String name, boolean isDealer);
}

BlackjackPlayer.java(工厂改成播放器)

public class BlackjackPlayer implements Player {
  private final PointCalculator pointsCalculator;
  private final String name;
  private final boolean isDealer;
  @AssistedInject BlackjackPlayer(PointCalculator pointsCalculator, @Assisted String name) {
    this.pointsCalculator = pointsCalculator;
    this.name = name;
    this.isDealer = false;
  }
  @AssistedInject BlackjackPlayer(PointCalculator pointsCalculator, @Assisted String name, @Assisted boolean isDealer) {
    this.pointsCalculator = pointsCalculator;
    this.name = name;
    this.isDealer = isDealer;
  }
}

并使用如下模块:

install(new FactoryModuleBuilder()
 .implement(Player.class, BlackjackPlayer.class)
 .build(PlayerFactory.class)
);