1 /* VMClassLoader.java -- Reference implementation of native interface 2 required by ClassLoader 3 Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation 4 5 This file is part of GNU Classpath. 6 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) 10 any later version. 11 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. 16 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 20 02111-1307 USA. 21 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 25 combination. 26 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. */ 38 39 package java.lang; 40 41 import java.security.ProtectionDomain; 42 import java.net.URL; 43 import java.io.IOException; 44 import java.util.Enumeration; 45 import java.util.Map; 46 import java.util.HashMap; 47 import java.lang.reflect.Constructor; 48 import java.security.AllPermission; 49 import java.security.Permission; 50 import java.security.Permissions; 51 import java.security.ProtectionDomain; 52 53 import gnu.java.util.EmptyEnumeration; 54 55 /** 56 * java.lang.VMClassLoader is a package-private helper for VMs to implement 57 * on behalf of java.lang.ClassLoader. 58 * 59 * @author John Keiser 60 * @author Mark Wielaard <mark@klomp.org> 61 * @author Eric Blake <ebb9@email.byu.edu> 62 */ 63 final class VMClassLoader 64 { 65 // Protection Domain definitions 66 // FIXME: should there be a special protection domain used for native code? 67 68 // The permission required to check what a classes protection domain is. 69 static final Permission protectionDomainPermission 70 = new RuntimePermission("getProtectionDomain"); 71 // The protection domain returned if we cannot determine it. 72 static ProtectionDomain unknownProtectionDomain; 73 74 static 75 { 76 Permissions permissions = new Permissions(); permissions.add(new AllPermission())77 permissions.add(new AllPermission()); 78 unknownProtectionDomain = new ProtectionDomain(null, permissions); 79 } 80 81 /** 82 * Helper to define a class using a string of bytes. This assumes that 83 * the security checks have already been performed, if necessary. 84 * 85 * <strong>For backward compatibility, this just ignores the protection 86 * domain; that is the wrong behavior, and you should directly implement 87 * this method natively if you can.</strong> 88 * 89 * @param name the name to give the class, or null if unknown 90 * @param data the data representing the classfile, in classfile format 91 * @param offset the offset into the data where the classfile starts 92 * @param len the length of the classfile data in the array 93 * @param pd the protection domain 94 * @return the class that was defined 95 * @throws ClassFormatError if data is not in proper classfile format 96 */ defineClass(ClassLoader cl, String name, byte[] data, int offset, int len, ProtectionDomain pd)97 static final native Class defineClass(ClassLoader cl, String name, 98 byte[] data, int offset, int len, 99 ProtectionDomain pd) 100 throws ClassFormatError; 101 linkClass0(Class klass)102 static final native void linkClass0 (Class klass); markClassErrorState0(Class klass)103 static final native void markClassErrorState0 (Class klass); 104 105 /** 106 * Helper to resolve all references to other classes from this class. 107 * 108 * @param c the class to resolve 109 */ resolveClass(Class clazz)110 static final void resolveClass(Class clazz) 111 { 112 synchronized (clazz) 113 { 114 try 115 { 116 linkClass0 (clazz); 117 } 118 catch (Throwable x) 119 { 120 markClassErrorState0 (clazz); 121 122 LinkageError e; 123 if (x instanceof LinkageError) 124 e = (LinkageError) x; 125 else if (x instanceof ClassNotFoundException) 126 { 127 e = new NoClassDefFoundError("while resolving class: " 128 + clazz.getName()); 129 e.initCause (x); 130 } 131 else 132 { 133 e = new LinkageError ("unexpected exception during linking: " 134 + clazz.getName()); 135 e.initCause (x); 136 } 137 throw e; 138 } 139 } 140 } 141 142 /** 143 * Helper to load a class from the bootstrap class loader. 144 * 145 * @param name the class name to load 146 * @param resolve whether to resolve it 147 * @return the class, loaded by the bootstrap classloader or null 148 * if the class wasn't found. Returning null is equivalent to throwing 149 * a ClassNotFoundException (but a possible performance optimization). 150 */ loadClass(String name, boolean resolve)151 static final native Class loadClass(String name, boolean resolve) 152 throws ClassNotFoundException; 153 154 /** 155 * Helper to load a resource from the bootstrap class loader. 156 * 157 * In libgcj, this does nothing, as the default system loader knows 158 * how to find resources that have been linked in. 159 * 160 * @param name the resource to find 161 * @return the URL to the resource 162 */ getResource(String name)163 static URL getResource(String name) 164 { 165 return null; 166 } 167 168 /** 169 * Helper to get a list of resources from the bootstrap class loader. 170 * 171 * In libgcj, this does nothing, as the default system loader knows 172 * how to find resources that have been linked in. 173 * 174 * @param name the resource to find 175 * @return an enumeration of resources 176 * @throws IOException if one occurs 177 */ getResources(String name)178 static Enumeration getResources(String name) throws IOException 179 { 180 return EmptyEnumeration.getInstance(); 181 } 182 183 /** 184 * Helper to get a package from the bootstrap class loader. The default 185 * implementation of returning null may be adequate, or you may decide 186 * that this needs some native help. 187 * 188 * @param name the name to find 189 * @return the named package, if it exists 190 */ getPackage(String name)191 static Package getPackage(String name) 192 { 193 return null; 194 } 195 196 /** 197 * Helper to get all packages from the bootstrap class loader. The default 198 * implementation of returning an empty array may be adequate, or you may 199 * decide that this needs some native help. 200 * 201 * @return all named packages, if any exist 202 */ getPackages()203 static Package[] getPackages() 204 { 205 return new Package[0]; 206 } 207 208 /** 209 * Helper for java.lang.Integer, Byte, etc to get the TYPE class 210 * at initialization time. The type code is one of the chars that 211 * represents the primitive type as in JNI. 212 * 213 * <ul> 214 * <li>'Z' - boolean</li> 215 * <li>'B' - byte</li> 216 * <li>'C' - char</li> 217 * <li>'D' - double</li> 218 * <li>'F' - float</li> 219 * <li>'I' - int</li> 220 * <li>'J' - long</li> 221 * <li>'S' - short</li> 222 * <li>'V' - void</li> 223 * </ul> 224 * 225 * @param type the primitive type 226 * @return a "bogus" class representing the primitive type 227 */ getPrimitiveClass(char type)228 static final native Class getPrimitiveClass(char type); 229 230 /** 231 * The system default for assertion status. This is used for all system 232 * classes (those with a null ClassLoader), as well as the initial value for 233 * every ClassLoader's default assertion status. 234 * 235 * XXX - Not implemented yet; this requires native help. 236 * 237 * @return the system-wide default assertion status 238 */ defaultAssertionStatus()239 static final boolean defaultAssertionStatus() 240 { 241 return true; 242 } 243 244 /** 245 * The system default for package assertion status. This is used for all 246 * ClassLoader's packageAssertionStatus defaults. It must be a map of 247 * package names to Boolean.TRUE or Boolean.FALSE, with the unnamed package 248 * represented as a null key. 249 * 250 * XXX - Not implemented yet; this requires native help. 251 * 252 * @return a (read-only) map for the default packageAssertionStatus 253 */ packageAssertionStatus()254 static final Map packageAssertionStatus() 255 { 256 return new HashMap(); 257 } 258 259 /** 260 * The system default for class assertion status. This is used for all 261 * ClassLoader's classAssertionStatus defaults. It must be a map of 262 * class names to Boolean.TRUE or Boolean.FALSE 263 * 264 * XXX - Not implemented yet; this requires native help. 265 * 266 * @return a (read-only) map for the default classAssertionStatus 267 */ classAssertionStatus()268 static final Map classAssertionStatus() 269 { 270 return new HashMap(); 271 } 272 getSystemClassLoaderInternal()273 static native ClassLoader getSystemClassLoaderInternal(); 274 getSystemClassLoader()275 static ClassLoader getSystemClassLoader() 276 { 277 // This method is called as the initialization of systemClassLoader, 278 // so if there is a null value, this is the first call and we must check 279 // for java.system.class.loader. 280 String loader = System.getProperty("java.system.class.loader"); 281 ClassLoader default_sys = getSystemClassLoaderInternal(); 282 if (loader != null) 283 { 284 try 285 { 286 Class load_class = Class.forName(loader, true, default_sys); 287 Constructor c 288 = load_class.getConstructor(new Class[] { ClassLoader.class }); 289 default_sys 290 = (ClassLoader) c.newInstance(new Object[] { default_sys }); 291 } 292 catch (Exception e) 293 { 294 System.err.println("Requested system classloader " 295 + loader + " failed, using " 296 + "gnu.gcj.runtime.VMClassLoader"); 297 e.printStackTrace(); 298 } 299 } 300 return default_sys; 301 } 302 } 303