Uncheck conversion on returning class type

127 Views Asked by At

I have a method that returns a class type, and it is working right.
The problem is... I am getting some compilation warnings indicating unchecked conversion.

Below is the warning message I got:

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>

even if I use @SuppressWarnings("unchecked") I still cannot get rid of the compilation warning.

Below are some of the codes pointing to the warning:

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() is called/passed on WicketUtil.onEvent()... specifically on the second parameter.
RefreshSearchResultEventPayload is just one of the classes that extends AjaxEventPayload and there are other classes that extends to AjaxEventPayloadand are passed to WicketUtil.onEvent().

1

There are 1 best solutions below

0
Tomasz Linkowski On

This should rather be:

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

and

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

The reason is that <P extends AjaxEventPayload> means that the method can return a class of arbitrary payload type (as requested by the caller), e.g.:

support.<AjaxEventPayload>getAjaxEventPayloadClass()

instead of returning only the one payload type that is specific to the implementing class (e.g. RefreshSearchResultEventPayload).