Domanda

I've installed a factory in Google Guice with AssistedInject, but I get the following error (I'm running unit tests with JUnit):

com.google.inject.CreationException: Guice creation errors:

1) No implementation for clusterestimator.OptimalClusterEstimatorFactory was bound.
  while locating clusterestimator.OptimalClusterEstimatorFactory
    for parameter 0 at com.myfeed.algorithm.clusterer.tree.fca.BasicFCATreeFactory.<init>(BasicFCATreeFactory.java:16)
  FCAModule.configure(FCAModule.java:29)

This error is the same even if I omit the install(new Factory...); line from my module, which makes me think that the line is somehow being ignored.

Here is the module code:

public class FCAModule extends AbstractModule {

    @Override
    protected void configure() {
        install(new FactoryModuleBuilder() // <-- Factory line that's not working
            .implement(OptimalClusterEstimator.class, FCAOptimalClusterEstimator.class)
            .build(OptimalClusterEstimatorFactory.class));
        bind(ValueWell.class).to(MapBackedValueWell.class).asEagerSingleton();
        bind(FCATreeFactory.class).to(BasicFCATreeFactory.class).asEagerSingleton();
        bind(ItemFactory.class).to(MapBackedItemFactory.class).asEagerSingleton();
        bind(ClustererFactory.class).asEagerSingleton();
        bind(ClusterFactory.class).to(MemoryBackedClusterFactory.class).asEagerSingleton();
    }

}

Here is the factory interface:

public interface OptimalClusterEstimatorFactory {
    public <T> OptimalClusterEstimator createFCA(int kValue, ItemPointReducer<T> pointReducer);
}

Here is the constructor of FCAOPtimalClusterEstimator:

@AssistedInject
public FCAOptimalClusterEstimator(@Assisted int kValue, @Assisted ItemPointReducer<T> pointReducer) {
    this.kValue = kValue;
    this.pointReducer = pointReducer;
}

Here is the constructor for BasicFCATreeFactory, the first thing to call for the other factory. Note that this factory is not created using AssistedInject because it uses generics.

@Inject
public BasicFCATreeFactory(OptimalClusterEstimatorFactory optimalClusterEstimatorFactory, ClustererFactory clustererFactory, ClusterFactory clusterFactory) {
    this.optimalClusterEstimatorFactory = optimalClusterEstimatorFactory;
    this.clustererFactory = clustererFactory;
    this.clusterFactory = clusterFactory;
}
È stato utile?

Soluzione

For some unknown reason, removing the <T> generic part from the factory interface solved the problem. I've had previous issues with generics and Guice, so maybe this is just another one of those quirks because of type erasure.

So the factory interface is now:

public interface OptimalClusterEstimatorFactory {
    public OptimalClusterEstimator createFCA(int kValue, ItemPointReducer pointReducer);
}

Altri suggerimenti

Guice assisted injection doesn't work well with generics. If you want to use generics then you have write your own factory.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top