Changeset 1391 for branches/GNU/src/gcc/libjava/java/lang/ClassLoader.java
- Timestamp:
- Apr 27, 2004, 8:39:34 PM (22 years ago)
- Location:
- branches/GNU/src/gcc
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
libjava/java/lang/ClassLoader.java (modified) (17 diffs, 1 prop)
Legend:
- Unmodified
- Added
- Removed
-
branches/GNU/src/gcc
- Property svn:ignore
-
old new 26 26 configure.vr 27 27 configure.vrs 28 28 29 Makefile 29 dir.info30 30 lost+found 31 31 update.out
-
- Property svn:ignore
-
branches/GNU/src/gcc/libjava/java/lang/ClassLoader.java
-
Property cvs2svn:cvs-rev
changed from
1.1to1.1.1.2
r1390 r1391 1 1 // ClassLoader.java - Define policies for loading Java classes. 2 2 3 /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation3 /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation 4 4 5 5 This file is part of libgcj. … … 14 14 import java.io.IOException; 15 15 import java.net.URL; 16 import java.net.URLConnection;17 16 import java.security.AllPermission; 18 17 import java.security.CodeSource; … … 21 20 import java.security.Policy; 22 21 import java.security.ProtectionDomain; 23 import java.util.Enumeration; 24 import java.util.HashMap; 25 import java.util.Stack; 22 import java.util.*; 26 23 27 24 /** 28 * The class <code>ClassLoader</code> is intended to be subclassed by 29 * applications in order to describe new ways of loading classes, 30 * such as over the network. 25 * The ClassLoader is a way of customizing the way Java gets its classes 26 * and loads them into memory. The verifier and other standard Java things 27 * still run, but the ClassLoader is allowed great flexibility in determining 28 * where to get the classfiles and when to load and resolve them. For that 29 * matter, a custom ClassLoader can perform on-the-fly code generation or 30 * modification! 31 31 * 32 * @author Kresten Krab Thorup 32 * <p>Every classloader has a parent classloader that is consulted before 33 * the 'child' classloader when classes or resources should be loaded. 34 * This is done to make sure that classes can be loaded from an hierarchy of 35 * multiple classloaders and classloaders do not accidentially redefine 36 * already loaded classes by classloaders higher in the hierarchy. 37 * 38 * <p>The grandparent of all classloaders is the bootstrap classloader, which 39 * loads all the standard system classes as implemented by GNU Classpath. The 40 * other special classloader is the system classloader (also called 41 * application classloader) that loads all classes from the CLASSPATH 42 * (<code>java.class.path</code> system property). The system classloader 43 * is responsible for finding the application classes from the classpath, 44 * and delegates all requests for the standard library classes to its parent 45 * the bootstrap classloader. Most programs will load all their classes 46 * through the system classloaders. 47 * 48 * <p>The bootstrap classloader in GNU Classpath is implemented as a couple of 49 * static (native) methods on the package private class 50 * <code>java.lang.VMClassLoader</code>, the system classloader is an 51 * instance of <code>gnu.java.lang.SystemClassLoader</code> 52 * (which is a subclass of <code>java.net.URLClassLoader</code>). 53 * 54 * <p>Users of a <code>ClassLoader</code> will normally just use the methods 55 * <ul> 56 * <li> <code>loadClass()</code> to load a class.</li> 57 * <li> <code>getResource()</code> or <code>getResourceAsStream()</code> 58 * to access a resource.</li> 59 * <li> <code>getResources()</code> to get an Enumeration of URLs to all 60 * the resources provided by the classloader and its parents with the 61 * same name.</li> 62 * </ul> 63 * 64 * <p>Subclasses should implement the methods 65 * <ul> 66 * <li> <code>findClass()</code> which is called by <code>loadClass()</code> 67 * when the parent classloader cannot provide a named class.</li> 68 * <li> <code>findResource()</code> which is called by 69 * <code>getResource()</code> when the parent classloader cannot provide 70 * a named resource.</li> 71 * <li> <code>findResources()</code> which is called by 72 * <code>getResource()</code> to combine all the resources with the 73 * same name from the classloader and its parents.</li> 74 * <li> <code>findLibrary()</code> which is called by 75 * <code>Runtime.loadLibrary()</code> when a class defined by the 76 * classloader wants to load a native library.</li> 77 * </ul> 78 * 79 * @author John Keiser 80 * @author Mark Wielaard 81 * @author Eric Blake 82 * @author Kresten Krab Thorup 83 * @see Class 84 * @since 1.0 85 * @status still missing 1.4 functionality 33 86 */ 34 35 87 public abstract class ClassLoader 36 88 { 37 private ClassLoader parent; 89 /** 90 * All classes loaded by this classloader. VM's may choose to implement 91 * this cache natively; but it is here available for use if necessary. It 92 * is not private in order to allow native code (and trusted subclasses) 93 * access to this field. 94 */ 95 final Map loadedClasses = new HashMap(); 96 97 /** 98 * The desired assertion status of classes loaded by this loader, if not 99 * overridden by package or class instructions. 100 */ 101 // Package visible for use by Class. 102 boolean defaultAssertionStatus = VMClassLoader.defaultAssertionStatus(); 103 104 /** 105 * The command-line state of the package assertion status overrides. This 106 * map is never modified, so it does not need to be synchronized. 107 */ 108 // Package visible for use by Class. 109 static final Map systemPackageAssertionStatus 110 = VMClassLoader.packageAssertionStatus(); 111 112 /** 113 * The map of package assertion status overrides, or null if no package 114 * overrides have been specified yet. The values of the map should be 115 * Boolean.TRUE or Boolean.FALSE, and the unnamed package is represented 116 * by the null key. This map must be synchronized on this instance. 117 */ 118 // Package visible for use by Class. 119 Map packageAssertionStatus; 120 121 /** 122 * The command-line state of the class assertion status overrides. This 123 * map is never modified, so it does not need to be synchronized. 124 */ 125 // Package visible for use by Class. 126 static final Map systemClassAssertionStatus 127 = VMClassLoader.classAssertionStatus(); 128 129 /** 130 * The map of class assertion status overrides, or null if no class 131 * overrides have been specified yet. The values of the map should be 132 * Boolean.TRUE or Boolean.FALSE. This map must be synchronized on this 133 * instance. 134 */ 135 // Package visible for use by Class. 136 Map classAssertionStatus; 137 138 /** 139 * The classloader that is consulted before this classloader. 140 * If null then the parent is the bootstrap classloader. 141 */ 142 private final ClassLoader parent; 143 144 /** 145 * All packages defined by this classloader. It is not private in order to 146 * allow native code (and trusted subclasses) access to this field. 147 */ 38 148 private HashMap definedPackages = new HashMap(); 39 149 150 151 152 153 154 155 156 157 158 40 159 public final ClassLoader getParent () 41 160 { 42 /* FIXME: security */ 161 // Check if we may return the parent classloader 162 SecurityManager sm = System.getSecurityManager(); 163 if (sm != null) 164 { 165 /* FIXME: security, getClassContext() not implemented. 166 Class c = VMSecurityManager.getClassContext()[1]; 167 ClassLoader cl = c.getClassLoader(); 168 if (cl != null && cl != this) 169 sm.checkPermission(new RuntimePermission("getClassLoader")); 170 */ 171 } 43 172 return parent; 44 173 } 45 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 46 200 public static ClassLoader getSystemClassLoader () 47 201 { … … 114 268 if (c == null) 115 269 { 116 try { 117 if (parent != null) 118 return parent.loadClass (name, link); 119 else 120 c = gnu.gcj.runtime.VMClassLoader.instance.findClass (name); 121 } catch (ClassNotFoundException ex) { 122 /* ignore, we'll try findClass */; 123 } 270 try 271 { 272 ClassLoader cl = parent; 273 if (parent == null) 274 cl = gnu.gcj.runtime.VMClassLoader.instance; 275 if (cl != this) 276 c = cl.loadClass (name, link); 277 } 278 catch (ClassNotFoundException ex) 279 { 280 /* ignore, we'll try findClass */; 281 } 124 282 } 125 283 … … 136 294 } 137 295 138 /** Find a class. This should be overridden by subclasses; the 139 * default implementation throws ClassNotFoundException. 140 * 141 * @param name Name of the class to find. 142 * @return The class found. 143 * @exception java.lang.ClassNotFoundException 144 * @since 1.2 145 */ 296 /** 297 * Called for every class name that is needed but has not yet been 298 * defined by this classloader or one of its parents. It is called by 299 * <code>loadClass()</code> after both <code>findLoadedClass()</code> and 300 * <code>parent.loadClass()</code> couldn't provide the requested class. 301 * 302 * <p>The default implementation throws a 303 * <code>ClassNotFoundException</code>. Subclasses should override this 304 * method. An implementation of this method in a subclass should get the 305 * class bytes of the class (if it can find them), if the package of the 306 * requested class doesn't exist it should define the package and finally 307 * it should call define the actual class. It does not have to resolve the 308 * class. It should look something like the following:<br> 309 * 310 * <pre> 311 * // Get the bytes that describe the requested class 312 * byte[] classBytes = classLoaderSpecificWayToFindClassBytes(name); 313 * // Get the package name 314 * int lastDot = name.lastIndexOf('.'); 315 * if (lastDot != -1) 316 * { 317 * String packageName = name.substring(0, lastDot); 318 * // Look if the package already exists 319 * if (getPackage(pkg) == null) 320 * { 321 * // define the package 322 * definePackage(packageName, ...); 323 * } 324 * } 325 * // Define and return the class 326 * return defineClass(name, classBytes, 0, classBytes.length); 327 * </pre> 328 * 329 * <p><code>loadClass()</code> makes sure that the <code>Class</code> 330 * returned by <code>findClass()</code> will later be returned by 331 * <code>findLoadedClass()</code> when the same class name is requested. 332 * 333 * @param name class name to find (including the package name) 334 * @return the requested Class 335 * @throws ClassNotFoundException when the class can not be found 336 * @since 1.2 337 */ 146 338 protected Class findClass (String name) 147 339 throws ClassNotFoundException … … 196 388 } 197 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 198 409 protected final Class defineClass(String name, byte[] data, int off, int len) 199 410 throws ClassFormatError … … 240 451 241 452 // as per 5.3.5.1 242 if (name != null &&findLoadedClass (name) != null)453 if (name != null findLoadedClass (name) != null) 243 454 throw new java.lang.LinkageError ("class " 244 455 + name 245 456 + " already loaded"); 246 457 247 458 if (protectionDomain == null) 248 459 protectionDomain = defaultProtectionDomain; 249 460 250 try { 251 // Since we're calling into native code here, 252 // we better make sure that any generated 253 // exception is to spec! 254 255 return defineClass0 (name, data, off, len, protectionDomain); 256 257 } catch (LinkageError x) { 258 throw x; // rethrow 259 260 } catch (java.lang.VirtualMachineError x) { 261 throw x; // rethrow 262 263 } catch (java.lang.Throwable x) { 264 // This should never happen, or we are beyond spec. 265 266 throw new InternalError ("Unexpected exception " 267 + "while defining class " 268 + name + ": " 269 + x.toString ()); 270 } 461 try 462 { 463 Class retval = defineClass0 (name, data, off, len, protectionDomain); 464 loadedClasses.put(retval.getName(), retval); 465 return retval; 466 } 467 catch (LinkageError x) 468 { 469 throw x; // rethrow 470 } 471 catch (VirtualMachineError x) 472 { 473 throw x; // rethrow 474 } 475 catch (Throwable x) 476 { 477 // This should never happen, or we are beyond spec. 478 InternalError r = new InternalError ("Unexpected exception " 479 + "while defining class " 480 + name); 481 r.initCause(x); 482 throw r; 483 } 271 484 } 272 485 … … 316 529 synchronized (clazz) 317 530 { 318 try { 319 linkClass0 (clazz); 320 } catch (Throwable x) { 321 markClassErrorState0 (clazz); 322 323 if (x instanceof Error) 324 throw (Error)x; 325 else 326 throw new java.lang.InternalError 327 ("unexpected exception during linking: " + x); 328 } 531 try 532 { 533 linkClass0 (clazz); 534 } 535 catch (Throwable x) 536 { 537 markClassErrorState0 (clazz); 538 539 LinkageError e; 540 if (x instanceof LinkageError) 541 e = (LinkageError)x; 542 else if (x instanceof ClassNotFoundException) 543 { 544 e = new NoClassDefFoundError("while resolving class: " 545 + clazz.getName()); 546 e.initCause (x); 547 } 548 else 549 { 550 e = new LinkageError ("unexpected exception during linking: " 551 + clazz.getName()); 552 e.initCause (x); 553 } 554 throw e; 555 } 329 556 } 330 557 } … … 394 621 * parents. 395 622 * 623 624 396 625 * @since 1.2 397 626 */ … … 419 648 * Returns all Package objects defined by this classloader and its parents. 420 649 * 650 421 651 * @since 1.2 422 652 */ … … 450 680 } 451 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 452 702 /** 453 703 * Returns a class found in a system-specific way, typically … … 466 716 } 467 717 468 /* 469 * Does currently nothing. FIXME. 470 */ 471 protected final void setSigners(Class claz, Object[] signers) { 472 /* claz.setSigners (signers); */ 718 /** 719 * Helper to set the signers of a class. This should be called after 720 * defining the class. 721 * 722 * @param c the Class to set signers of 723 * @param signers the signers to set 724 * @since 1.1 725 */ 726 protected final void setSigners(Class c, Object[] signers) 727 { 728 /* 729 * Does currently nothing. FIXME. 730 */ 473 731 } 474 732 … … 476 734 * If a class named <code>name</code> was previously loaded using 477 735 * this <code>ClassLoader</code>, then it is returned. Otherwise 478 * it returns <code>null</code>. (Unlike the JDK this is native, 479 * since we implement the class table internally.) 736 * it returns <code>null</code>. 480 737 * @param name class to find. 481 738 * @return the class loaded, or null. 482 739 */ 483 protected final native Class findLoadedClass(String name); 484 740 protected final synchronized Class findLoadedClass(String name) 741 { 742 return (Class) loadedClasses.get(name); 743 } 744 745 /** 746 * Get a resource using the system classloader. 747 * 748 * @param name the name of the resource relative to the system classloader 749 * @return an input stream for the resource, or null 750 * @since 1.1 751 */ 485 752 public static InputStream getSystemResourceAsStream(String name) { 486 753 return getSystemClassLoader().getResourceAsStream (name); 487 754 } 488 755 756 757 758 759 760 761 762 489 763 public static URL getSystemResource(String name) { 490 764 return getSystemClassLoader().getResource (name); 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 491 781 } 492 782 … … 503 793 public InputStream getResourceAsStream(String name) 504 794 { 505 try { 506 URL res = getResource (name); 507 if (res == null) return null; 508 return res.openStream (); 509 } catch (java.io.IOException x) { 510 return null; 511 } 795 try 796 { 797 URL res = getResource (name); 798 if (res == null) 799 return null; 800 return res.openStream (); 801 } 802 catch (java.io.IOException x) 803 { 804 return null; 805 } 512 806 } 513 807 … … 544 838 } 545 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 546 854 protected URL findResource (String name) 547 855 { … … 550 858 } 551 859 552 public final Enumeration getResources (String name) throws IOException 860 /** 861 * Returns an Enumeration of all resources with a given name that can 862 * be found by this classloader and its parents. Certain classloaders 863 * (such as the URLClassLoader when given multiple jar files) can have 864 * multiple resources with the same name that come from multiple locations. 865 * It can also occur that a parent classloader offers a resource with a 866 * certain name and the child classloader also offers a resource with that 867 * same name. <code>getResource() only offers the first resource (of the 868 * parent) with a given name. This method lists all resources with the 869 * same name. The name should use '/' as path separators. 870 * 871 * <p>The Enumeration is created by first calling <code>getResources()</code> 872 * on the parent classloader and then calling <code>findResources()</code> 873 * on this classloader. 874 * 875 * @param name the resource name 876 * @return an enumaration of all resources found 877 * @throws IOException if I/O errors occur in the process 878 * @since 1.2 879 */ 880 public final Enumeration getResources(String name) throws IOException 553 881 { 554 882 // The rules say search the parent class if non-null, … … 573 901 } 574 902 575 protected Enumeration findResources (String name) throws IOException 576 { 577 // Default to returning null. Derived classes implement this. 578 return null; 903 /** 904 * Called whenever all locations of a named resource are needed. 905 * It is called by <code>getResources()</code> after it has called 906 * <code>parent.getResources()</code>. The results are combined by 907 * the <code>getResources()</code> method. 908 * 909 * <p>The default implementation always returns an empty Enumeration. 910 * Subclasses should override it when they can provide an Enumeration of 911 * URLs (possibly just one element) to the named resource. 912 * The first URL of the Enumeration should be the same as the one 913 * returned by <code>findResource</code>. 914 * 915 * @param name the name of the resource to be found 916 * @return a possibly empty Enumeration of URLs to the named resource 917 * @throws IOException if I/O errors occur in the process 918 * @since 1.2 919 */ 920 protected Enumeration findResources(String name) throws IOException 921 { 922 return Collections.enumeration(Collections.EMPTY_LIST); 923 } 924 925 /** 926 * Set the default assertion status for classes loaded by this classloader, 927 * used unless overridden by a package or class request. 928 * 929 * @param enabled true to set the default to enabled 930 * @see #setClassAssertionStatus(String, boolean) 931 * @see #setPackageAssertionStatus(String, boolean) 932 * @see #clearAssertionStatus() 933 * @since 1.4 934 */ 935 public void setDefaultAssertionStatus(boolean enabled) 936 { 937 defaultAssertionStatus = enabled; 938 } 939 940 /** 941 * Set the default assertion status for packages, used unless overridden 942 * by a class request. This default also covers subpackages, unless they 943 * are also specified. The unnamed package should use null for the name. 944 * 945 * @param name the package (and subpackages) to affect 946 * @param enabled true to set the default to enabled 947 * @see #setDefaultAssertionStatus(String, boolean) 948 * @see #setClassAssertionStatus(String, boolean) 949 * @see #clearAssertionStatus() 950 * @since 1.4 951 */ 952 public synchronized void setPackageAssertionStatus(String name, 953 boolean enabled) 954 { 955 if (packageAssertionStatus == null) 956 packageAssertionStatus 957 = new HashMap(systemPackageAssertionStatus); 958 packageAssertionStatus.put(name, Boolean.valueOf(enabled)); 959 } 960 961 /** 962 * Set the default assertion status for a class. This only affects the 963 * status of top-level classes, any other string is harmless. 964 * 965 * @param name the class to affect 966 * @param enabled true to set the default to enabled 967 * @throws NullPointerException if name is null 968 * @see #setDefaultAssertionStatus(String, boolean) 969 * @see #setPackageAssertionStatus(String, boolean) 970 * @see #clearAssertionStatus() 971 * @since 1.4 972 */ 973 public synchronized void setClassAssertionStatus(String name, 974 boolean enabled) 975 { 976 if (classAssertionStatus == null) 977 classAssertionStatus = new HashMap(systemClassAssertionStatus); 978 // The toString() hack catches null, as required. 979 classAssertionStatus.put(name.toString(), Boolean.valueOf(enabled)); 980 } 981 982 /** 983 * Resets the default assertion status of this classloader, its packages 984 * and classes, all to false. This allows overriding defaults inherited 985 * from the command line. 986 * 987 * @see #setDefaultAssertionStatus(boolean) 988 * @see #setClassAssertionStatus(String, boolean) 989 * @see #setPackageAssertionStatus(String, boolean) 990 * @since 1.4 991 */ 992 public synchronized void clearAssertionStatus() 993 { 994 defaultAssertionStatus = false; 995 packageAssertionStatus = new HashMap(); 996 classAssertionStatus = new HashMap(); 579 997 } 580 998 } -
Property cvs2svn:cvs-rev
changed from
Note:
See TracChangeset
for help on using the changeset viewer.
