jdk/test/java/util/Collections/CheckedMapReplaceAll.java
author amlu
Fri, 18 Nov 2016 14:39:03 +0800
changeset 42166 5582c83b1156
parent 25171 5dd99a9e443b
permissions -rw-r--r--
8169836: ProblemList update for java/lang/management/MemoryMXBean/PendingAllGC.sh Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25171
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
     1
/*
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
     4
 *
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
     8
 *
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    13
 * accompanied this code).
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    14
 *
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    18
 *
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    21
 * questions.
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    22
 */
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    23
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    24
/*
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    25
 * @test
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    26
 * @bug     8047795
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    27
 * @summary Ensure that replaceAll operator cannot add bad elements
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    28
 * @author  Mike Duigou
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    29
 */
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    30
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    31
import java.util.*;
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    32
import java.util.function.BiFunction;
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    33
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    34
public class CheckedMapReplaceAll {
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    35
    public static void main(String[] args) {
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    36
        Map<Integer,Double> unwrapped = new HashMap<>();
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    37
        unwrapped.put(1, 1.0);
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    38
        unwrapped.put(2, 2.0);
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    39
        unwrapped.put(3, 3.0);
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    40
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    41
        Map<Integer,Double> wrapped = Collections.checkedMap(unwrapped, Integer.class, Double.class);
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    42
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    43
        BiFunction evil = (k, v) -> (((int)k) % 2 != 0) ? v : "evil";
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    44
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    45
        try {
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    46
            wrapped.replaceAll(evil);
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    47
            System.out.printf("Bwahaha! I have defeated you! %s\n", wrapped);
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    48
            throw new RuntimeException("String added to checked Map<Integer,Double>");
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    49
        } catch (ClassCastException thwarted) {
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    50
            thwarted.printStackTrace(System.out);
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    51
            System.out.println("Curses! Foiled again!");
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    52
        }
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    53
    }
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff changeset
    54
}