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.
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.MalformedURLException;
24 import java.nio.ByteBuffer;
25 import java.security.ProtectionDomain;
26 import java.util.Enumeration;
27 import java.util.Vector;
29 import sun.misc.CompoundEnumeration;
32 * @author Nastaran Shafiei <nastaran.shafiei@gmail.com>
34 * Model class for java.lang.ClassLoader
36 public abstract class ClassLoader {
38 private ClassLoader parent;
40 // This is JPF internal identifier which set to the globalId of the classLoader
45 protected ClassLoader() {
46 // constructed on the native side
49 protected ClassLoader (ClassLoader parent){
50 // constructed on the native side
53 private native String getResource0 (String rname);
55 public URL getResource(String name) {
59 String resourcePath = getSystemClassLoader().getResource0(name);
61 url = new URL(resourcePath);
62 } catch (MalformedURLException x){
66 url = parent.getResource(name);
70 url = findResource(name);
76 * Finds the resource with the given name. Class loader implementations
77 * should override this method to specify where to find resources.
79 protected URL findResource(String name) {
83 private native String[] getResources0 (String rname);
86 * Returns an array of URL including all resources with the given name
87 * found in the classpath of this classloader.
89 private Enumeration<URL> getResourcesURL(String name) {
90 String[] urls = getResources0(name);
91 Vector<URL> list = new Vector<URL>(0);
92 for(String url: urls) {
94 list.add(new URL(url));
95 } catch (MalformedURLException x){
100 return list.elements();
103 @SuppressWarnings({"unchecked","rawtypes"})
104 public Enumeration<URL> getResources(String name) throws IOException {
105 Enumeration<URL>[] resEnum = new Enumeration[2];
108 resEnum[0] = getSystemClassLoader().getResourcesURL(name);
110 resEnum[0] = parent.getResources(name);
112 resEnum[1] = findResources(name);
114 return new CompoundEnumeration<URL>(resEnum);
118 * Returns an enumeration representing all the resources with the given
119 * name. Class loader implementations should override this method to
120 * specify where to load resources from.
122 protected Enumeration<URL> findResources(String name) throws IOException {
123 return (new Vector<URL>()).elements();
126 public InputStream getResourceAsStream (String name){
127 URL foundResource = getResource(name);
128 if (foundResource != null) {
130 return foundResource.openStream();
131 } catch (IOException e) {
132 System.err.println("cannot open resource " + name);
138 public native static ClassLoader getSystemClassLoader ();
140 public static URL getSystemResource(String name){
141 return getSystemClassLoader().getResource(name);
144 public static InputStream getSystemResourceAsStream(String name) {
145 return getSystemClassLoader().getResourceAsStream(name);
148 public static Enumeration<URL> getSystemResources(String name) throws IOException {
149 return getSystemClassLoader().getResources(name);
152 public ClassLoader getParent() {
157 * If the class with the given name has been already defined, it is returned. OW, it
160 protected native final Class<?> findLoadedClass(String name);
162 protected native final Class<?> findSystemClass(String name) throws ClassNotFoundException;
164 public Class<?> loadClass(String name) throws ClassNotFoundException {
165 Class<?> c = findLoadedClass(name);
169 if (parent != null && parent != getSystemClassLoader()) {
170 c = parent.loadClass(name, false);
172 c = findSystemClass(name);
174 } catch (ClassNotFoundException e) {
182 protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
183 return loadClass(name);
187 * Finds the class with a given name. This method should be overridden by
188 * ClassLoader subclasses, and it will be used by loadClass().
190 protected Class<?> findClass(String name) throws ClassNotFoundException {
191 throw new ClassNotFoundException(name);
195 * All the class objects are resolved internally by JPF. So this method
198 protected final void resolveClass(Class<?> c) {
201 private native Class<?> defineClass0(String name, byte[] b, int off, int len);
203 protected final Class<?> defineClass(String name, byte[] b, int off, int len) throws ClassFormatError {
204 return defineClass0(name, b, off, len);
207 protected final Class<?> defineClass(String name, byte[] b, int off, int len, ProtectionDomain protectionDomain) throws ClassFormatError {
208 return defineClass(name, b, off, len);
211 protected String findLibrary(String libname) {
215 protected native Package getPackage(String name);
217 protected native Package[] getPackages();
219 public native void setDefaultAssertionStatus(boolean enabled);
221 public native void setClassAssertionStatus(String className, boolean enabled);
223 public native void setPackageAssertionStatus(String packageName, boolean enabled);
225 public native void clearAssertionStatus();
227 //--- unsupported methods
229 protected static boolean registerAsParallelCapable() {
230 return true; // dummy, in prep for jdk7
233 protected Object getClassLoadingLock(String className) {
234 throw new UnsupportedOperationException();
237 protected final Class<?> defineClass(byte[] b, int off, int len)
238 throws ClassFormatError {
239 throw new UnsupportedOperationException();
242 protected final Class<?> defineClass(String name, ByteBuffer b, ProtectionDomain protectionDomain)
243 throws ClassFormatError {
244 throw new UnsupportedOperationException();
247 protected final void setSigners(Class<?> c, Object[] signers) {
248 throw new UnsupportedOperationException();
251 protected Package definePackage(String name, String specTitle, String specVersion,
252 String specVendor, String implTitle, String implVersion,
253 String implVendor, URL sealBase)
254 throws IllegalArgumentException {
255 throw new UnsupportedOperationException();