RFR: 8349545: ClassLoader.definePackage() throws IllegalArgumentException if package already defined [v2]

Robert Stupp rstupp at openjdk.org
Mon Mar 24 07:53:26 UTC 2025


> Concurent calls to `ClassLoader.definePackage()` can yield `IllegalArgumentException`s if the package is already defined. Some built-in class loaders, like `URLClassLoader`, already handle this case, but custom class loaders (would) have to handle this case.
> 
> This change updates the logic of `CL.definePackage` to not throw an IAE if the "redefined" package is equal to the already defined one.
> 
> Tests added in `jdk/java/lang/Package/PackageDefineTest.java` (+ pulling up the `TestClassLoader` from `PackageDefineTest`).

Robert Stupp has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains three commits:

 - spec (javadoc)
 - move checks to CL + hoist IAE checks from URL/BuiltinClassLoaders
 - 8349545: ClassLoader.definePackage() throws IllegalArgumentException if package already defined
   
   Concurent calls to `ClassLoader.definePackage()` can yield `IllegalArgumentException`s if the package is already defined. Some built-in class loaders, like `URLClassLoader`, already handle this case, but custom class loaders (would) have to handle this case.
   
   This change updates the logic of `CL.definePackage` to not throw an IAE if the "redefined" package is equal to the already defined one. To enable this, `NamedPackage`, `Package` and `Package$VersionInfo` implement `hashCode`+`equals`, with the special handling to not involve a `URLStreamHandler` when comparing `VersionInfo.sealBase`.
   
   Tests added in `jdk/java/lang/Package/PackageDefineTest.java` (+ pulling up the `TestClassLoader` from `PackageDefineTest`).

-------------

Changes: https://git.openjdk.org/jdk/pull/23737/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=23737&range=01
  Stats: 260 lines in 6 files changed: 218 ins; 29 del; 13 mod
  Patch: https://git.openjdk.org/jdk/pull/23737.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/23737/head:pull/23737

PR: https://git.openjdk.org/jdk/pull/23737


More information about the core-libs-dev mailing list