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 private static JavaObjectInputStreamAccess javaObjectInputStreamAccess;
63 // (required for EnumSet ops)
64 public static JavaLangAccess getJavaLangAccess() {
65 return javaLangAccess;
67 // automatically called by java.lang.System clinit
68 public static void setJavaLangAccess(JavaLangAccess jla) {
73 public static void setJavaNetAccess(JavaNetAccess jna) {
76 // automatically called by java.net.URLClassLoader clinit
77 public static JavaNetAccess getJavaNetAccess() {
82 public static JavaUtilJarAccess javaUtilJarAccess() {
83 if (javaUtilJarAccess == null) {
84 unsafe.ensureClassInitialized(JarFile.class);
86 return javaUtilJarAccess;
88 public static void setJavaUtilJarAccess(JavaUtilJarAccess access) {
89 javaUtilJarAccess = access;
93 public static void setJavaIOAccess(JavaIOAccess jia) {
96 // this is normally done by java.io.Console, which is not in Java 1.5
97 // since this is a rather big beast with lost of bytecode, we don't add
99 public static JavaIOAccess getJavaIOAccess() {
100 if (javaIOAccess == null) {
101 //unsafe.ensureClassInitialized(Console.class);
102 throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaIOAccess() not supported yet");
108 public static void setJavaNioAccess(JavaNioAccess a) {
111 public static JavaNioAccess getJavaNioAccess() {
112 if (javaNioAccess == null) {
113 throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaNioAccess() not supported yet");
115 return javaNioAccess;
119 public static void setJavaIODeleteOnExitAccess(JavaIODeleteOnExitAccess jida) {
120 javaIODeleteOnExitAccess = jida;
123 public static JavaIODeleteOnExitAccess getJavaIODeleteOnExitAccess() {
124 if (javaIODeleteOnExitAccess == null) {
125 unsafe.ensureClassInitialized(File.class);
127 return javaIODeleteOnExitAccess;
130 public static void setJavaIOFileDescriptorAccess(JavaIOFileDescriptorAccess jiofda) {
131 javaIOFileDescriptorAccess = jiofda;
133 public static JavaIOFileDescriptorAccess getJavaIOFileDescriptorAccess() {
134 if (javaIOFileDescriptorAccess == null) {
135 unsafe.ensureClassInitialized(FileDescriptor.class);
136 throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess() not supported yet");
139 return javaIOFileDescriptorAccess;
142 public static JavaObjectInputStreamAccess getJavaObjectInputStreamAccess() {
143 if (javaObjectInputStreamAccess == null) {
144 unsafe.ensureClassInitialized(ObjectInputStream.class);
145 throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaObjectInputStreamAccess() not supported yet");
148 return javaObjectInputStreamAccess;
151 public static void setJavaObjectInputStreamAccess(JavaObjectInputStreamAccess access) {
152 javaObjectInputStreamAccess = access;
155 public static void setJavaAWTAccess (JavaAWTAccess jaa){
158 public static JavaAWTAccess getJavaAWTAccess(){
159 return javaAWTAccess;
162 public static void setJavaOISAccess(JavaOISAccess access) {
163 javaOISAccess = access;
166 public static JavaOISAccess getJavaOISAccess() {
167 if (javaOISAccess == null) {
168 unsafe.ensureClassInitialized(ObjectInputStream.class);
169 throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaOISAccess() not supported yet");
172 return javaOISAccess;