Java 9 迁移期间未找到不兼容的类型、等式约束和方法

Incompatible types, equality constraints and method not found during Java 9 Migration

在将我们的一个项目迁移到 Java 9(build 9+181) 时,我遇到了一个奇怪的问题在与 type inference and 相关的某些正在使用的库中,看起来像是不正确实现的问题。我正在使用 dropwizard-core(1.1.0)guice(4.1.0) 配置如下:

public class CustomService extends io.dropwizard.Application<CustomServiceConfig> {

    public static void main(String[] args) throws Exception {
        new CustomService().run(args);
    }

    // other initializations

    @Override
    public void run(CustomServiceConfig config, io.dropwizard.setup.Environment environment) throws Exception {
        com.google.inject.Injector injector = createInjector(config, environment);
        environment.jersey().register(injector.getInstance(SomeResource.class)); //line 45
        environment.healthChecks().register("DBHealth", injector.getInstance(HealthCheck.class)); 
        environment.servlets().addFilter("Filter-Name", SomeFilter.class)
                .addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
    }


    private com.google.inject.Injector createInjector(CustomServiceConfig config, Environment environment) {
        return com.google.inject.Guice.createInjector(new CustomServiceModule(config, environment));
    }

}

public class CustomServiceModule extends com.google.inject.AbstractModule {
    private final CustomServiceConfig serviceConfig;
    private final Environment environment;

    public CustomServiceModule(CustomServiceConfig serviceConfig, Environment environment) {
        this.serviceConfig = serviceConfig;
        this.environment = environment;
    }

    @Override
    protected void configure() {
        bind(SomeInterface.class).to(SomeInterfaceImpl.class);
        ..
    }
}

配置适用于我的以下组合:

但是当我切换到模块结构然后尝试编译由那些 classes 组成的 maven 模块时,我在 [=17= 期间遇到了这些错误] :

[ERROR] ../service/service/CustomService.java:[45,29] incompatible types: inference variable T has incompatible bounds
    equality constraints: base.SomeResource
    upper bounds: java.lang.Class<?>,java.lang.Object

[ERROR] ../service/service/CustomService.java:[56,31]
no suitable method found for
addFilter(java.lang.String,java.lang.Class< SomeFilter >)
[ERROR]     method io.dropwizard.jetty.setup.ServletEnvironment.addFilter(java.lang.String,javax.servlet.Filter)
is not applicable
[ERROR]       (argument mismatch; java.lang.Class< SomeFilter > cannot be
converted to javax.servlet.Filter)

我不确定为什么会出现这些错误,以及它们是否与正在使用的模块结构有关。

Q1。是否有任何与类型推断相关的更改会影响模块结构更改(如果可能)的 Maven 编译,同时记住所使用的依赖项?

此外,在迁移时,我主要使用 IntelliJ 建议的自动模块名称将 module-info 构造为:

module service {
//    Internal modules  which compile successfully
    requires model;
    requires util;

//    Dependent library modules
    requires httpcore;
    requires guice;
    requires guava;
    requires dropwizard.core;
    requires mongo.java.driver;
    requires org.apache.commons.lang3;
    requires javax.servlet.api;
}

Q2。如果这不是迁移时的回归问题,为什么我们之前的设置没有失败?这是否也要求在我们的服务中更改代码,或者如果这可能有帮助,我们是否应该等待库转移到模块?

注意:已尝试查看依赖版本的用法和 class 实现。它们在以前和当前的设置中都是相同的。

如有任何我能提供帮助的进一步信息,请告诉我。


更新: 我能够在我创建的 microservice sample 中重现相同的内容,以将其与我的项目的其余部分隔离开来。

从示例项目我能够修复编译问题。 com.SomeService#run 方法中有 2 个异常。您的模块中缺少模块-info.java,添加这些代码后应该可以编译。

requires dropwizard.jersey;
requires dropwizard.jetty;

JerseyEnvironment 来自 io.dropwizard:dropwizard-jersey:1.1.0

ServletEnvironment 来自 io.dropwizard:dropwizard-jetty:1.1.0

由于它们是不同的jar,它们导出不同的模块。因此,需要明确添加要求。 你的代码在没有 module-info.java 的情况下工作正常,因为那时没有使用模块系统。

我通过执行以下评论中提到的方法找到了修复方法:

@Override
public void run(SomeServiceConfig config, Environment environment) throws Exception {
    Injector injector = Guice.createInjector(new SomeServiceModule());
    // Fix: Extract to variable to find Type of jersey and then find the module to add under requires
    JerseyEnvironment jersey = environment.jersey(); 
    jersey.register(injector.getInstance(SomeResource.class));
    // Fix 2: Same method as Fix 1
    ServletEnvironment servlets = environment.servlets();
    servlets.addFilter("Some-Filter", SomeFilter.class);
}