2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 import java.io.FileDescriptor;
22 import java.io.ObjectInputStream;
23 import java.util.jar.JarFile;
26 * This is a backdoor mechanism in Java 6 to allow (some sort of)
27 * controlled access to internals between packages, using
28 * sun.misc.* interfaces (e.g. JavaLangAccess) that are anonymously
29 * instantiated within the exporting package (e.g. java.lang), and
30 * obtained via SharedSecrets, which in turn obtains the
31 * instances from sun.misc.Unsafe. For most packages these interface
32 * objects are created on demand by static init of some key classes of
33 * these packages that call the SharedSecrets setters
34 * (except for JavaLangAccess and JavaNetAccess)
36 * Since this is used from within the standard libraries of Java 6, we need
37 * some sort of support, but we don't want to break Java 1.5 yet by introducing
38 * lots of Java 6 dependencies, which would force us to duplicate their code
39 * even though it might be pure Java (like java.io.Console).
41 * This is a can of worms, which we only open partially to support
42 * EnumSets for both Java 1.5 and 6. We make the cut at java.* packages -
43 * if the backdoor interfaces/types require anything outside sun.* packages,
46 * All of this is hopefully going away when we drop Java 1.5 support, and is
47 * to be replaced by some native peers providing the required native calls
49 public class SharedSecrets {
50 private static final Unsafe unsafe = Unsafe.getUnsafe();
52 private static JavaUtilJarAccess javaUtilJarAccess;
53 private static JavaLangAccess javaLangAccess;
54 private static JavaIOAccess javaIOAccess;
55 private static JavaIODeleteOnExitAccess javaIODeleteOnExitAccess;
56 private static JavaNetAccess javaNetAccess;
57 private static JavaIOFileDescriptorAccess javaIOFileDescriptorAccess;
58 private static JavaNioAccess javaNioAccess;
59 private static JavaAWTAccess javaAWTAccess;
60 private static JavaOISAccess javaOISAccess;
61 // removed in Java8u161
62 // private static JavaObjectInputStreamAccess javaObjectInputStreamAccess;
64 // (required for EnumSet ops)
65 public static JavaLangAccess getJavaLangAccess() {
66 return javaLangAccess;
68 // automatically called by java.lang.System clinit
69 public static void setJavaLangAccess(JavaLangAccess jla) {
74 public static void setJavaNetAccess(JavaNetAccess jna) {
77 // automatically called by java.net.URLClassLoader clinit
78 public static JavaNetAccess getJavaNetAccess() {
83 public static JavaUtilJarAccess javaUtilJarAccess() {
84 if (javaUtilJarAccess == null) {
85 unsafe.ensureClassInitialized(JarFile.class);
87 return javaUtilJarAccess;
89 public static void setJavaUtilJarAccess(JavaUtilJarAccess access) {
90 javaUtilJarAccess = access;
94 public static void setJavaIOAccess(JavaIOAccess jia) {
97 // this is normally done by java.io.Console, which is not in Java 1.5
98 // since this is a rather big beast with lost of bytecode, we don't add
100 public static JavaIOAccess getJavaIOAccess() {
101 if (javaIOAccess == null) {
102 //unsafe.ensureClassInitialized(Console.class);
103 throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaIOAccess() not supported yet");
109 public static void setJavaNioAccess(JavaNioAccess a) {
112 public static JavaNioAccess getJavaNioAccess() {
113 if (javaNioAccess == null) {
114 throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaNioAccess() not supported yet");
116 return javaNioAccess;
120 public static void setJavaIODeleteOnExitAccess(JavaIODeleteOnExitAccess jida) {
121 javaIODeleteOnExitAccess = jida;
124 public static JavaIODeleteOnExitAccess getJavaIODeleteOnExitAccess() {
125 if (javaIODeleteOnExitAccess == null) {
126 unsafe.ensureClassInitialized(File.class);
128 return javaIODeleteOnExitAccess;
131 public static void setJavaIOFileDescriptorAccess(JavaIOFileDescriptorAccess jiofda) {
132 javaIOFileDescriptorAccess = jiofda;
134 public static JavaIOFileDescriptorAccess getJavaIOFileDescriptorAccess() {
135 if (javaIOFileDescriptorAccess == null) {
136 unsafe.ensureClassInitialized(FileDescriptor.class);
137 throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess() not supported yet");
140 return javaIOFileDescriptorAccess;
143 public static JavaObjectInputStreamAccess getJavaObjectInputStreamAccess() {
144 if (javaObjectInputStreamAccess == null) {
145 unsafe.ensureClassInitialized(ObjectInputStream.class);
146 throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaObjectInputStreamAccess() not supported yet");
149 return javaObjectInputStreamAccess;
152 public static void setJavaObjectInputStreamAccess(JavaObjectInputStreamAccess access) {
153 javaObjectInputStreamAccess = access;
156 public static void setJavaAWTAccess (JavaAWTAccess jaa){
159 public static JavaAWTAccess getJavaAWTAccess(){
160 return javaAWTAccess;
163 public static void setJavaOISAccess(JavaOISAccess access) {
164 javaOISAccess = access;
167 public static JavaOISAccess getJavaOISAccess() {
168 if (javaOISAccess == null) {
169 unsafe.ensureClassInitialized(ObjectInputStream.class);
170 throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaOISAccess() not supported yet");
173 return javaOISAccess;