Frage

I have a constructor that looks like this:

@Inject
public MyClass(
        @Named("config") String configFile,
        @Named("template") String templateFile,
        CachedSettings settings,
        @Assisted String channelId,
        @Nullable @Assisted("NetworkA") NetworkInterface localNetworkInterfaceA,
        @Nullable @Assisted("NetworkB") NetworkInterface localNetworkInterfaceB) {

And I get the following error (twice, once for each parameter)

1) null returned by binding at my.company.package.MyClassFactory.create()
 but parameter 4 of my.company.package.MyClass.<init>() is not @Nullable
  while locating java.net.NetworkInterface annotated with @com.google.inject.assistedinject.Assisted(value=NetworkA)
   for parameter 4 at my.company.package.MyClass.<init>(MyClass.java:24)
  while locating my.company.package.MyClass annotated with interface com.google.inject.assistedinject.Assisted

Any idea what is wrong? I found two other questions on this issue, one of them said it was a dependency issue which I don't think I have, the other said it was an Eclipse issue, which I do use, but I refreshed, cleaned, and rebuilt my maven projects from scratch, so I'm not sure what the problem is.

I am using javax.annotation.Nullable, which is supposed to be retained at Runtime. What else should I try?

War es hilfreich?

Lösung

@Nullable needs to be set on both the constructor as well as the factory declaration. We use FactoryModuleBuilder to declare our Factories. I am pasting the relevant bits of code that work for us

The constructor:

@Inject
public AddressActions(EC2Service ec2Service,
                      RequestFactory requestFactory,
                      @Assisted("spiceManager") SpiceManager spiceManager,
                      @Assisted("parent") Context parent,
                      @Assisted("publicIp") @Nullable String publicIp) {

The Abstract Factory:

public static interface Factory {
    AddressActions create(@Assisted("spiceManager") SpiceManager spiceManager,
                          @Assisted("parent") Context context,
                          @Assisted("publicIp") @Nullable String publicIp);
}

The Factory Module Builder:

install(new FactoryModuleBuilder().build(AddressActions.Factory.class));

The call:

AddressActions actions = actionsFactory.create(spiceManager, getSherlockActivity(), null);

Relevant versions:

  • Guice: guice-3.0-no_aop.jar
  • AssistedInject: guice-assistedinject-3.0.jar
  • JSR305: jsr305-1.3.9.jar

-k

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top