Re: classgraph question

9 views
Skip to first unread message

Luke Hutchison

unread,
Mar 30, 2019, 8:48:56 AM3/30/19
to Denis Zhdanov, classgra...@googlegroups.com
Hi Denis, (+CC: classgraph-users, since the answer may be helpful for someone else)

Great question. Firstly, many people do not use Spring, or do not want to use Spring just to get classpath scanning. There is no general purpose classpath scanning framework in Java. ClassGraph handles an enormously wide range of classpath specification mechanisms, classloaders and runtime environments. Most users of ClassGraph are probably using it to achieve something like Spring scanning, but in other runtime environments:


Also, I have never used Spring's scanner, but it looks like it can detect only a small handful of scan types (annotation, assignable, aspectj, regex, custom). ClassGraph works very differently, it builds a data structure in memory ("the class graph") representing all information about scanned classes, and you can query that data structure in very complex ways to find classes of interest. You can even generate a GraphViz .dot file from the class graph, to visualize how classes are related and how they refer to each other. So if you want to just use ClassGraph to find annotations, subclasses, implemented interfaces, etc., then you can do that (in fact that is why ClassGraph was created), but it can do a lot more than that. At least a couple of the users of ClassGraph are using it to generate API documentation.

ClassGraph does not require you to load classes when they are found during a scan. It reads the classfile directly, and and builds up metadata objects (ClassInfo, AnnotationInfo, FieldInfo, MethodInfo, MethodParameterInfo) that can be queried without loading the class. This dramatically reduces scan time, since classloading can be time consuming. ClassGraph allows you to read the type descriptors and type signatures of classes, fields, methods, etc., and has metadata types for those too. With Spring's scanner, you get a class reference, and you can query the class with reflection after loading the class, but sometimes it's better to simply have all the metadata on hand in a more abstract form, rather than having to delve into reflection (which is also slow).

Also, I have never timed Spring's scanner, but I believe ClassGraph will probably be a few times faster than Spring at scanning a very large project -- actually quite a few users of ClassGraph are using it in Spring or Spring Boot because they have enormous projects, and they need scanning to be very fast. In general, ClassGraph is the fastest scanner available, after lots of careful work to parallelize it and optimize it. It also borrows an idea from Spring Boot of using a custom zipfile central directory parser to be able to extract entries from nested jarfiles (zipfiles inside zipfiles) without having to extract the entire inner zipfile.

ClassGraph can also scan JPMS modules, and last time I checked it was the only scanner that could do that (it's actually quite difficult and complicated to scan JPMS modules). I published information on StackOverflow about how to do it though, so maybe some other scanners have now implemented that.

I hope this helps! Feel free to ask followup questions, and there's a Gitter page too: https://gitter.im/classgraph/Lobby

Luke



On Sat, Mar 30, 2019 at 3:21 AM Denis Zhdanov <denzh...@gmail.com> wrote:
Hi Luke,

I'm Denis, just found your classgraph library on gitgub.

The project looks interesting, could you please share more details on what are its target use-cases from your point of view?

Being jvm developers, we implicitly use classpath scanning all the time with spring framework, however, classgraph is not integrated with it, and it's not quite clear how else we can use it.

Regards, Denis
Reply all
Reply to author
Forward
0 new messages