Using PSIs to find the dependency graph of an API endpoint
We have a legacy Spring-groovy monolith which we need to decompose into micro-services and BFFs. It would be very useful to select a set of endpoints and then remove everything in the monolith that isn't depended on by the selected endpoints. Ideally this would result in working code, but falling short of that, just having the ability to remove everything from the project unrelated to a sub-set of endpoints would be hugely helpful just for analysis purposes.
Steps would include:
- remove any unused classes
- remove any unused functions
- remove any unused parameters
- remove any unreachable code
I'm not sure what the best way to do this would be. The brute force method would be to use PSIs, assuming this is even possible. Maybe there are already tools within the IDE that could be leveraged. Thoughts?
Thanks!
请先登录再写评论。
Not sure what kind of automation you have in mind here and I cannot imagine writing a plugin that would automate this fully.
Eventually doing this manually by deleting the Endpoint implementation first and then working further from there, re-invoking/re-analyzing the code base using inspections until everything is “green” again will work.
Hi Yann.
Perhaps I've set expectations too high with my long list of features, but it seems like IntelliJ includes the functionality needed to accomplish many of these tasks, if only there would be a way to automate everything in a well designed graph traversal.
For example, within the IDE, one can right-click on a function call and select 'Go to Implementation(s)'. If we do this recursively for each end-point, as well as for all of the constructors of each class visited (and their parent-classes), then, in theory, we will have identified all of the dependencies of the endpoints. [I acknowledge that there's some magical thinking here; I'm pretending like there aren't any unknown corner cases. Aspects/Annotation will be more complicated. I'm also ignoring test classes.]
Would this first step be possible using PSIs?
A courser approach would be to recursively traverse the import statements for the classes of each endpoint. That wouldn't even require PSIs. There would still be considerable cruft, but the task of manually eliminating it would be far easier.
Thanks for you input!
Yes, technically you could build a plugin that invokes all these steps programmatically and somehow presents the results or even does some (automatic) refactoring. But it is _far_ from trivial to build and test this.
Also consider writing some “macros” maybe that could help avoiding manual invocations of several steps https://plugins.jetbrains.com/docs/intellij/plugin-alternatives.html#liveplugin