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

Alan Bateman alanb at openjdk.org
Sun Feb 23 16:56:54 UTC 2025


On Sun, 23 Feb 2025 11:32:41 GMT, Robert Stupp <rstupp at openjdk.org> wrote:

> 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`).

It's a bug for a custom ClassLoader implementation to attempt to define the same Package more than once, so JDK-8349545 is really an issue that should be fixed in the custom class loader. The proposal here effectively changes definePackage to be "definePackageIfNotDefined" so that custom class loaders don't need to coordinate concurrent attempts to define a Package. This is a significant change in behavior to a JDK 1.2 era API so caution is needed, and will require effort to work through the implications. So I think more thinking required to decide if JDK-8349545 should be closed or whether the inconvenience for subclasses is worth introducing a new API rather changing the behavior of definePackage.

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

PR Comment: https://git.openjdk.org/jdk/pull/23737#issuecomment-2676985791


More information about the core-libs-dev mailing list