带有通配符而不编译的Java泛型

时间:2017-01-18 08:46:27

标签: java generics compiler-errors callback wildcard

我制作了这个真实代码的简化示例,它没有编译:

public class Example {

    public static void main(String[] args) {
        Callback<BalanceResponse> balanceRequestCaller = new Callback<BalanceResponse>() {
            @Override
            public void onResponse(BalanceResponse response) {}

            @Override
            public void onFailure(String error, int code) {}

            @Override
            public void onFailure(Throwable t) {}
        };

        BalanceRequest breq = new BalanceRequest();

        Interceptor interceptor = new Interceptor(balanceRequestCaller);

        breq.startRequest((Callback<BaseResponse>) interceptor); //compile-time error!!!
    }


    public static class Interceptor implements Callback<BaseResponse> {

        public Interceptor(Callback<? extends BaseResponse> originalCaller) {
            this.originalCaller = originalCaller;
        }

        private Callback<? extends BaseResponse> originalCaller;

        @Override
        public void onResponse(BaseResponse response) {
            //do some interception stuff

            if (response instanceof BalanceResponse) {
                ((Callback<BalanceResponse>) originalCaller).onResponse((BalanceResponse) response);
            } else if (response instanceof SubscriptionResponse) {
                ((Callback<SubscriptionResponse>) originalCaller).onResponse((SubscriptionResponse) response);
            }
        }

        @Override
        public void onFailure(String error, int code) {}

        @Override
        public void onFailure(Throwable t) {}
    }

    public interface Request<T extends BaseResponse> {
        void startRequest(Callback<T> callback);
    }

    public static class BalanceRequest implements Request<BalanceResponse> {
        @Override
        public void startRequest(Callback<BalanceResponse> callback) {}
    }

    public static class SubscriptionRequest implements Request<SubscriptionResponse> {
        @Override
        public void startRequest(Callback<SubscriptionResponse> callback) {}
    }

    public static class BaseResponse {
        public String status;
    }

    public static class BalanceResponse extends BaseResponse {

    }

    public static class SubscriptionResponse extends BaseResponse {

    }

    public interface Callback<T> {
        void onResponse(T response);
        void onFailure(String error, int code);
        void onFailure(Throwable t);
    }
}

我要做的是创建一个拦截回调的Interceptor,对回复执行一些操作,然后将其传递给原始回调。

我想制作一个可以拦截任何Interceptor回调的通用Callback<? extends BaseResponse>

顺便说一句,我从NetBeans获得的编译时错误是

incompatible types: Callback<BaseResponse> cannot be converted to Callback<BalanceResponse>

给我建议我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:3)

您应该在类Interceptor上定义类型参数以防止编译错误并避免方法onResponse中的显式强制转换,这通常是设计问题的证明

带有类型参数的Interceptor类:

public static class Interceptor<T extends BaseResponse> implements Callback<T> {

    public Interceptor(Callback<T> originalCaller) {
        this.originalCaller = originalCaller;
    }

    private Callback<T> originalCaller;

    @Override
    public void onResponse(T response) {
        originalCaller.onResponse(response);
    }

    ...
}

然后,您将实例化它并将其用作下一个:

Interceptor<BalanceResponse> interceptor = new Interceptor<>(balanceRequestCaller);
breq.startRequest(interceptor);

答案 1 :(得分:1)

startRequest()签名的签名更改为

void startRequest(Callback<? super T> callback);

这允许它接受更通用的回调,而不是只需要最窄的类型。有关详细信息,请参阅What is PECS (Producer Extends Consumer Super)?

编辑:@teppic's answer是更好的解决方案,除非您必须为不同的请求类型重用单个Interceptor实例。如果是这种情况,您仍然可以将样板减少为单个委派:

@Override
public void onResponse(BaseResponse response) {
    //do some interception stuff
    ((Callback<BaseResponse>)originalCaller).onResponse(response);
}