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.
19 package gov.nasa.jpf.vm;
21 import gov.nasa.jpf.annotation.MJI;
22 import gov.nasa.jpf.vm.ElementInfo;
23 import gov.nasa.jpf.vm.MJIEnv;
24 import gov.nasa.jpf.vm.NativePeer;
27 import java.io.IOException;
28 import java.net.MalformedURLException;
33 * intercept and forward some of the filesystem access methods. This is very
34 * slow, if a program uses this heavily we should keep the forwarding File
35 * object around and modify the model class accordingly
37 public class JPF_java_io_File extends NativePeer {
39 static File getFile(MJIEnv env, int objref) {
40 int fnref = env.getReferenceField(objref, "filename");
41 String fname = env.getStringObject(fnref);
42 return new File(fname);
45 static int createJPFFile(MJIEnv env, File file) {
46 int newFileRef = env.newObject("java.io.File");
47 ElementInfo fileEI = env.getModifiableElementInfo(newFileRef);
49 int fileNameRef = env.newString(file.getPath());
50 fileEI.setReferenceField("filename", fileNameRef);
56 public int getParentFile____Ljava_io_File_2(MJIEnv env, int objref) {
57 File thisFile = getFile(env, objref);
58 File parent = thisFile.getParentFile();
60 return createJPFFile(env, parent);
64 public int getAbsolutePath____Ljava_lang_String_2 (MJIEnv env, int objref) {
65 String pn = getFile(env,objref).getAbsolutePath();
66 return env.newString(pn);
70 public int getAbsoluteFile____Ljava_io_File_2 (MJIEnv env, int objref) {
71 File absoluteFile = getFile(env, objref).getAbsoluteFile();
72 return createJPFFile(env, absoluteFile);
76 public int getCanonicalPath____Ljava_lang_String_2 (MJIEnv env, int objref) {
78 String pn = getFile(env,objref).getCanonicalPath();
79 return env.newString(pn);
80 } catch (IOException iox) {
81 env.throwException("java.io.IOException", iox.getMessage());
87 public int getCanonicalFile____Ljava_io_File_2(MJIEnv env, int objref) {
89 File file = getFile(env, objref);
90 File canonicalFile = file.getCanonicalFile();
91 return createJPFFile(env, canonicalFile);
92 } catch (IOException iox) {
93 env.throwException("java.io.IOException", iox.getMessage());
99 @SuppressWarnings("deprecation")
101 public int getURLSpec____Ljava_lang_String_2 (MJIEnv env, int objref){
103 File f = getFile(env,objref);
105 return env.newString(url.toString());
106 } catch (MalformedURLException mfux) {
107 env.throwException("java.net.MalformedURLException", mfux.getMessage());
113 public int getURISpec____Ljava_lang_String_2 (MJIEnv env, int objref){
114 File f = getFile(env, objref);
116 return env.newString(uri.toString());
120 public boolean isAbsolute____Z (MJIEnv env, int objref) {
121 return getFile(env, objref).isAbsolute();
125 public boolean isDirectory____Z (MJIEnv env, int objref) {
126 return getFile(env,objref).isDirectory();
130 public boolean isFile____Z (MJIEnv env, int objref) {
131 return getFile(env,objref).isFile();
135 public boolean delete____Z (MJIEnv env, int objref) {
136 return getFile(env,objref).delete();
140 public long length____J (MJIEnv env, int objref) {
141 return getFile(env,objref).length();
145 public boolean canRead____Z (MJIEnv env, int objref) {
146 return getFile(env,objref).canRead();
150 public boolean canWrite____Z (MJIEnv env, int objref) {
151 return getFile(env,objref).canWrite();
155 public boolean exists____Z (MJIEnv env, int objref) {
156 return getFile(env,objref).exists();
160 public boolean createNewFile____Z(MJIEnv env, int objref) {
161 File fileToCreate = getFile(env, objref);
163 return fileToCreate.createNewFile();
165 } catch (IOException iox) {
166 env.throwException("java.io.IOException", iox.getMessage());
172 public int list_____3Ljava_lang_String_2(MJIEnv env, int objref){
173 File f=getFile(env,objref);
174 if (f.isDirectory()){
175 String[] farr=f.list();
176 return env.newStringArray(farr);
183 public int listRoots_____3Ljava_io_File_2(MJIEnv env, int classRef) {
184 File[] roots = File.listRoots();
185 int rootResultRef = env.newObjectArray("java.io.File", roots.length);
186 ElementInfo rootsEI = env.getModifiableElementInfo(rootResultRef);
188 for (int i = 0; i < roots.length; i++) {
189 int rootFileRef = createJPFFile(env, roots[i]);
190 rootsEI.setReferenceElement(i, rootFileRef);
193 return rootResultRef;
195 // <2do> ..and lots more