类型安全:未经检查的强制转换导致java.lang.ClassCastException

时间:2013-10-15 12:11:47

标签: java

我收到了错误:

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;
    }

3 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

我从代码中看到的内容

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

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

答案 2 :(得分:1)

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

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

group.getRecipients()的返回类型更改为List<Person>或让GroupPerson继承。