无法在JDK 11中

时间:2019-03-22 12:28:14

标签: java maven lambda compiler-errors java-11

我目前正在将项目迁移到JDK 11并使用Maven进行编译。但是,Maven在单个方法引用上摇摆不定(在其他地方没有问题)。有问题的方法如下:

public class MyThing {

    boolean something = true;

    // ...

    public boolean isSomething() {
        return something;
    }

    // ...
}

对上述方法的调用大致像这样:

return methodThatGetsOptionalDefinition(aString)                 // Optional<Definition>
        .map(definition -> defenition.getMyThing(anotherString)) // Optional<MyThing>
        .map(MyThing::isSomething)                               // Optional<Boolean>
        .orElse(true);

对此进行编译,Maven会引发以下消息:

> mvn clean install -Pdist-snapshot -DskipTests
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project XXXXX: Compilation failure
[ERROR] /D:/XXXXX.java:[63,38] incompatible types: invalid method reference
[ERROR]     method isSomething in class XXXXX.MyThing cannot be applied to given types
[ERROR]       required: no arguments
[ERROR]       found: java.lang.Object
[ERROR]       reason: actual and formal argument lists differ in length
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

如果我像这样展开方法引用,它将毫无问题地编译:

return methodThatGetsOptionalDefinition(aString)
        .map(definition -> defenition.getMyThing(anotherString))
        .map(myThing -> myThing.isSomething())
        .orElse(true);

键入Optional也可以:

return methodThatGetsOptionalDefinition(aString)
        .<MyThing>map(definition -> defenition.getMyThing(anotherString))
        .map(myThing -> myThing.isSomething())
        .orElse(true);

使用IntelliJ和Open JDK版本11.0.1和11.0.2编译项目时,会发生相同的错误。具有讽刺意味的是,IntelliJ抱怨“可以用方法引用代替Lambda”。使用其他“语言模式”时也会发生此问题。

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>11</source>
        <target>11</target>
    </configuration>
</plugin>

有人知道为什么会这样吗?

一些具体的实现细节已被混淆。

0 个答案:

没有答案