Guice: "Could not find suitable constructor" when suitable constructor supplied in FactoryBuilder

StackOverflow https://stackoverflow.com/questions/23680901

質問

I'm using Guice for dependency injection, but I'm getting this error:

1) Could not find a suitable constructor in java.lang.Void. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
  at java.lang.Void.class(Void.java:44)
  while locating java.lang.Void
  at com.myfeed.algorithm.clusterer.tree.wrapper.ItemSourceTreeWrapperFactory.create(ItemSourceTreeWrapperFactory.java:1)

What's especially odd is that it mentions java.lang.Void.

This is the module code: (I bind a class to itself, not an interface to a class)

install(new FactoryModuleBuilder().implement(
     new TypeLiteral<ItemSourceTreeWrapper<Document>>(){}, ItemSourceTreeWrapper.class
).build(new TypeLiteral<ItemSourceTreeWrapperFactory<Document>>(){}));

Here is ItemSourceTreeWrapper constructor:

@Inject
public ItemSourceTreeWrapper(@Assisted Tree<Item<Source>> treeToWrap, @Assisted ItemGroup itemGroup, Itemizer<Source> itemizer, SourceItemConverterFactory sourceItemConverterFactory) {
    this.treeToWrap = treeToWrap;
    this.itemizer = itemizer;
    sourceItemConverter = sourceItemConverterFactory.create(itemGroup, itemizer);
}

And here is the factory interface:

public interface ItemSourceTreeWrapperFactory<Source> {
    public void create(Tree<Item<Source>> treeToWrap, ItemGroup<Source> itemGroup);
}
役に立ちましたか?

解決

Your call to implement() looks suspicious - since ItemSourceTreeWrapper is generic, the second parameter should likely be using a TypeLiteral<>. Furthermore, since you're implementing a class with itself, you should be able to omit the implement() call entirely.

But your actual problem is that in ItemSourceTreeWrapperFactory, public void create(...) should probably be public ItemSourceTreeWrapper<Source> create(...).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top