java 对 的引用不明确,从java8迁移到java9时,对方法的引用是不明确的

I'm migrating a project from JAVA 8 to JAVA 9 and I'm having some trouble getting the code to work. All work in JAVA 8 but in 9 I'm having the following errors:

Error java: reference to ok is ambiguous

both method ok(java.util.function.Supplier) and method ok(web.Procedure) match

here is the code when I'm calling the method:

public ResponseEntity> mailTemplateFindAll() {

return ok(() -> mailTemplateService.findAll());

}

and here is the implementation :

public ResponseEntity ok(Supplier action) {

return this.body(HttpStatus.OK, action);

}

public ResponseEntity ok(T body) {

return this.ok(() -> {

return body;

});

}

public ResponseEntity ok(Procedure action) {

action.invoke();

return this.status(HttpStatus.OK);

}

public ResponseEntity ok() {

return this.status(HttpStatus.OK);

}

code for Procedure interface:

@FunctionalInterface

public interface Procedure {

void invoke();

}

Any ideas?

Reproducible Code ::

public class Q48227496 {

public A> test() {

return ok(() -> System.out.append("aaa"));

}

private class A {

}

private A ok(java.util.function.Supplier action) {

return new A<>();

}

public A ok(T body) {

return new A<>();

}

private A ok(Procedure action) {

return new A<>();

}

public A ok() {

return new A<>();

}

@FunctionalInterface

public interface Procedure {

void invoke();

}

}

Resulting in the following error with java9 compiler::

error: reference to ok is ambiguous

return ok(() -> System.out.append("aaa"));

^

both method ok(Supplier) in Q48227496 and method ok(Procedure) in Q48227496 match

where T#1,T#2 are type-variables:

T#1 extends Object declared in method ok(Supplier)

T#2 extends Object declared in method ok(Procedure)

解决方案

This is a bug.

It has been reported with bug ID : JDK-8195598

I simplified your example further:

public class Q48227496 {

public CompletableFuture> test() {

return ok(() -> System.out.append("aaa"));

}

public CompletableFuture ok(Supplier action) {

return CompletableFuture.supplyAsync(action);

}

public CompletableFuture ok(T body) {

return CompletableFuture.completedFuture(body);

}

public CompletableFuture ok(Runnable action) {

return CompletableFuture.runAsync(action);

}

}

This fails in the release version of Java 9 with “reference to ok is ambiguous”, stating “both method ok(Supplier) in Q48227496 and method ok(Runnable) in Q48227496 match”.

But just changing the order of the methods

public class Q48227496 {

public CompletableFuture> test() {

return ok(() -> System.out.append("aaa"));

}

public CompletableFuture ok(T body) {

return CompletableFuture.completedFuture(body);

}

public CompletableFuture ok(Supplier action) {

return CompletableFuture.supplyAsync(action);

}

public CompletableFuture ok(Runnable action) {

return CompletableFuture.runAsync(action);

}

}

causes the compiler to accept the code without any errors.

So, obviously, this is a compiler bug as the order of the method declarations should never have an impact on the validity of the code.

Also, removing the ok(T) method makes the code accepted.

Note that whenever the compiler accepts the code, it considers ok(Supplier) to be more specific than ok(Runnable), which is the expected behavior for a function parameter that matches both.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值