Java11函数流在方法调用时推断出错误的类型

Java11函数流在方法调用时推断出错误的类型,java,generics,types,functional-programming,java-11,Java,Generics,Types,Functional Programming,Java 11,我有这样的代码: public interface Checker<A,B> extends BiFunction<CheckRequest<A>,Function<A,B>,CheckResponse<B>> { // ... } public class CheckResponse<B> { private B operationResponse; //... public void setOperat

我有这样的代码:

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;
  }
我得到

不兼容的类型:java.lang.Object无法转换为[actual] B类]

如果方法调用是内联的,为什么不能正确推断getOperationResponse()的返回类型

我正在使用Oracle的OpenJDK11:

IMPLEMENTOR=“Oracle Corporation”IMPLEMENTOR_VERSION=“18.9” JAVA_VERSION=“11”JAVA_VERSION_DATE=“2018-09-25”


Windows 10上的Intellij IDEA 2018.3和Maven 3.5.4。

您需要确保您的
检查器的定义类似于:

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<>();
    }
};
Checker Checker=new Checker(){
@凌驾
公共检查响应应用(检查请求aCheckRequest,函数abFunction){
//执行任何操作并返回类型B的CheckResponse
返回新的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;
    }
}
类检查响应{
私人B运营响应;
公共无效设置操作响应(B操作响应){
this.operationResponse=operationResponse;
}
公共B getOperationResponse(){
返回操作响应;
}
}
请求模型

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 void setOperationRequest(操作请求){
this.operationRequest=operationRequest;
}
公开一个getOperationRequest(){
返回操作请求;
}
}
然后你对方法的完整定义可以是

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 B执行(A请求){
CheckRequest chkReq=新的CheckRequest();
chkReq.设置操作请求(req);
函数op;//初始化
棋盘格=新棋盘格(){
@凌驾
公共检查响应应用(检查请求aCheckRequest,函数abFunction){
//执行任何操作并返回类型B的CheckResponse
返回新的CheckResponse();
}
};
返回检查器.apply(chkReq,op).getOperationResponse();
}

我可以确认以上在语法上对我来说很好。

你能提供
CheckResponse
CheckRequest
类的(相关)代码吗?另外,您可以在这里添加代码:
CheckRequest chkReq=/…
和这里的
Function op=/…
。听起来好像您在某处使用了原始类型,导致
checker.apply(chkReq,op)
成为
CheckResponse
(有点类似,但与
CheckResponse
不完全相同)而不是
CheckResponse
,导致给定错误的原因。@KevinCruijssen您可能是对的,我错过了一段代码,其中我使用了一个工厂类,如:
public Checker getChecker(OperationType operation){switch(operation){case getOperation:return new SecurityChecker(serviceRepository);default:throw new UnsupportedOperationException(“unsupported Operation:”+Operation);}}
哪种返回类型太“原始”,如果我理解正确的话……这是“必须”吗“要让匿名内联
检查器
实现?为什么我不能仅仅使用一个实现
Cheker
接口的具体类的实例?@AleZ我不小心删除了我以前的评论,但不,它不必是内联的。您可以创建一个松散的类来实现
检查器
接口。只需确保该类与nullpointer的代码中覆盖的
apply
-方法的
类CheckerImpl实现Checker
类似。重要的部分是菱形操作符
。没有它,
newcheckerresponse()将是“原始”的,正如我在您问题下方的评论中所述。然后可以使用
Checker=newcheckerimpl()
@AleZ No正如Kevin指出的,这只是为了演示与接口绑定的类型,以及此后如何成功调用方法
apply
。@KevinCruijssen感谢您的解释,这与我打算使用示例解释的内容非常相似。