author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 44042 | jdk/test/jdk/internal/misc/JavaLangAccess/NewUnsafeString.java@2040b44fe636 |
permissions | -rw-r--r-- |
12859 | 1 |
/* |
44042
2040b44fe636
8176187: jdk/internal/misc/JavaLangAccess/NewUnsafeString.java is not run
amlu
parents:
32834
diff
changeset
|
2 |
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. |
12859 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
17426
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
24 |
import java.util.Objects; |
18571 | 25 |
import java.util.Comparator; |
32834
e1dca5fe4de3
8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents:
30820
diff
changeset
|
26 |
import jdk.internal.misc.JavaLangAccess; |
e1dca5fe4de3
8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents:
30820
diff
changeset
|
27 |
import jdk.internal.misc.SharedSecrets; |
17426
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
28 |
|
12859 | 29 |
/* |
17426
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
30 |
* @test |
30820 | 31 |
* @bug 8013528 |
17426
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
32 |
* @summary Test JavaLangAccess.newUnsafeString |
32834
e1dca5fe4de3
8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents:
30820
diff
changeset
|
33 |
* @modules java.base/jdk.internal.misc |
17426
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
34 |
* @compile -XDignore.symbol.file NewUnsafeString.java |
44042
2040b44fe636
8176187: jdk/internal/misc/JavaLangAccess/NewUnsafeString.java is not run
amlu
parents:
32834
diff
changeset
|
35 |
* @run main NewUnsafeString |
12859 | 36 |
*/ |
17426
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
37 |
public class NewUnsafeString { |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
38 |
|
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
39 |
static final JavaLangAccess jla = SharedSecrets.getJavaLangAccess(); |
12859 | 40 |
|
17426
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
41 |
public static void testNewUnsafeString() { |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
42 |
String benchmark = "exemplar"; |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
43 |
String constructorCopy = new String(benchmark); |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
44 |
char[] jlaChars = benchmark.toCharArray(); |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
45 |
String jlaCopy = jla.newStringUnsafe(jlaChars); |
12859 | 46 |
|
17426
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
47 |
if (benchmark == constructorCopy) { |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
48 |
throw new Error("should be different instances"); |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
49 |
} |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
50 |
if (!benchmark.equals(constructorCopy)) { |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
51 |
throw new Error("Copy not equal"); |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
52 |
} |
18571 | 53 |
if (0 != Objects.compare(benchmark, constructorCopy, Comparator.naturalOrder())) { |
17426
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
54 |
throw new Error("Copy not equal"); |
12859 | 55 |
} |
56 |
||
17426
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
57 |
if (benchmark == jlaCopy) { |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
58 |
throw new Error("should be different instances"); |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
59 |
} |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
60 |
if (!benchmark.equals(jlaCopy)) { |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
61 |
throw new Error("Copy not equal"); |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
62 |
} |
18571 | 63 |
if (0 != Objects.compare(benchmark, jlaCopy, Comparator.naturalOrder())) { |
17426
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
64 |
throw new Error("Copy not equal"); |
12859 | 65 |
} |
66 |
||
17426
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
67 |
if (constructorCopy == jlaCopy) { |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
68 |
throw new Error("should be different instances"); |
12859 | 69 |
} |
17426
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
70 |
if (!constructorCopy.equals(jlaCopy)) { |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
71 |
throw new Error("Copy not equal"); |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
72 |
} |
18571 | 73 |
if (0 != Objects.compare(constructorCopy, jlaCopy, Comparator.naturalOrder())) { |
17426
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
74 |
throw new Error("Copy not equal"); |
12859 | 75 |
} |
76 |
||
17426
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
77 |
// The following is extremely "evil". Never ever do this in non-test code. |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
78 |
jlaChars[0] = 'X'; |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
79 |
if (!"Xxemplar".equals(jlaCopy)) { |
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
80 |
throw new Error("jla.newStringUnsafe did not use provided string"); |
12859 | 81 |
} |
82 |
||
83 |
} |
|
84 |
||
85 |
public static void main(String[] args) { |
|
17426
45db19517346
8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents:
12859
diff
changeset
|
86 |
testNewUnsafeString(); |
12859 | 87 |
} |
88 |
} |