author | vlivanov |
Wed, 02 Mar 2016 15:42:03 +0300 | |
changeset 36349 | 6cc8e6f596b2 |
parent 25796 | c6b97b1f9fee |
permissions | -rw-r--r-- |
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 |
25796
c6b97b1f9fee
8053938: Collections.checkedList(empty list).replaceAll((UnaryOperator)null) doesn't throw NPE after JDK-8047795
chegar
parents:
25171
diff
changeset
|
26 |
* @bug 8047795 8053938 |
25171
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.UnaryOperator; |
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 CheckedListReplaceAll { |
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 |
List unwrapped = Arrays.asList(new Object[]{1, 2, 3}); |
5dd99a9e443b
8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff
changeset
|
37 |
List<Object> wrapped = Collections.checkedList(unwrapped, Integer.class); |
5dd99a9e443b
8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff
changeset
|
38 |
|
5dd99a9e443b
8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff
changeset
|
39 |
UnaryOperator evil = e -> (((int) e) % 2 != 0) ? e : "evil"; |
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 |
try { |
5dd99a9e443b
8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff
changeset
|
42 |
wrapped.replaceAll(evil); |
5dd99a9e443b
8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff
changeset
|
43 |
System.out.printf("Bwahaha! I have defeated you! %s\n", wrapped); |
5dd99a9e443b
8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff
changeset
|
44 |
throw new RuntimeException("String added to checked List<Integer>"); |
5dd99a9e443b
8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff
changeset
|
45 |
} catch (ClassCastException thwarted) { |
5dd99a9e443b
8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff
changeset
|
46 |
thwarted.printStackTrace(System.out); |
5dd99a9e443b
8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff
changeset
|
47 |
System.out.println("Curses! Foiled again!"); |
5dd99a9e443b
8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff
changeset
|
48 |
} |
25796
c6b97b1f9fee
8053938: Collections.checkedList(empty list).replaceAll((UnaryOperator)null) doesn't throw NPE after JDK-8047795
chegar
parents:
25171
diff
changeset
|
49 |
|
c6b97b1f9fee
8053938: Collections.checkedList(empty list).replaceAll((UnaryOperator)null) doesn't throw NPE after JDK-8047795
chegar
parents:
25171
diff
changeset
|
50 |
unwrapped = Arrays.asList(new Object[]{}); // Empty list |
c6b97b1f9fee
8053938: Collections.checkedList(empty list).replaceAll((UnaryOperator)null) doesn't throw NPE after JDK-8047795
chegar
parents:
25171
diff
changeset
|
51 |
wrapped = Collections.checkedList(unwrapped, Integer.class); |
c6b97b1f9fee
8053938: Collections.checkedList(empty list).replaceAll((UnaryOperator)null) doesn't throw NPE after JDK-8047795
chegar
parents:
25171
diff
changeset
|
52 |
try { |
c6b97b1f9fee
8053938: Collections.checkedList(empty list).replaceAll((UnaryOperator)null) doesn't throw NPE after JDK-8047795
chegar
parents:
25171
diff
changeset
|
53 |
wrapped.replaceAll((UnaryOperator)null); |
c6b97b1f9fee
8053938: Collections.checkedList(empty list).replaceAll((UnaryOperator)null) doesn't throw NPE after JDK-8047795
chegar
parents:
25171
diff
changeset
|
54 |
System.out.printf("Bwahaha! I have defeated you! %s\n", wrapped); |
c6b97b1f9fee
8053938: Collections.checkedList(empty list).replaceAll((UnaryOperator)null) doesn't throw NPE after JDK-8047795
chegar
parents:
25171
diff
changeset
|
55 |
throw new RuntimeException("NPE not thrown when passed a null operator"); |
c6b97b1f9fee
8053938: Collections.checkedList(empty list).replaceAll((UnaryOperator)null) doesn't throw NPE after JDK-8047795
chegar
parents:
25171
diff
changeset
|
56 |
} catch (NullPointerException thwarted) { |
c6b97b1f9fee
8053938: Collections.checkedList(empty list).replaceAll((UnaryOperator)null) doesn't throw NPE after JDK-8047795
chegar
parents:
25171
diff
changeset
|
57 |
thwarted.printStackTrace(System.out); |
c6b97b1f9fee
8053938: Collections.checkedList(empty list).replaceAll((UnaryOperator)null) doesn't throw NPE after JDK-8047795
chegar
parents:
25171
diff
changeset
|
58 |
System.out.println("Curses! Foiled again!"); |
c6b97b1f9fee
8053938: Collections.checkedList(empty list).replaceAll((UnaryOperator)null) doesn't throw NPE after JDK-8047795
chegar
parents:
25171
diff
changeset
|
59 |
} |
25171
5dd99a9e443b
8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff
changeset
|
60 |
} |
5dd99a9e443b
8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents:
diff
changeset
|
61 |
} |