简体   繁体   English

@Nullable似乎不适用于@AssistedInject

[英]@Nullable doesn't seem to work with @AssistedInject

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. 我在这个问题上发现了另外两个问题,其中一个说这是我不认为有的依赖关系问题 ,另一个说这是我确实使用过的Eclipse问题 ,但是我刷新,清理并重建了我的问题。 Maven从头开始项目,所以我不确定是什么问题。

I am using javax.annotation.Nullable , which is supposed to be retained at Runtime. 我正在使用javax.annotation.Nullable ,应该将其保留在运行时。 What else should I try? 我还应该尝试什么?

@Nullable needs to be set on both the constructor as well as the factory declaration. @Nullable需要在构造函数和工厂声明中都设置。 We use FactoryModuleBuilder to declare our Factories. 我们使用FactoryModuleBuilder声明我们的工厂。 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 吉斯: guice-3.0-no_aop.jar
  • AssistedInject: guice-assistedinject-3.0.jar AssistedInject: guice-assistedinject-3.0.jar
  • JSR305: jsr305-1.3.9.jar JSR305: jsr305-1.3.9.jar

-k -k

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM