I am currently migrating a project to JDK 11 and compiling it with Maven. However, Maven throws a wobbly over a single method reference (it doesn't have issues elsewhere). The method in question looks like this:
public class MyThing {
boolean something = true;
// ...
public boolean isSomething() {
return something;
}
// ...
}
And the call to the above method looks more or less like this:
return methodThatGetsOptionalDefinition(aString) // Optional<Definition>
.map(definition -> defenition.getMyThing(anotherString)) // Optional<MyThing>
.map(MyThing::isSomething) // Optional<Boolean>
.orElse(true);
Compiling this, Maven throws the following message:
> 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.
If I expand the method reference like so, it compiles without a problem:
return methodThatGetsOptionalDefinition(aString)
.map(definition -> defenition.getMyThing(anotherString))
.map(myThing -> myThing.isSomething())
.orElse(true);
Typing the Optional also works:
return methodThatGetsOptionalDefinition(aString)
.<MyThing>map(definition -> defenition.getMyThing(anotherString))
.map(myThing -> myThing.isSomething())
.orElse(true);
The same error occurs when compiling the project using IntelliJ with both Open JDK versions 11.0.1 and 11.0.2. Ironically, IntelliJ complains that the "Lambda can be replaced with method reference". The problem also occurs when using a different "language mode".
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
Does anyone have any idea why this might be happening?
Some of the specific implementation details have been obfuscated.