1 |
/* |
2 |
* Written by Martin Buchholz with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
package jsr166; |
8 |
|
9 |
import java.io.InvalidClassException; |
10 |
import java.io.ObjectInputStream; |
11 |
import java.lang.invoke.MethodHandle; |
12 |
import java.lang.invoke.MethodHandles; |
13 |
import static java.lang.invoke.MethodType.methodType; |
14 |
|
15 |
/** |
16 |
* A portability layer to allow running on a variety of JDKs. |
17 |
* |
18 |
* SharedSecrets was moved to a different package in jdk12. |
19 |
* |
20 |
* This package should never be integrated into openjdk. |
21 |
*/ |
22 |
public class Platform { |
23 |
/** |
24 |
* See JavaObjectInputStreamAccess#checkArray. |
25 |
* @param ois ... |
26 |
* @param arrayType ... |
27 |
* @param arrayLength ... |
28 |
* @throws InvalidClassException ... |
29 |
*/ |
30 |
public static void checkArray( |
31 |
ObjectInputStream ois, Class<?> arrayType, int arrayLength) |
32 |
throws InvalidClassException { |
33 |
try { |
34 |
checkArray.invoke(theJavaObjectInputStreamAccess, |
35 |
ois, arrayType, arrayLength); |
36 |
} |
37 |
catch (InvalidClassException | RuntimeException x) { throw x; } |
38 |
catch (Throwable x) { throw new Error(x); } |
39 |
} |
40 |
|
41 |
private static final Object theJavaObjectInputStreamAccess; |
42 |
private static final MethodHandle checkArray; |
43 |
static { |
44 |
try { |
45 |
Class<?> sharedSecrets, access; |
46 |
try { |
47 |
sharedSecrets = Class.forName("jdk.internal.misc.SharedSecrets"); |
48 |
access = Class.forName("jdk.internal.misc.JavaObjectInputStreamAccess"); |
49 |
} catch (ClassNotFoundException retry) { |
50 |
sharedSecrets = Class.forName("jdk.internal.access.SharedSecrets"); |
51 |
access = Class.forName("jdk.internal.access.JavaObjectInputStreamAccess"); |
52 |
} |
53 |
MethodHandles.Lookup lookup = MethodHandles.lookup(); |
54 |
theJavaObjectInputStreamAccess = lookup.findStatic( |
55 |
sharedSecrets, "getJavaObjectInputStreamAccess", |
56 |
methodType(access)) |
57 |
.invoke(); |
58 |
checkArray = lookup.findVirtual( |
59 |
access, "checkArray", |
60 |
methodType(void.class, ObjectInputStream.class, Class.class, int.class)); |
61 |
} catch (Throwable e) { |
62 |
throw new ExceptionInInitializerError(e); |
63 |
} |
64 |
} |
65 |
} |