<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    On 11/08/2024 15:17, Stephen Colebourne wrote:<br>
    <blockquote type="cite" cite="mid:CACzrW9DSPwBR=bpib1tUCf+BFNouAAQKciDooC+vqpU93uNrpg@mail.gmail.com">
      <pre class="moz-quote-pre" wrap="">I make use of Package.getPackage in Joda-Convert but the method has
now been deprecated. I'd like to update my code, but AFAICT the
suggested alternative does not work.

The Joda-Convert code allows a user to convert a Package object to a
String, and back again. Reading the deprecation, I'm aware that this
may not work perfectly, but I do have to maintain compatibility as
best I can.

The deprecation asks users to use ClassLoader#getDefinedPackage
instead. To do this properly, users have to loop up the ClassLoader
parent list. However, the boot class loader is generally returned as
null, and it is obviously impossible to call getDefinedPackage on a
null reference.

ie.
Package.getPackage("java.util") works OK, but is deprecated

The suggested replacement is this code, but it does not work because
the boot class loader is returned as null:
 private static Package parsePackage(String str) {
  var loader = JDKStringConverter.class.getClassLoader();
  var pkg = (Package) null;
  while (loader != null && pkg == null) {
    pkg = loader.getDefinedPackage(str);
    loader = loader.getParent();
  }
  return pkg;
 }

Am I misunderstanding things?
</pre>
    </blockquote>
    <br>
    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).<br>
    <br>
    The set of package names for packages in the modules defined to the
    boot loader can be obtained with code like this:<br>
    <br>
        ModuleLayer.boot()<br>
                    .modules()<br>
                    .stream()<br>
                    .filter(m -> m.getClassLoader() == null)<br>
                    .flatMap(m -> m.getPackages().stream())<br>
                    .collect(Collectors.toSet());<br>
    <br>
    which I think is what you are looking for here.<br>
    <br>
    -Alan<br>
  </body>
</html>