Analysing an entire project in one go

JavaParser works great to analyse java code and provides a way to process a source directory in one go. But it was lacking a way to analyse a project, which might consists of multiple source directories.

Previously the answer was to manually create all SourceRoots, which you could then analyse. Let’s review the following example for the JavaParser repository. In order to parse all files within the repository, you had to manually define the roots of each module and then create a SourceRoot with each source root, which you could then process further.

We needed a way to achieve this automatically, saving everyone the trouble of reinventing the wheel.
For this we introduce ProjectRootCollectionContext and CollectionStrategy. If you only require to parse java files, it is sufficient enough to only collect the SourceRoots within the project. However, if you also want to resolve symbols within the java files, you need to collect jar files as well. You can set the strategy of you choosing with CollectionContext. With either ParserCollectionStrategy or SymbolSolverCollectionStrategy you can specify whether you want to parse or resolve respectively.

The following examples show how to initialise the ProjectRoot:

Written by Daan Schipper

Be the first to comment

Leave a Reply

Your email address will not be published.


*