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., 51 Franklin Street, Fifth Floor, 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 void resolveClass(Class clazz
)
130 // There doesn't seem to be a need for this to do anything.
131 // Testing reveals that the JDK doesn't seem to do anything here,
136 * Helper to load a class from the bootstrap class loader.
138 * @param name the class name to load
139 * @param resolve whether to resolve it
140 * @return the class, loaded by the bootstrap classloader or null
141 * if the class wasn't found. Returning null is equivalent to throwing
142 * a ClassNotFoundException (but a possible performance optimization).
144 static final native Class
loadClass(String name
, boolean resolve
)
145 throws ClassNotFoundException
;
148 * Helper to load a resource from the bootstrap class loader.
150 * In libgcj, this does nothing, as the default system loader knows
151 * how to find resources that have been linked in.
153 * @param name the resource to find
154 * @return the URL to the resource
156 static URL
getResource(String name
)
158 if (bootLoader
!= null)
159 return bootLoader
.bootGetResource(name
);
164 * Helper to get a list of resources from the bootstrap class loader.
166 * In libgcj, this does nothing, as the default system loader knows
167 * how to find resources that have been linked in.
169 * @param name the resource to find
170 * @return an enumeration of resources
171 * @throws IOException if one occurs
173 static Enumeration
getResources(String name
) throws IOException
175 if (bootLoader
!= null)
176 return bootLoader
.bootGetResources(name
);
177 return EmptyEnumeration
.getInstance();
181 * Helper to get a package from the bootstrap class loader. The default
182 * implementation of returning null may be adequate, or you may decide
183 * that this needs some native help.
185 * @param name the name to find
186 * @return the named package, if it exists
188 static synchronized Package
getPackage(String name
)
190 return (Package
) definedPackages
.get(name
);
194 * Helper to get all packages from the bootstrap class loader. The default
195 * implementation of returning an empty array may be adequate, or you may
196 * decide that this needs some native help.
198 * @return all named packages, if any exist
200 static synchronized Package
[] getPackages()
202 Package
[] packages
= new Package
[definedPackages
.size()];
203 return (Package
[]) definedPackages
.values().toArray(packages
);
206 // Define a package for something loaded natively.
207 static synchronized void definePackageForNative(String className
)
209 int lastDot
= className
.lastIndexOf('.');
212 String packageName
= className
.substring(0, lastDot
);
213 if (getPackage(packageName
) == null)
215 // FIXME: this assumes we're defining the core, which
216 // isn't necessarily so. We could detect this and set up
217 // appropriately. We could also look at a manifest file
218 // compiled into the .so.
219 Package p
= new Package(packageName
,
220 "Java Platform API Specification",
221 "GNU", "1.4", "gcj", "GNU",
222 null, // FIXME: gcj version.
224 definedPackages
.put(packageName
, p
);
230 * Helper for java.lang.Integer, Byte, etc to get the TYPE class
231 * at initialization time. The type code is one of the chars that
232 * represents the primitive type as in JNI.
235 * <li>'Z' - boolean</li>
236 * <li>'B' - byte</li>
237 * <li>'C' - char</li>
238 * <li>'D' - double</li>
239 * <li>'F' - float</li>
241 * <li>'J' - long</li>
242 * <li>'S' - short</li>
243 * <li>'V' - void</li>
246 * @param type the primitive type
247 * @return a "bogus" class representing the primitive type
249 static final native Class
getPrimitiveClass(char type
);
252 * The system default for assertion status. This is used for all system
253 * classes (those with a null ClassLoader), as well as the initial value for
254 * every ClassLoader's default assertion status.
256 * XXX - Not implemented yet; this requires native help.
258 * @return the system-wide default assertion status
260 static final boolean defaultAssertionStatus()
266 * The system default for package assertion status. This is used for all
267 * ClassLoader's packageAssertionStatus defaults. It must be a map of
268 * package names to Boolean.TRUE or Boolean.FALSE, with the unnamed package
269 * represented as a null key.
271 * XXX - Not implemented yet; this requires native help.
273 * @return a (read-only) map for the default packageAssertionStatus
275 static final Map
packageAssertionStatus()
277 return new HashMap();
281 * The system default for class assertion status. This is used for all
282 * ClassLoader's classAssertionStatus defaults. It must be a map of
283 * class names to Boolean.TRUE or Boolean.FALSE
285 * XXX - Not implemented yet; this requires native help.
287 * @return a (read-only) map for the default classAssertionStatus
289 static final Map
classAssertionStatus()
291 return new HashMap();
294 static native ClassLoader
getSystemClassLoaderInternal();
296 static native void initBootLoader(String libdir
);
298 static void initialize(String libdir
)
300 initBootLoader(libdir
);
303 = System
.getProperty ("gnu.gcj.runtime.VMClassLoader.library_control",
305 if ("never".equals(p
))
306 lib_control
= LIB_NEVER
;
307 else if ("cache".equals(p
))
308 lib_control
= LIB_CACHE
;
309 else if ("full".equals(p
))
310 lib_control
= LIB_FULL
;
312 lib_control
= LIB_NEVER
;
314 tried_libraries
= new HashSet();
318 * Possibly load a .so and search it for classes.
320 static native Class
nativeFindClass(String name
);
322 static ClassLoader
getSystemClassLoader()
324 // This method is called as the initialization of systemClassLoader,
325 // so if there is a null value, this is the first call and we must check
326 // for java.system.class.loader.
327 String loader
= System
.getProperty("java.system.class.loader");
328 ClassLoader default_sys
= getSystemClassLoaderInternal();
333 Class load_class
= Class
.forName(loader
, true, default_sys
);
335 = load_class
.getConstructor(new Class
[] { ClassLoader
.class });
337 = (ClassLoader
) c
.newInstance(new Object
[] { default_sys
});
341 throw new Error("Failed to load requested system classloader "