Fixing the ClassLoader.defineClass() method issue that could not find the necessary...
[jpf-core.git] / src / peers / gov / nasa / jpf / vm / JPF_java_net_URLClassLoader.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 gov.nasa.jpf.vm;
19
20 import gov.nasa.jpf.JPF;
21 import java.net.MalformedURLException;
22 import java.net.URL;
23
24 import gov.nasa.jpf.annotation.MJI;
25 import gov.nasa.jpf.util.JPFLogger;
26
27 /**
28  * @author Nastaran Shafiei <nastaran.shafiei@gmail.com>
29  * 
30  * Native peer for java.net.URLClassLoader
31  */
32 public class JPF_java_net_URLClassLoader extends JPF_java_lang_ClassLoader{
33
34   static JPFLogger log = JPF.getLogger("class");
35   
36   @MJI
37   public void addURL0__Ljava_lang_String_2__V (MJIEnv env, int objRef, int urlRef) throws MalformedURLException {
38     ClassLoaderInfo cl = env.getClassLoaderInfo(objRef);
39     String url = env.getStringObject(urlRef);
40
41     String path = null;
42     URL u = new URL(url);
43     String protocol = u.getProtocol();
44     if(protocol.equals("file")) {
45       path = u.getFile();
46     } else if(protocol.equals("jar")){
47       path = url.substring(url.lastIndexOf(':')+1, url.indexOf('!'));
48     } else {
49       // we don't support other protocols for now!
50       log.warning("unknown path element specification: ", url);
51       return;
52     }
53
54     cl.addClassPathElement(path);
55   }
56
57   @MJI
58   public int findClass__Ljava_lang_String_2__Ljava_lang_Class_2 (MJIEnv env, int objRef, int nameRef) {
59     String typeName = env.getStringObject(nameRef);
60     ClassLoaderInfo cl = env.getClassLoaderInfo(objRef);
61     ThreadInfo ti = env.getThreadInfo();
62
63     try {
64       ClassInfo ci = cl.getResolvedClassInfo( typeName);
65       if(!ci.isRegistered()) {
66         ci.registerClass(env.getThreadInfo());
67       }
68       // note that we don't initialize yet
69       return ci.getClassObjectRef();
70           
71     } catch (LoadOnJPFRequired rre) { // this classloader has a overridden loadClass 
72       env.repeatInvocation();
73       return MJIEnv.NULL;
74       
75     } catch (ClassInfoException cix){
76       if (cix.getCause() instanceof ClassParseException){
77         env.throwException("java.lang.ClassFormatError", typeName);
78       } else {
79         env.throwException("java.lang.ClassNotFoundException", typeName);
80       }
81       return MJIEnv.NULL;      
82     }
83   }
84
85   @MJI
86   public int findResource0__Ljava_lang_String_2__Ljava_lang_String_2 (MJIEnv env, int objRef, int resRef){
87     String rname = env.getStringObject(resRef);
88
89     ClassLoaderInfo cl = env.getClassLoaderInfo(objRef);
90
91     String resourcePath = cl.findResource(rname);
92
93     return env.newString(resourcePath);
94   }
95
96   @MJI
97   public int findResources0__Ljava_lang_String_2___3Ljava_lang_String_2 (MJIEnv env, int objRef, int resRef) {
98     String rname = env.getStringObject(resRef);
99
100     ClassLoaderInfo cl = env.getClassLoaderInfo(objRef);
101
102     String[] resources = cl.findResources(rname);
103
104     return env.newStringArray(resources);
105   }
106 }