1 /* VMClassLoader.java -- Reference implementation of native interface
2 required by ClassLoader
3 Copyright (C) 1998, 2001, 2002, 2003, 2004, 2005 Free Software Foundation
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
41 import gnu
.java
.util
.EmptyEnumeration
;
42 import java
.lang
.reflect
.Constructor
;
44 import java
.io
.IOException
;
46 import java
.net
.URLClassLoader
;
47 import java
.security
.AllPermission
;
48 import java
.security
.Permission
;
49 import java
.security
.Permissions
;
50 import java
.security
.ProtectionDomain
;
51 import java
.util
.ArrayList
;
52 import java
.util
.Enumeration
;
53 import java
.util
.HashMap
;
54 import java
.util
.HashSet
;
56 import java
.util
.StringTokenizer
;
57 import gnu
.gcj
.runtime
.BootClassLoader
;
60 * java.lang.VMClassLoader is a package-private helper for VMs to implement
61 * on behalf of java.lang.ClassLoader.
64 * @author Mark Wielaard <mark@klomp.org>
65 * @author Eric Blake <ebb9@email.byu.edu>
67 final class VMClassLoader
69 // Protection Domain definitions
70 // FIXME: should there be a special protection domain used for native code?
72 // The permission required to check what a classes protection domain is.
73 static final Permission protectionDomainPermission
74 = new RuntimePermission("getProtectionDomain");
75 // The protection domain returned if we cannot determine it.
76 static ProtectionDomain unknownProtectionDomain
;
80 Permissions permissions
= new Permissions();
81 permissions
.add(new AllPermission());
82 unknownProtectionDomain
= new ProtectionDomain(null, permissions
);
85 static final HashMap definedPackages
= new HashMap();
87 // This is a helper for handling java.endorsed.dirs. It is null
88 // until we've initialized the system, at which point it is created.
89 static BootClassLoader bootLoader
;
91 // This keeps track of shared libraries we've already tried to load.
92 private static HashSet tried_libraries
;
94 // Holds one of the LIB_* constants; used to determine how shared
95 // library loads are done.
96 private static int lib_control
;
98 private static final int LIB_FULL
= 0;
99 private static final int LIB_CACHE
= 1;
100 private static final int LIB_NEVER
= 2;
103 * Helper to define a class using a string of bytes. This assumes that
104 * the security checks have already been performed, if necessary.
106 * <strong>For backward compatibility, this just ignores the protection
107 * domain; that is the wrong behavior, and you should directly implement
108 * this method natively if you can.</strong>
110 * @param name the name to give the class, or null if unknown
111 * @param data the data representing the classfile, in classfile format
112 * @param offset the offset into the data where the classfile starts
113 * @param len the length of the classfile data in the array
114 * @param pd the protection domain
115 * @return the class that was defined
116 * @throws ClassFormatError if data is not in proper classfile format
118 static final native Class
defineClass(ClassLoader cl
, String name
,
119 byte[] data
, int offset
, int len
,
121 throws ClassFormatError
;
124 * Helper to resolve all references to other classes from this class.
126 * @param c the class to resolve
128 static final native void resolveClass(Class clazz
);
130 static final void transformException(Class clazz
, Throwable x
)
133 if (x
instanceof LinkageError
)
134 e
= (LinkageError
) x
;
135 else if (x
instanceof ClassNotFoundException
)
137 e
= new NoClassDefFoundError("while resolving class: "
143 e
= new LinkageError ("unexpected exception during linking: "
151 * Helper to load a class from the bootstrap class loader.
153 * @param name the class name to load
154 * @param resolve whether to resolve it
155 * @return the class, loaded by the bootstrap classloader or null
156 * if the class wasn't found. Returning null is equivalent to throwing
157 * a ClassNotFoundException (but a possible performance optimization).
159 static final native Class
loadClass(String name
, boolean resolve
)
160 throws ClassNotFoundException
;
163 * Helper to load a resource from the bootstrap class loader.
165 * In libgcj, this does nothing, as the default system loader knows
166 * how to find resources that have been linked in.
168 * @param name the resource to find
169 * @return the URL to the resource
171 static URL
getResource(String name
)
173 if (bootLoader
!= null)
174 return bootLoader
.bootGetResource(name
);
179 * Helper to get a list of resources from the bootstrap class loader.
181 * In libgcj, this does nothing, as the default system loader knows
182 * how to find resources that have been linked in.
184 * @param name the resource to find
185 * @return an enumeration of resources
186 * @throws IOException if one occurs
188 static Enumeration
getResources(String name
) throws IOException
190 if (bootLoader
!= null)
191 return bootLoader
.bootGetResources(name
);
192 return EmptyEnumeration
.getInstance();
196 * Helper to get a package from the bootstrap class loader. The default
197 * implementation of returning null may be adequate, or you may decide
198 * that this needs some native help.
200 * @param name the name to find
201 * @return the named package, if it exists
203 static synchronized Package
getPackage(String name
)
205 return (Package
) definedPackages
.get(name
);
209 * Helper to get all packages from the bootstrap class loader. The default
210 * implementation of returning an empty array may be adequate, or you may
211 * decide that this needs some native help.
213 * @return all named packages, if any exist
215 static synchronized Package
[] getPackages()
217 Package
[] packages
= new Package
[definedPackages
.size()];
218 return (Package
[]) definedPackages
.values().toArray(packages
);
221 // Define a package for something loaded natively.
222 static synchronized void definePackageForNative(String className
)
224 int lastDot
= className
.lastIndexOf('.');
227 String packageName
= className
.substring(0, lastDot
);
228 if (getPackage(packageName
) == null)
230 // FIXME: this assumes we're defining the core, which
231 // isn't necessarily so. We could detect this and set up
232 // appropriately. We could also look at a manifest file
233 // compiled into the .so.
234 Package p
= new Package(packageName
,
235 "Java Platform API Specification",
236 "GNU", "1.4", "gcj", "GNU",
237 null, // FIXME: gcj version.
239 definedPackages
.put(packageName
, p
);
245 * Helper for java.lang.Integer, Byte, etc to get the TYPE class
246 * at initialization time. The type code is one of the chars that
247 * represents the primitive type as in JNI.
250 * <li>'Z' - boolean</li>
251 * <li>'B' - byte</li>
252 * <li>'C' - char</li>
253 * <li>'D' - double</li>
254 * <li>'F' - float</li>
256 * <li>'J' - long</li>
257 * <li>'S' - short</li>
258 * <li>'V' - void</li>
261 * @param type the primitive type
262 * @return a "bogus" class representing the primitive type
264 static final native Class
getPrimitiveClass(char type
);
267 * The system default for assertion status. This is used for all system
268 * classes (those with a null ClassLoader), as well as the initial value for
269 * every ClassLoader's default assertion status.
271 * XXX - Not implemented yet; this requires native help.
273 * @return the system-wide default assertion status
275 static final boolean defaultAssertionStatus()
281 * The system default for package assertion status. This is used for all
282 * ClassLoader's packageAssertionStatus defaults. It must be a map of
283 * package names to Boolean.TRUE or Boolean.FALSE, with the unnamed package
284 * represented as a null key.
286 * XXX - Not implemented yet; this requires native help.
288 * @return a (read-only) map for the default packageAssertionStatus
290 static final Map
packageAssertionStatus()
292 return new HashMap();
296 * The system default for class assertion status. This is used for all
297 * ClassLoader's classAssertionStatus defaults. It must be a map of
298 * class names to Boolean.TRUE or Boolean.FALSE
300 * XXX - Not implemented yet; this requires native help.
302 * @return a (read-only) map for the default classAssertionStatus
304 static final Map
classAssertionStatus()
306 return new HashMap();
309 static native ClassLoader
getSystemClassLoaderInternal();
311 static native void initBootLoader(String libdir
);
313 static void initialize(String libdir
)
315 initBootLoader(libdir
);
318 = System
.getProperty ("gnu.gcj.runtime.VMClassLoader.library_control",
320 if ("never".equals(p
))
321 lib_control
= LIB_NEVER
;
322 else if ("cache".equals(p
))
323 lib_control
= LIB_CACHE
;
324 else if ("full".equals(p
))
325 lib_control
= LIB_FULL
;
327 lib_control
= LIB_CACHE
;
329 tried_libraries
= new HashSet();
333 * Possibly load a .so and search it for classes.
335 static native Class
nativeFindClass(String name
);
337 static ClassLoader
getSystemClassLoader()
339 // This method is called as the initialization of systemClassLoader,
340 // so if there is a null value, this is the first call and we must check
341 // for java.system.class.loader.
342 String loader
= System
.getProperty("java.system.class.loader");
343 ClassLoader default_sys
= getSystemClassLoaderInternal();
348 Class load_class
= Class
.forName(loader
, true, default_sys
);
350 = load_class
.getConstructor(new Class
[] { ClassLoader
.class });
352 = (ClassLoader
) c
.newInstance(new Object
[] { default_sys
});
356 throw new Error("Failed to load requested system classloader "