205 * or a fully qualified name as defined by 206 * <cite>The Java™ Language Specification</cite>. 207 * 208 * @jls 6.7 Fully Qualified Names 209 * @jls 13.1 The Form of a Binary 210 * @see #resolveClass(Class) 211 * @since 1.0 212 */ 213 public abstract class ClassLoader { 214 215 private static native void registerNatives(); 216 static { 217 registerNatives(); 218 } 219 220 // The parent class loader for delegation 221 // Note: VM hardcoded the offset of this field, thus all new fields 222 // must be added *after* it. 223 private final ClassLoader parent; 224 225 // the unnamed module for this ClassLoader 226 private final Module unnamedModule; 227 228 /** 229 * Encapsulates the set of parallel capable loader types. 230 */ 231 private static class ParallelLoaders { 232 private ParallelLoaders() {} 233 234 // the set of parallel capable loader types 235 private static final Set<Class<? extends ClassLoader>> loaderTypes = 236 Collections.newSetFromMap(new WeakHashMap<>()); 237 static { 238 synchronized (loaderTypes) { loaderTypes.add(ClassLoader.class); } 239 } 240 241 /** 242 * Registers the given class loader type as parallel capable. 243 * Returns {@code true} is successfully registered; {@code false} if 244 * loader's super class is not registered. 321 p = value; 322 // if definePackage is called by this class loader to define 323 // a package in a named module, this will return Package 324 // object of the same name. Package object may contain 325 // unexpected information but it does not impact the runtime. 326 // this assertion may be helpful for troubleshooting 327 assert value.module() == m; 328 } 329 } 330 return p; 331 } 332 333 private static Void checkCreateClassLoader() { 334 SecurityManager security = System.getSecurityManager(); 335 if (security != null) { 336 security.checkCreateClassLoader(); 337 } 338 return null; 339 } 340 341 private ClassLoader(Void unused, ClassLoader parent) { 342 this.parent = parent; 343 this.unnamedModule 344 = SharedSecrets.getJavaLangReflectModuleAccess() 345 .defineUnnamedModule(this); 346 if (ParallelLoaders.isRegistered(this.getClass())) { 347 parallelLockMap = new ConcurrentHashMap<>(); 348 package2certs = new ConcurrentHashMap<>(); 349 assertionLock = new Object(); 350 } else { 351 // no finer-grained lock; lock on the classloader instance 352 parallelLockMap = null; 353 package2certs = new Hashtable<>(); 354 assertionLock = this; 355 } 356 } 357 358 /** 359 * Creates a new class loader using the specified parent class loader for 360 * delegation. 361 * 362 * <p> If there is a security manager, its {@link 363 * SecurityManager#checkCreateClassLoader() 364 * <tt>checkCreateClassLoader</tt>} method is invoked. This may result in 365 * a security exception. </p> 366 * 367 * @param parent 368 * The parent class loader 369 * 370 * @throws SecurityException 371 * If a security manager exists and its 372 * <tt>checkCreateClassLoader</tt> method doesn't allow creation 373 * of a new class loader. 374 * 375 * @since 1.2 376 */ 377 protected ClassLoader(ClassLoader parent) { 378 this(checkCreateClassLoader(), parent); 379 } 380 381 /** 382 * Creates a new class loader using the <tt>ClassLoader</tt> returned by 383 * the method {@link #getSystemClassLoader() 384 * <tt>getSystemClassLoader()</tt>} as the parent class loader. 385 * 386 * <p> If there is a security manager, its {@link 387 * SecurityManager#checkCreateClassLoader() 388 * <tt>checkCreateClassLoader</tt>} method is invoked. This may result in 389 * a security exception. </p> 390 * 391 * @throws SecurityException 392 * If a security manager exists and its 393 * <tt>checkCreateClassLoader</tt> method doesn't allow creation 394 * of a new class loader. 395 */ 396 protected ClassLoader() { 397 this(checkCreateClassLoader(), getSystemClassLoader()); 398 } 399 400 // -- Class -- 401 402 /** 403 * Loads the class with the specified <a href="#name">binary name</a>. 404 * This method searches for classes in the same manner as the {@link 405 * #loadClass(String, boolean)} method. It is invoked by the Java virtual 406 * machine to resolve class references. Invoking this method is equivalent 407 * to invoking {@link #loadClass(String, boolean) <tt>loadClass(name, 408 * false)</tt>}. 409 * 410 * @param name 411 * The <a href="#name">binary name</a> of the class 412 * 413 * @return The resulting <tt>Class</tt> object 414 * 415 * @throws ClassNotFoundException 416 * If the class was not found 417 */ 418 public Class<?> loadClass(String name) throws ClassNotFoundException { 419 return loadClass(name, false); | 205 * or a fully qualified name as defined by 206 * <cite>The Java™ Language Specification</cite>. 207 * 208 * @jls 6.7 Fully Qualified Names 209 * @jls 13.1 The Form of a Binary 210 * @see #resolveClass(Class) 211 * @since 1.0 212 */ 213 public abstract class ClassLoader { 214 215 private static native void registerNatives(); 216 static { 217 registerNatives(); 218 } 219 220 // The parent class loader for delegation 221 // Note: VM hardcoded the offset of this field, thus all new fields 222 // must be added *after* it. 223 private final ClassLoader parent; 224 225 // class loader name 226 private final String name; 227 228 // the unnamed module for this ClassLoader 229 private final Module unnamedModule; 230 231 /** 232 * Encapsulates the set of parallel capable loader types. 233 */ 234 private static class ParallelLoaders { 235 private ParallelLoaders() {} 236 237 // the set of parallel capable loader types 238 private static final Set<Class<? extends ClassLoader>> loaderTypes = 239 Collections.newSetFromMap(new WeakHashMap<>()); 240 static { 241 synchronized (loaderTypes) { loaderTypes.add(ClassLoader.class); } 242 } 243 244 /** 245 * Registers the given class loader type as parallel capable. 246 * Returns {@code true} is successfully registered; {@code false} if 247 * loader's super class is not registered. 324 p = value; 325 // if definePackage is called by this class loader to define 326 // a package in a named module, this will return Package 327 // object of the same name. Package object may contain 328 // unexpected information but it does not impact the runtime. 329 // this assertion may be helpful for troubleshooting 330 assert value.module() == m; 331 } 332 } 333 return p; 334 } 335 336 private static Void checkCreateClassLoader() { 337 SecurityManager security = System.getSecurityManager(); 338 if (security != null) { 339 security.checkCreateClassLoader(); 340 } 341 return null; 342 } 343 344 private ClassLoader(Void unused, String name, ClassLoader parent) { 345 this.name = name; 346 this.parent = parent; 347 this.unnamedModule 348 = SharedSecrets.getJavaLangReflectModuleAccess() 349 .defineUnnamedModule(this); 350 if (ParallelLoaders.isRegistered(this.getClass())) { 351 parallelLockMap = new ConcurrentHashMap<>(); 352 package2certs = new ConcurrentHashMap<>(); 353 assertionLock = new Object(); 354 } else { 355 // no finer-grained lock; lock on the classloader instance 356 parallelLockMap = null; 357 package2certs = new Hashtable<>(); 358 assertionLock = this; 359 } 360 } 361 362 /** 363 * Creates a new class loader of the specified name and using the 364 * specified parent class loader for delegation. 365 * 366 * @param name class loader name; or {@code null} if not named 367 * @param parent the parent class loader 368 * 369 * @throws SecurityException 370 * If a security manager exists and its 371 * {@link SecurityManager#checkCreateClassLoader()} 372 * method doesn't allow creation of a new class loader. 373 * 374 * @since 9 375 */ 376 protected ClassLoader(String name, ClassLoader parent) { 377 this(checkCreateClassLoader(), name, parent); 378 } 379 380 381 /** 382 * Creates a new class loader using the specified parent class loader for 383 * delegation. 384 * 385 * <p> If there is a security manager, its {@link 386 * SecurityManager#checkCreateClassLoader() 387 * <tt>checkCreateClassLoader</tt>} method is invoked. This may result in 388 * a security exception. </p> 389 * 390 * @param parent 391 * The parent class loader 392 * 393 * @throws SecurityException 394 * If a security manager exists and its 395 * <tt>checkCreateClassLoader</tt> method doesn't allow creation 396 * of a new class loader. 397 * 398 * @since 1.2 399 */ 400 protected ClassLoader(ClassLoader parent) { 401 this(checkCreateClassLoader(), null, parent); 402 } 403 404 405 /** 406 * Creates a new class loader using the <tt>ClassLoader</tt> returned by 407 * the method {@link #getSystemClassLoader() 408 * <tt>getSystemClassLoader()</tt>} as the parent class loader. 409 * 410 * <p> If there is a security manager, its {@link 411 * SecurityManager#checkCreateClassLoader() 412 * <tt>checkCreateClassLoader</tt>} method is invoked. This may result in 413 * a security exception. </p> 414 * 415 * @throws SecurityException 416 * If a security manager exists and its 417 * <tt>checkCreateClassLoader</tt> method doesn't allow creation 418 * of a new class loader. 419 */ 420 protected ClassLoader() { 421 this(checkCreateClassLoader(), null, getSystemClassLoader()); 422 } 423 424 425 /** 426 * Returns the name of this class loader or {@code null} if 427 * this class loader is not named. 428 * 429 * @return name of this class loader; or {@code null} if 430 * this class loader is not named. 431 * 432 * @since 9 433 */ 434 public String getName() { 435 return name; 436 } 437 438 439 // -- Class -- 440 441 /** 442 * Loads the class with the specified <a href="#name">binary name</a>. 443 * This method searches for classes in the same manner as the {@link 444 * #loadClass(String, boolean)} method. It is invoked by the Java virtual 445 * machine to resolve class references. Invoking this method is equivalent 446 * to invoking {@link #loadClass(String, boolean) <tt>loadClass(name, 447 * false)</tt>}. 448 * 449 * @param name 450 * The <a href="#name">binary name</a> of the class 451 * 452 * @return The resulting <tt>Class</tt> object 453 * 454 * @throws ClassNotFoundException 455 * If the class was not found 456 */ 457 public Class<?> loadClass(String name) throws ClassNotFoundException { 458 return loadClass(name, false); |