unchecked cast 'java.lang.object' to 'java.util.list '

10,359

In your example, it does not look like you need this to be a List<User>, so you can do

List<?> users = (List<?>) xStream.fromXML(xml);

If you do need it to be typed, there is no way to avoid the warning. If you are confident that the type is correct, use @SuppressWarnings("unchecked"). If there is a doubt, write some code to assert that all elements are indeed of the correct type (I wish the JDK had a helper method for that).

Share:
10,359
Admin
Author by

Admin

Updated on June 06, 2022

Comments

  • Admin
    Admin almost 2 years
    @DataProvider
    public Iterator<Object[]> validLogin() throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(new File("src/test/java/resources/UserData/login.xml")));
        String xml = "";
        String line = reader.readLine();
        while (line != null) {
            xml += line;
            line = reader.readLine();
        }
        XStream xStream = new XStream();
        xStream.processAnnotations(User.class);
        List<User> users = (List<User>) xStream.fromXML(xml);
        return users.stream().map((g) -> new Object[]{g}).collect(Collectors.toList()).iterator();
    }
    

    I see this warning

    unchecked cast 'java.lang.object' to 'java.util.list '   
    

    at

    List<User> users = (List<User>) xStream.fromXML(xml) ;
    

    How can I avoid them?