Java11 功能流在方法调用时推断出错误的类型
Java 11 Functional flow infers wrong type on method call
我有这样的代码:
public interface Checker<A,B> extends BiFunction<CheckRequest<A>,Function<A,B>,CheckResponse<B>> { // ... }
public class CheckResponse<B> {
private B operationResponse;
//...
public void setOperationResponse(B operationResponse) {
this.operationResponse = operationResponse;
}
public B getOperationResponse() {
return operationResponse;
}
}
和类似的方法:
public B execute(A req){
CheckRequest<A> chkReq = //...
chkReq.setOriginalRequest(req);
Function<A,B> op = //...
CheckResponse<B> chkRes= checker.apply(chkReq ,op)
// [...]
return chkRes.getOperationResponse();
}
我想将 "op" 的执行包装到一个将执行一些其他副作用的检查器对象中。我还需要将 "op" 的输入和输出包装到适当的 CheckRequest 和 CheckResponse 中以传递和取回额外的数据。但是,为了取回 "op" 的原始结果,我需要 CheckResponse 中的 getOperationResponse() 方法。听起来很简单。
上面的代码按预期工作但是,如果我 "inline" 它喜欢:
return checker.apply(chkReq ,op).getOperationResponse();
我得到了
incompatible types: java.lang.Object cannot be converted to [actual
type of B]
如果方法调用是内联的,为什么无法正确推断出 return 类型的 getOperationResponse()?
我正在使用来自 Oracle 的 OpenJDK11:
IMPLEMENTOR="Oracle Corporation" IMPLEMENTOR_VERSION="18.9"
JAVA_VERSION="11" JAVA_VERSION_DATE="2018-09-25"
Intellij IDEA 2018.3 和 Maven 3.5.4 Windows 10.
您需要确保您的 checker
的定义类似于:
Checker<A, B> checker = new Checker<A, B>() {
@Override
public CheckResponse<B> apply(CheckRequest<A> aCheckRequest, Function<A, B> abFunction) {
// perform whatever operation and return a CheckResponse of type B
return new CheckResponse<>();
}
};
基本假设完整类以下是:
响应模型:
class CheckResponse<B> {
private B operationResponse;
public void setOperationResponse(B operationResponse) {
this.operationResponse = operationResponse;
}
public B getOperationResponse() {
return operationResponse;
}
}
请求型号:
class CheckRequest<A> {
private A operationRequest;
public void setOperationRequest(A operationRequest) {
this.operationRequest = operationRequest;
}
public A getOperationRequest() {
return operationRequest;
}
}
然后您对该方法的完整定义可以是
public B execute(A req) {
CheckRequest<A> chkReq = new CheckRequest<>();
chkReq.setOperationRequest(req);
Function<A, B> op;// intialised
Checker<A, B> checker = new Checker<A, B>() {
@Override
public CheckResponse<B> apply(CheckRequest<A> aCheckRequest, Function<A, B> abFunction) {
// perform whatever operation and return a CheckResponse of type B
return new CheckResponse<>();
}
};
return checker.apply(chkReq, op).getOperationResponse();
}
我可以确认以上在语法上对我来说没问题。
我有这样的代码:
public interface Checker<A,B> extends BiFunction<CheckRequest<A>,Function<A,B>,CheckResponse<B>> { // ... }
public class CheckResponse<B> {
private B operationResponse;
//...
public void setOperationResponse(B operationResponse) {
this.operationResponse = operationResponse;
}
public B getOperationResponse() {
return operationResponse;
}
}
和类似的方法:
public B execute(A req){
CheckRequest<A> chkReq = //...
chkReq.setOriginalRequest(req);
Function<A,B> op = //...
CheckResponse<B> chkRes= checker.apply(chkReq ,op)
// [...]
return chkRes.getOperationResponse();
}
我想将 "op" 的执行包装到一个将执行一些其他副作用的检查器对象中。我还需要将 "op" 的输入和输出包装到适当的 CheckRequest 和 CheckResponse 中以传递和取回额外的数据。但是,为了取回 "op" 的原始结果,我需要 CheckResponse 中的 getOperationResponse() 方法。听起来很简单。
上面的代码按预期工作但是,如果我 "inline" 它喜欢:
return checker.apply(chkReq ,op).getOperationResponse();
我得到了
incompatible types: java.lang.Object cannot be converted to [actual type of B]
如果方法调用是内联的,为什么无法正确推断出 return 类型的 getOperationResponse()?
我正在使用来自 Oracle 的 OpenJDK11:
IMPLEMENTOR="Oracle Corporation" IMPLEMENTOR_VERSION="18.9" JAVA_VERSION="11" JAVA_VERSION_DATE="2018-09-25"
Intellij IDEA 2018.3 和 Maven 3.5.4 Windows 10.
您需要确保您的 checker
的定义类似于:
Checker<A, B> checker = new Checker<A, B>() {
@Override
public CheckResponse<B> apply(CheckRequest<A> aCheckRequest, Function<A, B> abFunction) {
// perform whatever operation and return a CheckResponse of type B
return new CheckResponse<>();
}
};
基本假设完整类以下是:
响应模型:
class CheckResponse<B> {
private B operationResponse;
public void setOperationResponse(B operationResponse) {
this.operationResponse = operationResponse;
}
public B getOperationResponse() {
return operationResponse;
}
}
请求型号:
class CheckRequest<A> {
private A operationRequest;
public void setOperationRequest(A operationRequest) {
this.operationRequest = operationRequest;
}
public A getOperationRequest() {
return operationRequest;
}
}
然后您对该方法的完整定义可以是
public B execute(A req) {
CheckRequest<A> chkReq = new CheckRequest<>();
chkReq.setOperationRequest(req);
Function<A, B> op;// intialised
Checker<A, B> checker = new Checker<A, B>() {
@Override
public CheckResponse<B> apply(CheckRequest<A> aCheckRequest, Function<A, B> abFunction) {
// perform whatever operation and return a CheckResponse of type B
return new CheckResponse<>();
}
};
return checker.apply(chkReq, op).getOperationResponse();
}
我可以确认以上在语法上对我来说没问题。