Proper way to scan all classes inside application/war files

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Proper way to scan all classes inside application/war files

Sundara Mohan M
Hi,
   I was scanning all classes (to find all annotated class) using the
URLClassLoader.getUrls() methods to find all URL and find it with JDK8.
Since JDK9 onwards all App/System Class loaders are not deriving from
URLClassLoader it doesn't work anymore. (solution of making ucp variable
inside BuiltinClassLoader accesible and reading URLs works).

What is the proper way to scan all(AppClassLoader + any other class loader)
classes?

Any pointers on this will be helpful.


Thanks
Sundar
Reply | Threaded
Open this post in threaded view
|

Re: Proper way to scan all classes inside application/war files

Alan Bateman
On 19/04/2019 01:33, Sundara Mohan M wrote:
> Hi,
>     I was scanning all classes (to find all annotated class) using the
> URLClassLoader.getUrls() methods to find all URL and find it with JDK8.
> Since JDK9 onwards all App/System Class loaders are not deriving from
> URLClassLoader it doesn't work anymore. (solution of making ucp variable
> inside BuiltinClassLoader accesible and reading URLs works).
I think you are looking for the "java.class.path" system property. Split
the value, map each element to a file path and file URL, and you should
the equivalent to the array of URLs that getURLs would have given you.
Same limitations too in that it's just the initial class path and
doesn't taken into account additions that arise when JAR files have the
Class-Path attribute.

>
> What is the proper way to scan all(AppClassLoader + any other class loader)
> classes?
>
This is a bigger question. I suspect you are looking to enumerate all
the resources in all modules in the boot layer. The following will print
the names of all resources in all modules in the boot layer and might
give you ideas:

      ModuleLayer.boot().configuration().modules().forEach(m -> {
             System.out.format("module %s%n", m.name());
             try (ModuleReader reader = m.reference().open()) {
                 reader.list().forEach(rn -> System.out.format("   
%s%n", rn));
             } catch (IOException ioe) {
                 throw new UncheckedIOException(ioe);
             }
         });

In addition you want the resources on the class path and I think you
have code for this already once you map the value of java.class.path to
a sequence of file URLs.

-Alan
Reply | Threaded
Open this post in threaded view
|

Re: Proper way to scan all classes inside application/war files

Sundara Mohan M
Thank you Alan


On Fri, Apr 19, 2019 at 12:48 AM Alan Bateman <[hidden email]>
wrote:

> On 19/04/2019 01:33, Sundara Mohan M wrote:
> > Hi,
> >     I was scanning all classes (to find all annotated class) using the
> > URLClassLoader.getUrls() methods to find all URL and find it with JDK8.
> > Since JDK9 onwards all App/System Class loaders are not deriving from
> > URLClassLoader it doesn't work anymore. (solution of making ucp variable
> > inside BuiltinClassLoader accesible and reading URLs works).
> I think you are looking for the "java.class.path" system property. Split
> the value, map each element to a file path and file URL, and you should
> the equivalent to the array of URLs that getURLs would have given you.
> Same limitations too in that it's just the initial class path and
> doesn't taken into account additions that arise when JAR files have the
> Class-Path attribute.
>
> >
> > What is the proper way to scan all(AppClassLoader + any other class
> loader)
> > classes?
> >
> This is a bigger question. I suspect you are looking to enumerate all
> the resources in all modules in the boot layer. The following will print
> the names of all resources in all modules in the boot layer and might
> give you ideas:
>
>       ModuleLayer.boot().configuration().modules().forEach(m -> {
>              System.out.format("module %s%n", m.name());
>              try (ModuleReader reader = m.reference().open()) {
>                  reader.list().forEach(rn -> System.out.format("
> %s%n", rn));
>              } catch (IOException ioe) {
>                  throw new UncheckedIOException(ioe);
>              }
>          });
>
> In addition you want the resources on the class path and I think you
> have code for this already once you map the value of java.class.path to
> a sequence of file URLs.
>
> -Alan
>