Package.getPackage deprecation
Chen Liang
liangchenblue at gmail.com
Mon Aug 12 00:43:09 UTC 2024
Hi Stephen,
I agree: there's no way to get defined packages for the boot class loader
directly via Package.getDefinedPackage. They are not accessible via the
system or platform class loaders. The alternative approaches via
Package.getDefinedPackages() or getPackage() can be intercepted by custom
class loaders as well.
Since the introduction of the Module system, package is now seen more like
a property, that a package in a class loader always maps to one module.
(This trick has been useful in defining classes with MethodHandles.Lookup)
I think we probably need a way to work around this restriction; it indeed
makes little sense that regular java code cannot access the boot loader
packages reliably. I think we can do this as easy as making
ClassLoader.getPackages() and getPackage() public like getDefinedPackages()
or getDefinedPackage(), but the impact is unsure.
P.S. About the package class itself, we can probably upgrade its nested
VersionInfo class to a record, and make Package itself final.
Chen Liang
On Sun, Aug 11, 2024 at 5:50 PM Stephen Colebourne <scolebourne at joda.org>
wrote:
> On Sun, 11 Aug 2024 at 17:19, Alan Bateman <alan.bateman at oracle.com>
> wrote:
> > Package.getPackage is deprecated a long time, I don't think we've seen
> too many complaints. Nowadays it's probably not too useful except to get to
> package annotations (everything else in that API dates from JDK 1.2 and the
> since removed extension mechanism).
> >
> > The set of package names for packages in the modules defined to the boot
> loader can be obtained with code like this:
> >
> > ModuleLayer.boot()
> > .modules()
> > .stream()
> > .filter(m -> m.getClassLoader() == null)
> > .flatMap(m -> m.getPackages().stream())
> > .collect(Collectors.toSet());
> >
> > which I think is what you are looking for here.
>
> That doesn't quite work as it returns String package names, not
> Package objects (I need the Package object to maintain compatibility).
> Sounds like Package.getPackages() is the only current workaround.
>
> FWIW I do think there is a case to review the deprecation text of
> Package.getPackage(String).
>
> thanks
> Stephen
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20240811/b197cd90/attachment.htm>
More information about the core-libs-dev
mailing list