可以避免重写相同回调多次?

时间:2013-12-10 12:10:27

标签: java gwt callback duplicates

我需要调用一个服务,总是传递相同的回调“AsyncCallbackAdapter”

    auditingServicesRpc.tracePrivacy(requestDesc, ip, new AsyncCallbackAdapter<Integer>() {
            @Override
            public void onSuccess(Integer arg0) {}

            @Override
            public void onFailure(Throwable caught) {
            super.onFailure(caught);

        }
    });

这是从GWT实现AsyncCallback的AsyncCallbackAdapter

    import com.google.gwt.user.client.rpc.AsyncCallback;
    public abstract class AsyncCallbackAdapter<T> implements AsyncCallback<T> {
        private Boolean caughtException = false;

        @Override
        public void onFailure(Throwable caught) {
            //log
            GWT.log(caught.getMessage());

            //invalid session handling
            if (caught instanceof InvocationException) {
        ...
            //other common handling
        }

        private void sessionExpired() {
            ...
        }

        public Boolean getCaughtException() {
            return caughtException;
        }

        public AsyncCallbackAdapter() {

        }
    }   

我想在abstrast超类AsyncCallbackAdapter中创建这样的方法

public static AsyncCallbackAdapter<T> getInstance(){

    return new AsyncCallbackAdapter<T>(){
        @Override
        public void onSuccess(T result) {
        };

        @Override
        public void onFailure(Throwable caught) {
            super.onFailure(caught);

        }
    };
}

但我不能因为我“无法对非静态类型T进行静态引用”。

你有什么想法!?!? 谢谢


@ user902383 谢谢,这个方法删除所有错误,但我怎么能用这个方法?我需要使用Integer for T. 使用这个:

    AsyncCallbackAdapter.getInstance()
Eclipse告诉我: “ - IAuditingServiceRpcAsync类型中的方法tracePrivacy(String,String,AsyncCallback)不适用于参数(String,String,AsyncCallbackAdapter)”

1 个答案:

答案 0 :(得分:1)

尝试public static <T> AsyncCallbackAdapter<T> getInstance(){ ...}这应该可以解决您的问题