Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8047795

Collections.checkedList checking bypassed by List.replaceAll

XMLWordPrintable

    • b22
    • generic
    • generic
    • Verified

        FULL PRODUCT VERSION :


        A DESCRIPTION OF THE PROBLEM :
        The replaceAll method of a list returned by java.util.Collections.checkedList passes the replacement operator directly to the underlying list, enabling violation of the type checking.


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.util.*;
        import java.util.function.UnaryOperator;

        class CheckedListViolationBug {
            public static void main(String... args) {
                // Create a runtime-checked List of Integers only
                List<Integer> integersOnly = Collections.checkedList(new ArrayList<>(), Integer.class);
                integersOnly.add(1);
                integersOnly.add(2);
                integersOnly.add(3);
                
                // the UnaryOperator bypasses the check, enabling insertion of arbitrary elements
                integersOnly.replaceAll((UnaryOperator)(e -> "ha!"));
                
                // the list now contains Strings:
                System.out.println(integersOnly);
            }
        }
        ---------- END SOURCE ----------

              mduigou Mike Duigou
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Created:
                Updated:
                Resolved: