KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > JPF_java_lang_System


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.jvm;
20
21 /**
22  * MJI NativePeer class for java.lang.System library abstraction
23  */

24 public class JPF_java_lang_System {
25   public static void setErr0 (MJIEnv env, int clsObjRef, int streamRef) {
26     env.setStaticIntField("java.lang.System", "err", streamRef);
27   }
28
29   public static void setIn0 (MJIEnv env, int clsObjRef, int streamRef) {
30     env.setStaticIntField("java.lang.System", "in", streamRef);
31   }
32
33   public static void setOut0 (MJIEnv env, int clsObjRef, int streamRef) {
34     env.setStaticIntField("java.lang.System", "out", streamRef);
35   }
36
37   public static void $clinit (MJIEnv env, int clsObjRef) {
38     env.setStaticReferenceField("java.lang.System", "out",
39                              env.newObject("java.io.PrintStream"));
40     env.setStaticReferenceField("java.lang.System", "err",
41                              env.newObject("java.io.PrintStream"));
42   }
43
44   // <2do> - now this baby really needs to be fixed. Imagine what it
45
// does to an app that works with large vectors
46
public static void arraycopy (MJIEnv env, int clsObjRef, int srcArrayRef,
47                                 int srcIdx, int dstArrayRef, int dstIdx,
48                                 int length) {
49     int i;
50     
51     if ((srcArrayRef == -1) || (dstArrayRef == -1)) {
52       env.throwException("java.lang.NullPointerException");
53
54       return;
55     }
56
57     if (!env.isArray(srcArrayRef) || !env.isArray(dstArrayRef)) {
58       env.throwException("java.lang.ArrayStoreException");
59
60       return;
61     }
62
63     int sts = env.getArrayTypeSize(srcArrayRef);
64     int dts = env.getArrayTypeSize(dstArrayRef);
65
66     if (sts != dts) {
67       // not enough, real thing checks types !!
68
env.throwException("java.lang.ArrayStoreException");
69
70       return;
71     }
72
73     // ARGHH <2do> pcm - at some point, we should REALLY do this with a block
74
// operation (saves lots of state, speed if arraycopy is really used)
75
// we could also use the native checks in that case
76
int sl = env.getArrayLength(srcArrayRef);
77     int dl = env.getArrayLength(dstArrayRef);
78
79     // <2do> - we need detail messages!
80
if ((srcIdx < 0) || ((srcIdx + length) > sl)) {
81       env.throwException("java.lang.ArrayIndexOutOfBoundsException");
82
83       return;
84     }
85
86     if ((dstIdx < 0) || ((dstIdx + length) > dl)) {
87       env.throwException("java.lang.ArrayIndexOutOfBoundsException");
88
89       return;
90     }
91
92     if (sts == 1) {
93       for (i = 0; i < length; i++, srcIdx++, dstIdx++) {
94         int v = env.getIntArrayElement(srcArrayRef, srcIdx);
95         env.setIntArrayElement(dstArrayRef, dstIdx, v);
96       }
97     } else {
98       for (i = 0; i < length; i++, srcIdx++, dstIdx++) {
99         long v = env.getLongArrayElement(srcArrayRef, srcIdx);
100         env.setLongArrayElement(dstArrayRef, dstIdx, v);
101       }
102     }
103   }
104
105   // <2do> - this break every app which uses time delta thresholds
106
// (sort of "if ((t2 - t1) > d)"). Ok, we can't deal with
107
// real time, but we could at least give some SystemState dependent
108
// increment
109
public static long currentTimeMillis (MJIEnv env, int clsObjRef) {
110     return 1L;
111   }
112
113   // <2do> - now this implementation is dangerous if it's called from
114
// a context where we have subsequent unchecked stack frame access
115
// (the bytecodes). We better choose a 'exit' flag instead of
116
// this stack suicide
117
public static void exit (MJIEnv env, int clsObjRef, int ret) {
118     KernelState ks = env.getKernelState();
119     int length = ks.tl.length();
120
121     for (int i = 0; i < length; i++) {
122       ThreadInfo ti = ks.tl.get(i);
123
124       while (ti.countStackFrames() > 0) {
125         ti.popFrame();
126       }
127     }
128   }
129
130   public static void gc (MJIEnv env, int clsObjRef) {
131     env.getSystemState().activateGC();
132   }
133
134   public static int identityHashCode (MJIEnv env, int clsObjRef, int objref) {
135     return (objref ^ 0xABCD);
136   }
137
138   public static void registerNatives (MJIEnv env, int clsObjRef) {
139     // ignore
140
}
141   
142   /**
143    * <2do> pcm - replace this with a fixed set of system properties
144    */

145   public static int getProperty__Ljava_lang_String_2 (MJIEnv env, int clsObjRef, int keyRef) {
146     int r = MJIEnv.NULL;
147     
148     if (keyRef != MJIEnv.NULL) {
149       String JavaDoc k = env.getStringObject(keyRef);
150       String JavaDoc v = System.getProperty(k);
151       if (v != null) {
152         r = env.newString(v);
153       }
154     }
155     
156     return r;
157   }
158 }
159
Popular Tags