在返回的类类型上取消选中转换

时间:2018-10-31 12:38:44

标签: java spring generics wicket-7 wicketstuff

我有一个返回类类型的方法,它可以正常工作。
问题是...我收到一些编译警告,指示未经检查的转换。

以下是我收到的警告消息:

Warning: java: getAjaxEventPayloadClass() in <anonymous com.iyotbihagay.wicket.panel.requestpage.resultlist.ResultList2$7> implements <P>getAjaxEventPayloadClass() in com.iyotbihagay.wicket.panel.paging.Paging.Support
  return type requires unchecked conversion from java.lang.Class<com.iyotbihagay.wicket.support.RefreshSearchResultEventPayload> to java.lang.Class<P>

即使我使用@SuppressWarnings("unchecked"),我仍然无法摆脱编译警告。

下面是一些指向警告的代码:

ResultList2.java

public class ResultList2 {
    ...
    private Component createPaging() {
        return new Paging("paging", getRequestPageContext(), new Paging.Support() {
            @Override
            public void switchPage(AjaxRequestTarget target, int targetPageNum) {
                getRequestPageContext().switchPage(target, targetPageNum);
            }
            @Override
            public int getTotalNum() {
                return getSearchResult().getTotalNum();
            }
            @Override
            public int getPageSize() {
                return getSearchResult().getPageSize();
            }
            @Override
            public int getLastPageNum() {
                return getSearchResult().getLastPageNum();
            }
            @Override
            public int getCurrentPageNum() {
                return getSearchResult().getCurrentPageNum();
            }

            @SuppressWarnings("unchecked")
            @Override
            public Class<RefreshSearchResultEventPayload> getAjaxEventPayloadClass() {
                return RefreshSearchResultEventPayload.class;
            }

            @Override
            public void decorateAjaxAttributes(AjaxRequestAttributes attributes, String pageNumMarkupId) {
                decorateAjaxRefreshSearchResult(attributes, pageNumMarkupId);
            }
        });
    }
    ...
}

Support.java

public static interface Support extends Serializable {
    ...
    <P extends AjaxEventPayload> Class<P> getAjaxEventPayloadClass();
    ...
}

Paging.java

public class Paging {
    ...
    private Support m_support;
    ...

    @Override
    public void onEvent(IEvent<?> event) {
        super.onEvent(event);
        WicketUtil.onEvent(event, m_support.getAjaxEventPayloadClass(), new AjaxEventHandler<AjaxEventPayload>() {
            @Override
            public void onEvent(IEvent<?> event, AjaxRequestTarget target, AjaxEventPayload paymentLoad) {
                m_firstPageNumModel.detach();
                m_previousPageNumModel.detach();
                m_nextPageNumModel.detach();
                m_lastPageNumModel.detach();
                target.add(m_container);
            }
        });
    }
    ...
}

WicketUtil.java

public class WicketUtil {
    ...
    public static <P extends AjaxEventPayload> void onEvent(IEvent<?> event, Class<P> targetPayload, AjaxEventHandler<P> handler) {
        if (event.getPayload() != null) {
            if (targetPayload.isAssignableFrom(event.getPayload().getClass())) {
                P p = (AjaxEventPayload)event.getPayload();
                handler.onEvent(event, p.getTarget(), p);
            }

        }
    }
    ...
}

m_support.getAjaxEventPayloadClass()WicketUtil.onEvent()上被调用/传递,特别是在第二个参数上。
RefreshSearchResultEventPayload只是扩展AjaxEventPayload的类之一,还有其他扩展到AjaxEventPayload并传递给WicketUtil.onEvent()的类。

1 个答案:

答案 0 :(得分:2)

这应该是:

public static interface Support extends Serializable {
    ...
    Class<? extends AjaxEventPayload> getAjaxEventPayloadClass();
    ...
}

public class ResultList2 {
    ...
    @Override
    public Class<RefreshSearchResultEventPayload> getAjaxEventPayloadClass() {
        return RefreshSearchResultEventPayload.class;
    }
    ...
}

原因是<P extends AjaxEventPayload>意味着该方法可以返回任意有效负载类型的类(根据调用者的请求),例如:

support.<AjaxEventPayload>getAjaxEventPayloadClass()

而不是仅返回特定于实现类的一种有效负载类型(例如RefreshSearchResultEventPayload)。