Initial import
[jpf-core.git] / src / classes / java / lang / System.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
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
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
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.
17  */
18 package java.lang;
19
20 import java.io.InputStream;
21 import java.io.PrintStream;
22 import java.nio.channels.Channel;
23 import java.util.Map;
24 import java.util.Properties;
25
26 import sun.misc.JavaLangAccess;
27 import sun.misc.SharedSecrets;
28 import sun.nio.ch.Interruptible;
29 import sun.reflect.ConstantPool;
30 import sun.reflect.annotation.AnnotationType;
31
32
33 public class System {
34
35   static Properties properties;
36
37   public static InputStream in; // new BufferedInputStream(...);  // <2do> not yet
38   public static PrintStream out;
39   public static PrintStream err;
40   
41   static {
42
43     out = createSystemOut();
44     err = createSystemErr();
45
46     properties = new Properties();
47
48     String[] kv = getKeyValuePairs();
49     for (int i=0; i<kv.length; i+=2){
50       String key = kv[i];
51       String val = kv[i+1];
52
53       if (key != null && val != null) {
54         properties.put(kv[i], kv[i+1]);
55       }
56     }
57
58     // this is the Java 6 sun.misc.SharedSecrets backdoor mechanism which I
59     // would have prefered not to learn about. It's a mess WRT Java 1.5 / 6 compatibility
60     // <2do> - most if this isn't supported yet
61     SharedSecrets.setJavaLangAccess( createJavaLangAccess());
62
63     // <2do> this is an approximation that isn't particularly safe since we don't
64     // initialize sun.misc.VM
65     //sun.misc.VM.booted();
66   }
67
68   static JavaLangAccess createJavaLangAccess () {
69     return new JavaLangAccess(){
70       @Override
71         public ConstantPool getConstantPool(Class<?> cls) {
72         throw new UnsupportedOperationException("JavaLangAccess.getConstantPool() not supported yet");
73         //return cls.getConstantPool();
74       }
75       @Override
76         public void setAnnotationType(Class<?> cls, AnnotationType type) {
77         throw new UnsupportedOperationException("JavaLangAccess.setAnnotationType() not supported yet");
78         //cls.setAnnotationType(type);
79       }
80       @Override
81         public AnnotationType getAnnotationType(Class<?> cls) {
82         throw new UnsupportedOperationException("JavaLangAccess.getAnnotationType() not supported yet");
83         //return cls.getAnnotationType();
84       }
85       @Override
86         public <E extends Enum<E>> E[] getEnumConstantsShared(Class<E> cls) {
87         return cls.getEnumConstantsShared();
88       }
89       @Override
90         public void blockedOn(Thread t, Interruptible b) {
91         throw new UnsupportedOperationException("JavaLangAccess.blockedOn() not supported yet");
92         //t.blockedOn(b);
93       }
94       @Override
95         public void registerShutdownHook(int slot, Runnable r) {
96         throw new UnsupportedOperationException("JavaLangAccess.registerShutdownHook() not supported yet");
97       }
98       @Override
99         public int getStackTraceDepth(Throwable t) {
100         return t.getStackTraceDepth();
101       }
102       @Override
103         public StackTraceElement getStackTraceElement(Throwable t, int i) {
104         StackTraceElement[] st = t.getStackTrace();
105         return st[i];
106       }
107     };
108   }
109
110   static private native String[] getKeyValuePairs();
111
112   static private native PrintStream createSystemOut();
113   static private native PrintStream createSystemErr();
114
115   //--- standard streams
116   public static void setIn (InputStream newIn) {
117     in = newIn;
118   }
119
120   public static void setOut (PrintStream newOut){
121     out = newOut;
122   }
123
124   public static void setErr (PrintStream newErr) {
125     err = newErr;
126   }
127
128   public static Channel inheritedChannel() {
129     throw new UnsupportedOperationException("inheritedChannel() not yet supported");
130   }
131
132   //--- misc
133   public static native void exit (int rc);
134   public static native void arraycopy (Object src, int srcPos,
135                                        Object dst, int dstPos, int len);
136   public static native void gc();
137   public static native void runFinalization();
138   public static native void runFinalizersOnExit(boolean cond);
139   static native Class<?> getCallerClass();
140   public static native int identityHashCode (Object o);
141
142
143   //--- time management
144   public static native long currentTimeMillis();
145   public static native long nanoTime();
146
147   //--- environment
148   public static native String getenv (String key);
149   public static Map<String,String> getenv() {
150     throw new UnsupportedOperationException("getenv() not yet supported");
151   }
152
153   //--- security manager
154   static SecurityManager securityManager;
155
156   public static void setSecurityManager (SecurityManager newManager) {
157     securityManager = newManager;
158   }
159
160   public static SecurityManager getSecurityManager() {
161     return securityManager;
162   }
163
164   //--- system properties
165
166   public static Properties getProperties() {
167     return properties;
168   }
169   public static void setProperties(Properties newProps){
170     properties = newProps;
171   }
172
173   public static String getProperty (String key) {
174     return properties.getProperty(key);
175   }
176
177   public static String getProperty (String key, String def){
178     String v = properties.getProperty(key);
179     if (v == null){
180       return def;
181     } else {
182       return v;
183     }
184   }
185
186   public static String setProperty (String key, String value){
187     String oldVal = properties.getProperty(key);
188     properties.put(key,value);
189     return oldVal;
190   }
191
192   public static String clearProperty (String key) {
193     String oldVal = properties.getProperty(key);
194     properties.remove(key);
195     return oldVal;
196   }
197
198   //--- native libs
199   public static void load (String pathName) {
200     // nothing, we don't have native libs
201     // (maybe we could on-demand load peers?)
202   }
203
204   public static void loadLibrary (String libName){
205     // nothing yet
206   }
207
208   public static String mapLibraryName (String libName){
209     // just a placeholder (Unix flavor)
210     return "lib" + libName + ".so";
211   }
212
213 }