0

我收到错误:

java.lang.ClassCastException: Group cannot be cast to Person

由于

Type safety: Unchecked cast from List<capture#1-of ?> to List<Person>

从代码:

List<Person> arrangedPerson = PersonAddImportHelper.setChannelInfo(
                (List<Person>) (List<?>) group.getRecipients(), model);
at ui.PersonAddImportHelper.setChannelInfo(PersonAddImportHelper.java:42)
at ui.GroupMembersController.viewGroupMembers(GroupMembersController.java:170)  

GroupMembersController.viewGroupMembers (GroupMembersController.java:170)

 @RequestMapping(value = "/list/{id}/{page}", method = RequestMethod.GET)
    public String viewGroupMembers(@PathVariable("id") final Group group,
            @PathVariable("page") final int page,
            @RequestParam(value = "pageSize", defaultValue = DEFAULT_PAGE_SIZE) final int pageSize,
            @RequestParam(value = "search", defaultValue = "") final String search,
            @RequestParam(value = "sortColumn", defaultValue = "lastname") final String sortColumn,
            @RequestParam(value = "filter", defaultValue = "") final String filter,
            final Model model) {
        List<Person> arrangedPerson = PersonAddImportHelper.setChannelInfo(
                (List<Person>) (List<?>) group.getRecipients(), model);
        for (Person person : arrangedPerson) {
            group.addMember(person);
        }
        list(group, page, Integer.valueOf(pageSize), search, sortColumn, filter, model);
        model.addAttribute("group", group);
        return "/recipient/groupPersonList";
    }   

并且ui.PersonAddImportHelper.setChannelInfo是:

public static List<Person> setChannelInfo(final List<Person> items,
            final Model model) {
        List<Person> arrangedPerson = new ArrayList<>();
        final Map<Person, Boolean> hasEmail = Maps
                .newHashMapWithExpectedSize(items.size());
        final Map<Person, Boolean> hasFixed = Maps
                .newHashMapWithExpectedSize(items.size());
        final Map<Person, Boolean> hasMobile = Maps
                .newHashMapWithExpectedSize(items.size());
        for (Person person : items) {
            List<Address> addresses = ((Person) person).getAddresses();
            List<Address> arrangedAddresses = new ArrayList<Address>(
                    Arrays.asList(new Address[ADDRESS_MAX_SIZE]));
            int mobileIndex = 0;
            int fixedIndex = 2;
            int emailIndex = ADDRESS_PHONE_START_INDX;
            for (Address address : addresses) {
                // some Logic
            }
            ((Person) person).setAddresses(arrangedAddresses);
            arrangedPerson.add(person);
        }
        model.addAttribute("hasEmail", hasEmail);
        model.addAttribute("hasFixed", hasFixed);
        model.addAttribute("hasMobile", hasMobile);
        return arrangedPerson;
    }
4

3 回答 3

1

这是一段非常奇怪的代码:

(List<Person>) (List<?>) group.getRecipients()

返回什么getRecipients()?你为什么要投两次?似乎 group.getRecipients() 返回的不是 List 吗?

于 2013-10-15T12:18:39.777 回答
1

我从代码中看到的

 (List<Person>) (List<?>) group.getRecipients()

您正在尝试将 Group 转换为您的 Person 类,该错误甚至显示了..

于 2013-10-15T12:18:51.607 回答
1

您必须提供继承层次结构。从您收到的错误中,我推断group.getRecipients()返回一个列表,其中包含不是Person. 我认为这也是双重转换的原因(List<Person>) (List<?>) group.getRecipients(),因为编译器不会接受直接转换(List<Person>) group.getRecipients()

您使编译器接受您的代码的解决方法只是将错误从编译时推送到运行时,并且在for (Person person : items)达到时将被触发。

更改 to 的返回类型group.getRecipients()List<Person>Group继承自Person

于 2013-10-15T12:25:07.410 回答